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