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.
		
		
		
		
		
			
		
			
				
	
	
		
			43 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			TypeScript
		
	
			
		
		
	
	
			43 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			TypeScript
		
	
| // this file is a weird one as it is used by both sides of electron at the same time
 | |
| 
 | |
| import { LocaleMessagesType } from '../node/locale';
 | |
| 
 | |
| export const setupi18n = (locale: string, messages: LocaleMessagesType) => {
 | |
|   if (!locale) {
 | |
|     throw new Error('i18n: locale parameter is required');
 | |
|   }
 | |
|   if (!messages) {
 | |
|     throw new Error('i18n: messages parameter is required');
 | |
|   }
 | |
| 
 | |
|   function getMessage(key: string, substitutions: Array<string>) {
 | |
|     const message = messages[key];
 | |
|     if (!message) {
 | |
|       // tslint:disable-next-line: no-console
 | |
|       (window.log.error || console.log)(
 | |
|         `i18n: Attempted to get translation for nonexistent key '${key}'`
 | |
|       );
 | |
|       return '';
 | |
|     }
 | |
| 
 | |
|     if (Array.isArray(substitutions)) {
 | |
|       const replacedNameDollarSign = message.replaceAll('$', 'ᅲ');
 | |
| 
 | |
|       const substituted = substitutions.reduce(
 | |
|         (result, substitution) => result.replace(/ᅲ.+?ᅲ/, substitution),
 | |
|         replacedNameDollarSign
 | |
|       );
 | |
| 
 | |
|       return substituted.replaceAll('ᅲ', '$');
 | |
|     } else if (substitutions) {
 | |
|       return message.replace(/\$.+?\$/, substitutions);
 | |
|     }
 | |
| 
 | |
|     return message;
 | |
|   }
 | |
| 
 | |
|   getMessage.getLocale = () => locale;
 | |
| 
 | |
|   return getMessage;
 | |
| };
 |