You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
	
	
		
			38 lines
		
	
	
		
			854 B
		
	
	
	
		
			JavaScript
		
	
			
		
		
	
	
			38 lines
		
	
	
		
			854 B
		
	
	
	
		
			JavaScript
		
	
| /* eslint-env node */
 | |
| /* global log */
 | |
| 
 | |
| exports.setup = (locale, messages) => {
 | |
|   if (!locale) {
 | |
|     throw new Error('i18n: locale parameter is required');
 | |
|   }
 | |
|   if (!messages) {
 | |
|     throw new Error('i18n: messages parameter is required');
 | |
|   }
 | |
| 
 | |
|   function getMessage(key, substitutions) {
 | |
|     const entry = messages[key];
 | |
|     if (!entry) {
 | |
|       log.error(
 | |
|         `i18n: Attempted to get translation for nonexistent key '${key}'`
 | |
|       );
 | |
|       return '';
 | |
|     }
 | |
| 
 | |
|     const { message } = entry;
 | |
|     if (Array.isArray(substitutions)) {
 | |
|       return substitutions.reduce(
 | |
|         (result, substitution) => result.replace(/\$.+?\$/, substitution),
 | |
|         message
 | |
|       );
 | |
|     } else if (substitutions) {
 | |
|       return message.replace(/\$.+?\$/, substitutions);
 | |
|     }
 | |
| 
 | |
|     return message;
 | |
|   }
 | |
| 
 | |
|   getMessage.getLocale = () => locale;
 | |
| 
 | |
|   return getMessage;
 | |
| };
 |