Add option to merge extracted strings with existing translations (Thanks to @ocombe)
This commit is contained in:
		
							
								
								
									
										9
									
								
								src/compilers/compiler.interface.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/compilers/compiler.interface.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| import { TranslationCollection } from '../utils/translation.collection'; | ||||
|  | ||||
| export interface CompilerInterface { | ||||
|  | ||||
| 	compile(collection: TranslationCollection): string; | ||||
|  | ||||
| 	parse(contents: string): TranslationCollection; | ||||
|  | ||||
| } | ||||
							
								
								
									
										14
									
								
								src/compilers/json.compiler.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/compilers/json.compiler.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,14 @@ | ||||
| import { CompilerInterface } from './compiler.interface'; | ||||
| import { TranslationCollection } from '../utils/translation.collection'; | ||||
|  | ||||
| export class JsonCompiler implements CompilerInterface { | ||||
|  | ||||
| 	public compile(collection: TranslationCollection): string { | ||||
| 		return JSON.stringify(collection.values, null, '\t'); | ||||
| 	} | ||||
|  | ||||
| 	public parse(contents: string): TranslationCollection { | ||||
| 		return new TranslationCollection(JSON.parse(contents)); | ||||
| 	} | ||||
|  | ||||
| } | ||||
							
								
								
									
										53
									
								
								src/compilers/po.compiler.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								src/compilers/po.compiler.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,53 @@ | ||||
| import { CompilerInterface } from './compiler.interface'; | ||||
| import { TranslationCollection } from '../utils/translation.collection'; | ||||
|  | ||||
| import * as gettext from 'gettext-parser'; | ||||
|  | ||||
| export class PoCompiler implements CompilerInterface { | ||||
|  | ||||
| 	/** | ||||
| 	 * Translation domain | ||||
| 	 */ | ||||
| 	public domain = ''; | ||||
|  | ||||
| 	public compile(collection: TranslationCollection): string { | ||||
| 		const data = { | ||||
| 			charset: 'utf-8', | ||||
| 			headers: { | ||||
| 				'mime-version': '1.0', | ||||
| 				'content-type': 'text/plain; charset=utf-8', | ||||
| 				'content-transfer-encoding': '8bit' | ||||
| 			}, | ||||
| 			translations: { | ||||
| 				'default': Object.keys(collection.values).reduce((translations, key) => { | ||||
| 					translations[key] = { | ||||
| 						msgid: key, | ||||
| 						msgstr: collection.get(key) | ||||
| 					}; | ||||
| 					return translations; | ||||
| 				}, {}) | ||||
| 			} | ||||
| 		}; | ||||
|  | ||||
| 		return gettext.po.compile(data, 'utf-8'); | ||||
| 	} | ||||
|  | ||||
| 	public parse(contents: string): TranslationCollection { | ||||
| 		const collection = new TranslationCollection(); | ||||
|  | ||||
| 		const po = gettext.po.parse(contents, 'utf-8'); | ||||
| 		if (!po.translations.hasOwnProperty(this.domain)) { | ||||
| 			return collection; | ||||
| 		} | ||||
|  | ||||
| 		const values = Object.keys(po.translations[this.domain]) | ||||
| 			.filter(key => key.length > 0) | ||||
| 			.reduce((values, key) => { | ||||
| 				values[key] = po.translations[this.domain][key].msgstr.pop(); | ||||
| 				return values; | ||||
| 			}, {}); | ||||
|  | ||||
| 		return new TranslationCollection(values); | ||||
| 	} | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user