translation-adapter-ts
Advanced tools
Comparing version 1.0.7 to 1.1.0
@@ -7,2 +7,5 @@ "use strict"; | ||
class PofileTransformer { | ||
constructor(outputFormat) { | ||
this.outputFormat = outputFormat; | ||
} | ||
transformDirectory(path) { | ||
@@ -13,11 +16,13 @@ let files = fs_1.readdirSync(path); | ||
}).forEach(file => { | ||
this.createJson(path + '/' + file); | ||
this.createOutput(path + '/' + file); | ||
}); | ||
} | ||
createJson(file) { | ||
let data = fs_1.readFileSync(file, "utf-8").toString(); | ||
let jsonObject = this.encodePoStringToJSON(data); | ||
return fs_1.writeFileSync(file.replace(/.po$/, ".json"), JSON.stringify(jsonObject)); | ||
createOutput(fileName) { | ||
let rawData = fs_1.readFileSync(fileName, "utf-8").toString(); | ||
let dataObject = this.getStringsFromPoFile(rawData); | ||
let newFileName = fileName.replace(/.po$/, "." + this.outputFormat.getExtension()); | ||
let outputData = this.outputFormat.output(dataObject); | ||
return fs_1.writeFileSync(newFileName, outputData); | ||
} | ||
encodePoStringToJSON(data) { | ||
getStringsFromPoFile(data) { | ||
let parsedData = PO.parse(data); | ||
@@ -24,0 +29,0 @@ let result = {}; |
{ | ||
"name": "translation-adapter-ts", | ||
"version": "1.0.7", | ||
"version": "1.1.0", | ||
"description": "The translation adapter for i18n_mrg tool support in TypeScript projects.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -41,1 +41,26 @@ # How to install | ||
``` | ||
# Using with web JS | ||
```js | ||
import Localizator from 'translation-adapter-ts/build/Localizator'; | ||
Localizator.addTranslation('kr', { | ||
"Hello": "여보세요" | ||
}); | ||
Localizator.addTranslation('it', { | ||
"Hello": "Ciao" | ||
}); | ||
Localizator.addTranslation('cn', { | ||
"Hello": "你好" | ||
}); | ||
Localizator.setLanguage('cn'); | ||
export default function __ (args) { | ||
return Localizator.localize(args); | ||
} | ||
console.log(__('Hello')); // 你好 | ||
``` |
import {readFileSync, writeFileSync, readdirSync} from 'fs'; | ||
import {parse} from 'path'; | ||
import PO = require("pofile"); | ||
import IOutputFormat from "./Formats/IOutputFormat"; | ||
export default class PofileTransformer { | ||
private outputFormat: IOutputFormat; | ||
constructor(outputFormat: IOutputFormat) { | ||
this.outputFormat = outputFormat; | ||
} | ||
public transformDirectory(path: string) | ||
@@ -13,13 +19,16 @@ { | ||
}).forEach(file => { | ||
this.createJson(path+'/'+file); | ||
this.createOutput(path+'/'+file); | ||
}); | ||
} | ||
private createJson(file: string): void { | ||
let data = readFileSync(file, "utf-8").toString(); | ||
let jsonObject = this.encodePoStringToJSON(data); | ||
return writeFileSync(file.replace(/.po$/, ".json"), JSON.stringify(jsonObject)); | ||
private createOutput(fileName: string): void | ||
{ | ||
let rawData = readFileSync(fileName, "utf-8").toString(); | ||
let dataObject = this.getStringsFromPoFile(rawData); | ||
let newFileName = fileName.replace(/.po$/, "."+this.outputFormat.getExtension()); | ||
let outputData = this.outputFormat.output(dataObject); | ||
return writeFileSync(newFileName, outputData); | ||
} | ||
private encodePoStringToJSON(data: string): object | ||
private getStringsFromPoFile(data: string): object | ||
{ | ||
@@ -26,0 +35,0 @@ let parsedData = PO.parse(data); |
import {expect} from "chai"; | ||
import PofileTransformer from "../../src/PofileTransformer"; | ||
import {existsSync, readFileSync} from 'fs'; | ||
import JsonOutputFormat from "../../src/Formats/JsonOutputFormat"; | ||
import BrowserJavascriptOutputFormat from "../../src/Formats/JavascriptOutputFormat"; | ||
describe('Tests PofileTransformer.test.ts', () => { | ||
it('Transforms po files to json', () => { | ||
let transformer = new PofileTransformer(); | ||
let transformer = new PofileTransformer(new JsonOutputFormat()); | ||
transformer.transformDirectory('test/translationFiles'); | ||
@@ -12,3 +14,3 @@ expect(existsSync('test/translationFiles/main.json')).to.be.true; | ||
it('Checks that translations strings are okay', () => { | ||
it('Checks that translations strings are okay in json', () => { | ||
let transformedFileContents = readFileSync('test/translationFiles/main.json').toString(); | ||
@@ -19,8 +21,22 @@ let jsonObject = JSON.parse(transformedFileContents); | ||
it('Transforms quotes correctly', () => { | ||
let transformer = new PofileTransformer(); | ||
it('Transforms quotes correctly in json format', () => { | ||
let outputFormat = new JsonOutputFormat(); | ||
let transformer = new PofileTransformer(outputFormat); | ||
let data = readFileSync('test/translationFiles/main.po').toString(); | ||
let jsonObject = transformer['encodePoStringToJSON'](data); | ||
expect(jsonObject.hasOwnProperty('After you press the "Create campaign" button the campaign will be created and sent to the moderation. You can change it anytime')).to.be.true; | ||
let jsonObject = transformer['getStringsFromPoFile'](data); | ||
let transformedString = outputFormat.output(jsonObject); | ||
let decodedObject = JSON.parse(transformedString); | ||
expect(decodedObject.hasOwnProperty('After you press the "Create campaign" button the campaign will be created and sent to the moderation. You can change it anytime')).to.be.true; | ||
}); | ||
it('Exports to browser javascript', () => { | ||
let transformer = new PofileTransformer(new BrowserJavascriptOutputFormat()); | ||
transformer.transformDirectory('test/translationFiles'); | ||
expect(existsSync('test/translationFiles/main.js')).to.be.true; | ||
}); | ||
it('Checks that translations strings are okay in javascript', () => { | ||
let transformedJsContents = require('../translationFiles/main'); | ||
expect(transformedJsContents['API Documentation']).to.equal('Документация API'); | ||
}); | ||
}); |
@@ -15,3 +15,5 @@ { | ||
"src/PofileTransformer.ts", | ||
"src/Localizator.ts" | ||
"src/Localizator.ts", | ||
"src/Formats/JavascriptOutputFormat.ts", | ||
"src/Formats/JsonOutputFormat.ts" | ||
], | ||
@@ -18,0 +20,0 @@ "types": [ |
Sorry, the diff of this file is not supported yet
35878
37
577
65