translation-adapter-ts
Advanced tools
Comparing version 1.0.2 to 1.0.3
{ | ||
"name": "translation-adapter-ts", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "The translation adapter for i18n_mrg tool support in TypeScript projects.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -26,2 +26,9 @@ # How to install | ||
If you use babel, with TS resolveJsonModule compiler directive, you may encounter an issue with data structure being exported as module with no default export. There's a workaround to fix that: | ||
```typescript | ||
import * as data from "translation-adapter-ts/test/translationFiles/test.json"; | ||
//@ts-ignore | ||
Localizator.addTranslation(LanguageEnum.RU, data.default); | ||
``` | ||
4. Wrap translations into __() function: | ||
@@ -28,0 +35,0 @@ ```typescript |
@@ -1,3 +0,3 @@ | ||
export interface ITranslation { | ||
export default interface ITranslation { | ||
[key: string]: string; | ||
} |
import {LanguageEnum} from "./Enums/LanguageEnum"; | ||
import {ITranslation} from "./ITranslation"; | ||
import ITranslation from "./ITranslation"; | ||
export class Localizator { | ||
export default class Localizator { | ||
private static _language: LanguageEnum = LanguageEnum.RU; | ||
@@ -96,3 +96,5 @@ private static _defaultLanguage: LanguageEnum = LanguageEnum.EN; | ||
args[0] = args[0].replace(regex, "$" + form, this.translate((args[0]))); | ||
let translation = this.translate(args[0]); | ||
let forms = translation.split(regex); | ||
args[0] = forms[form] === undefined ? translation : translation.replace(regex, forms[form]); | ||
} else { | ||
@@ -99,0 +101,0 @@ args[0] = this.translate((args[0])); |
@@ -1,5 +0,5 @@ | ||
import {Localizator} from "./Localizator"; | ||
import Localizator from "./Localizator"; | ||
export function __(...args: any[]) { | ||
export default function __(...args: any[]) { | ||
return Localizator.localize(...args); | ||
} |
import {readFileSync, writeFileSync, readdirSync} from 'fs'; | ||
import {parse} from 'path'; | ||
export class PofileTransformer { | ||
export default class PofileTransformer { | ||
@@ -6,0 +6,0 @@ public transformDirectory(path: string) |
import {expect} from "chai"; | ||
import {Localizator} from "../../src/Localizator"; | ||
import Localizator from "../../src/Localizator"; | ||
import {LanguageEnum} from "../../src/Enums/LanguageEnum"; | ||
import {ITranslation} from "../../src/ITranslation"; | ||
import ITranslation from "../../src/ITranslation"; | ||
import * as data from "./../translationFiles/test.json"; | ||
@@ -84,2 +84,42 @@ | ||
}); | ||
it('Tests tests word forms in Russian', () => { | ||
Localizator.addTranslation(LanguageEnum.RU, data); | ||
Localizator.setLanguage(LanguageEnum.RU); | ||
expect(Localizator.localize('You have %count% products in the shopping cart', { | ||
count: 1, | ||
})).to.be.equal('У вас 1 товар в корзине'); | ||
expect(Localizator.localize('You have %count% products in the shopping cart', { | ||
count: 2, | ||
})).to.be.equal('У вас 2 товара в корзине'); | ||
expect(Localizator.localize('You have %count% products in the shopping cart', { | ||
count: 3, | ||
})).to.be.equal('У вас 3 товара в корзине'); | ||
expect(Localizator.localize('You have %count% products in the shopping cart', { | ||
count: 4, | ||
})).to.be.equal('У вас 4 товара в корзине'); | ||
expect(Localizator.localize('You have %count% products in the shopping cart', { | ||
count: 5, | ||
})).to.be.equal('У вас 5 товаров в корзине'); | ||
expect(Localizator.localize('You have %count% products in the shopping cart', { | ||
count: 25, | ||
})).to.be.equal('У вас 25 товаров в корзине'); | ||
expect(Localizator.localize('You have %count% products in the shopping cart', { | ||
count: 104, | ||
})).to.be.equal('У вас 104 товара в корзине'); | ||
}); | ||
it('Tests non-existing word forms in Russian', () => { | ||
Localizator.addTranslation(LanguageEnum.RU, data); | ||
Localizator.setLanguage(LanguageEnum.RU); | ||
expect(Localizator.localize('You have %count% new emails', { | ||
count: 2, | ||
})).to.be.equal('У вас 2 новых писем'); | ||
}); | ||
}); |
import {expect} from "chai"; | ||
import {ITranslation} from "../../src/ITranslation"; | ||
import {Localizator} from "../../src/Localizator"; | ||
import ITranslation from "../../src/ITranslation"; | ||
import Localizator from "../../src/Localizator"; | ||
import {LanguageEnum} from "../../src/Enums/LanguageEnum"; | ||
import {__} from "../../src/Main"; | ||
import __ from "../../src/Main"; | ||
@@ -7,0 +7,0 @@ describe('Tests Main.ts', () => { |
import {expect} from "chai"; | ||
import {PofileTransformer} from "../../src/PofileTransformer"; | ||
import PofileTransformer from "../../src/PofileTransformer"; | ||
import {existsSync} from 'fs'; | ||
@@ -4,0 +4,0 @@ |
@@ -1,1 +0,1 @@ | ||
{"Test translation":"Тестовый перевод"} | ||
{"Test translation":"Тестовый перевод", "You have %count% products in the shopping cart": "У вас %count% {товар|товара|товаров} в корзине", "You have %count% new emails": "У вас %count% новых писем"} |
24930
434
41
22