i18n-dialect
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -30,3 +30,4 @@ "use strict"; | ||
msgid: plurals[0], | ||
msgidPlural: plurals[1], | ||
msgidPlural: plurals[plurals.length - 1], | ||
allPlurals: plurals, | ||
msgstr: [], | ||
@@ -42,3 +43,4 @@ substitutions: substitutions | ||
msgid: plurals[0], | ||
msgidPlural: plurals[1], | ||
msgidPlural: plurals[plurals.length - 1], | ||
allPlurals: plurals, | ||
msgstr: [], | ||
@@ -49,2 +51,46 @@ msgctxt: context, | ||
}; }; | ||
exports._ggGen = function (ctrl) { return function (str, substitutions) { | ||
if (substitutions === void 0) { substitutions = []; } | ||
return ctrl.getString({ | ||
type: '_t', | ||
msgid: str, | ||
msgstr: '', | ||
substitutions: substitutions | ||
}, /* forceUntranslated = */ true); | ||
}; }; | ||
exports._pggGen = function (ctrl) { return function (context, str, substitutions) { | ||
if (substitutions === void 0) { substitutions = []; } | ||
return ctrl.getString({ | ||
type: '_pt', | ||
msgid: str, | ||
msgstr: '', | ||
msgctxt: context, | ||
substitutions: substitutions | ||
}, /* forceUntranslated = */ true); | ||
}; }; | ||
exports._nggGen = function (ctrl) { return function (plurals, factor, substitutions) { | ||
if (substitutions === void 0) { substitutions = []; } | ||
return ctrl.getString({ | ||
type: '_nt', | ||
factor: factor, | ||
msgid: plurals[0], | ||
msgidPlural: plurals[plurals.length - 1], | ||
allPlurals: plurals, | ||
msgstr: [], | ||
substitutions: substitutions | ||
}, /* forceUntranslated = */ true); | ||
}; }; | ||
exports._npggGen = function (ctrl) { return function (context, plurals, factor, substitutions) { | ||
if (substitutions === void 0) { substitutions = []; } | ||
return ctrl.getString({ | ||
type: '_npt', | ||
factor: factor, | ||
msgid: plurals[0], | ||
msgidPlural: plurals[plurals.length - 1], | ||
allPlurals: plurals, | ||
msgstr: [], | ||
msgctxt: context, | ||
substitutions: substitutions | ||
}, /* forceUntranslated = */ true); | ||
}; }; | ||
var TranslationProvider = (function () { | ||
@@ -57,2 +103,6 @@ function TranslationProvider(ctrl) { | ||
this._npt = exports._nptGen(this.ctrl); | ||
this._gg = exports._ggGen(this.ctrl); | ||
this._pgg = exports._pggGen(this.ctrl); | ||
this._ngg = exports._nggGen(this.ctrl); | ||
this._npgg = exports._npggGen(this.ctrl); | ||
} | ||
@@ -59,0 +109,0 @@ return TranslationProvider; |
@@ -10,4 +10,5 @@ "use strict"; | ||
} | ||
TranslationController.prototype.getString = function (descriptor) { | ||
var key = this.getDictKeyForDescriptor(descriptor); | ||
TranslationController.prototype.getString = function (descriptor, forceUntranslated) { | ||
if (forceUntranslated === void 0) { forceUntranslated = false; } | ||
var key = forceUntranslated ? undefined : this.getDictKeyForDescriptor(descriptor); | ||
var translationForms = key && this.dictionary[key] || this.getUntranslatedFallback(descriptor); | ||
@@ -79,3 +80,3 @@ var translation = this.selectPluralForm(translationForms, descriptor); | ||
case '_npt': | ||
return [descriptor.msgid, descriptor.msgidPlural]; | ||
return descriptor.allPlurals; | ||
} | ||
@@ -82,0 +83,0 @@ }; |
58
index.ts
@@ -37,3 +37,4 @@ import { | ||
msgid: plurals[0], | ||
msgidPlural: plurals[1], | ||
msgidPlural: plurals[plurals.length - 1], | ||
allPlurals: plurals, | ||
msgstr: [], // just for type conformity | ||
@@ -50,3 +51,4 @@ substitutions | ||
msgid: plurals[0], | ||
msgidPlural: plurals[1], | ||
msgidPlural: plurals[plurals.length - 1], | ||
allPlurals: plurals, | ||
msgstr: [], // just for type conformity | ||
@@ -58,2 +60,50 @@ msgctxt: context, | ||
export const _ggGen: (ctrl: TranslationController) => SimpleTranslation = | ||
(ctrl) => (str, substitutions = []): string => { | ||
return ctrl.getString({ | ||
type: '_t', | ||
msgid: str, | ||
msgstr: '', // just for type conformity | ||
substitutions | ||
}, /* forceUntranslated = */ true); | ||
}; | ||
export const _pggGen: (ctrl: TranslationController) => ContextualTranslation = | ||
(ctrl) => (context, str, substitutions = []): string => { | ||
return ctrl.getString({ | ||
type: '_pt', | ||
msgid: str, | ||
msgstr: '', // just for type conformity | ||
msgctxt: context, | ||
substitutions | ||
}, /* forceUntranslated = */ true); | ||
}; | ||
export const _nggGen: (ctrl: TranslationController) => PluralTranslation = | ||
(ctrl) => (plurals, factor, substitutions = []): string => { | ||
return ctrl.getString({ | ||
type: '_nt', | ||
factor, | ||
msgid: plurals[0], | ||
msgidPlural: plurals[plurals.length - 1], | ||
allPlurals: plurals, | ||
msgstr: [], // just for type conformity | ||
substitutions | ||
}, /* forceUntranslated = */ true); | ||
}; | ||
export const _npggGen: (ctrl: TranslationController) => PluralContextualTranslation = | ||
(ctrl) => (context, plurals, factor, substitutions = []): string => { | ||
return ctrl.getString({ | ||
type: '_npt', | ||
factor, | ||
msgid: plurals[0], | ||
msgidPlural: plurals[plurals.length - 1], | ||
allPlurals: plurals, | ||
msgstr: [], // just for type conformity | ||
msgctxt: context, | ||
substitutions | ||
}, /* forceUntranslated = */ true); | ||
}; | ||
export class TranslationProvider { | ||
@@ -65,2 +115,6 @@ constructor(private ctrl: TranslationController) { } | ||
public _npt: PluralContextualTranslation = _nptGen(this.ctrl); | ||
public _gg: SimpleTranslation = _ggGen(this.ctrl); | ||
public _pgg: ContextualTranslation = _pggGen(this.ctrl); | ||
public _ngg: PluralTranslation = _nggGen(this.ctrl); | ||
public _npgg: PluralContextualTranslation = _npggGen(this.ctrl); | ||
} |
{ | ||
"name": "i18n-dialect", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Internationalization support library", | ||
@@ -43,3 +43,6 @@ "main": "dist/index.js", | ||
"watchify": "^3.9.0" | ||
}, | ||
"dependencies": { | ||
"@types/assert": "^0.0.31" | ||
} | ||
} |
@@ -18,4 +18,4 @@ import { | ||
public getString(descriptor: Descriptor): string { | ||
let key: string | undefined = this.getDictKeyForDescriptor(descriptor); | ||
public getString(descriptor: Descriptor, forceUntranslated: boolean = false): string { | ||
let key: string | undefined = forceUntranslated ? undefined : this.getDictKeyForDescriptor(descriptor); | ||
let translationForms: string[] = key && this.dictionary[key] || this.getUntranslatedFallback(descriptor); | ||
@@ -96,3 +96,3 @@ let translation = this.selectPluralForm(translationForms, descriptor); | ||
case '_npt': | ||
return [descriptor.msgid, descriptor.msgidPlural]; | ||
return descriptor.allPlurals; | ||
} | ||
@@ -99,0 +99,0 @@ } |
@@ -29,2 +29,3 @@ export { | ||
msgidPlural: string, | ||
allPlurals: string[], // for untranslated fallbacks | ||
msgstr: string[], | ||
@@ -39,2 +40,3 @@ substitutions: Scalar[] | ||
msgidPlural: string, | ||
allPlurals: string[], // for untranslated fallbacks | ||
msgstr: string[], | ||
@@ -41,0 +43,0 @@ msgctxt: string, |
@@ -38,3 +38,3 @@ import * as assert from 'assert'; | ||
it('Compatibility: getDictKeyForEntry and getDictKeyForDescriptor values are compatible for _nt', () => { | ||
it('Compatibility: getDictKeyForEntry and getDictKeyForDescriptor values are compatible for _pt', () => { | ||
let t = getController(); | ||
@@ -59,7 +59,7 @@ let entry: SingleI18NEntry = { | ||
it('Compatibility: getDictKeyForEntry and getDictKeyForDescriptor values are compatible for _pt', () => { | ||
it('Compatibility: getDictKeyForEntry and getDictKeyForDescriptor values are compatible for _nt', () => { | ||
let t = getController(); | ||
let entry: PluralI18NEntry = { | ||
type: 'plural', | ||
entry: ['test1', 'test2'], | ||
entry: ['test1', 'test3'], | ||
translations: [] | ||
@@ -71,3 +71,4 @@ }; | ||
msgid: 'test1', | ||
msgidPlural: 'test2', | ||
msgidPlural: 'test3', | ||
allPlurals: ['test1', 'test2', 'test3'], | ||
msgstr: [], | ||
@@ -86,3 +87,3 @@ substitutions: [] | ||
type: 'plural', | ||
entry: ['test1', 'test2'], | ||
entry: ['test1', 'test3'], | ||
translations: [], | ||
@@ -95,3 +96,4 @@ context: 'ctx' | ||
msgid: 'test1', | ||
msgidPlural: 'test2', | ||
msgidPlural: 'test3', | ||
allPlurals: ['test1', 'test2', 'test3'], | ||
msgctxt: 'ctx', | ||
@@ -113,3 +115,4 @@ msgstr: [], | ||
msgid: 'test1', // ignored | ||
msgidPlural: 'test2', // ignored | ||
msgidPlural: 'test3', // ignored | ||
allPlurals: ['test1', 'test2', 'test3'], | ||
msgstr: [], | ||
@@ -235,3 +238,4 @@ substitutions: [] | ||
msgid: 'test1', // ignored | ||
msgidPlural: 'test2', // ignored | ||
msgidPlural: 'test3', // ignored | ||
allPlurals: ['test1', 'test2', 'test3'], | ||
msgstr: [], | ||
@@ -254,3 +258,4 @@ substitutions: [] | ||
msgid: 'test1', // ignored | ||
msgidPlural: 'test2', // ignored | ||
msgidPlural: 'test3', // ignored | ||
allPlurals: ['test1', 'test2', 'test3'], | ||
msgstr: [], | ||
@@ -273,3 +278,4 @@ substitutions: [] | ||
msgid: 'test1', // ignored | ||
msgidPlural: 'test2', // ignored | ||
msgidPlural: 'test3', // ignored | ||
allPlurals: ['test1', 'test2', 'test3'], | ||
msgstr: [], | ||
@@ -434,3 +440,3 @@ substitutions: [ | ||
}, (err: any) => { | ||
assert.empty(err); | ||
assert.equal(err, undefined); | ||
done(); | ||
@@ -453,3 +459,3 @@ }); | ||
}, (err: any) => { | ||
assert.empty(err); | ||
assert.equal(err, undefined); | ||
done(); | ||
@@ -473,3 +479,3 @@ }); | ||
}, (err: any) => { | ||
assert.empty(err); | ||
assert.equal(err, undefined); | ||
done(); | ||
@@ -488,2 +494,3 @@ }); | ||
msgidPlural: '%% ТВ-каналов', | ||
allPlurals: ['%% ТВ-канал', '%% ТВ-канала', '%% ТВ-каналов'], | ||
msgstr: [], | ||
@@ -504,3 +511,3 @@ substitutions: [] | ||
}, (err: any) => { | ||
assert.empty(err); | ||
assert.equal(err, undefined); | ||
done(); | ||
@@ -519,2 +526,3 @@ }); | ||
msgidPlural: '%% мест', | ||
allPlurals: ['%% место', '%% места', '%% мест'], | ||
msgctxt: 'Вместимость парковки', | ||
@@ -536,7 +544,173 @@ msgstr: [], | ||
}, (err: any) => { | ||
assert.empty(err); | ||
assert.equal(err, undefined); | ||
done(); | ||
}); | ||
}); | ||
it('Integration: properly gets fallback string when no dictionary loaded with _t', () => { | ||
let t = getController(); | ||
let descr: Descriptor = { | ||
type: '_t', | ||
msgid: '"%1" не подключает по вашему адресу.', | ||
msgstr: '', | ||
substitutions: ['Provider'] | ||
}; | ||
assert.equal(t.getString(descr), '"Provider" не подключает по вашему адресу.'); | ||
}); | ||
it('Integration: properly gets fallback string when no dictionary loaded with _pt', () => { | ||
let t = getController(); | ||
let descr: Descriptor = { | ||
type: '_pt', | ||
msgid: '%1 км', | ||
msgctxt: 'километры', | ||
msgstr: '', | ||
substitutions: ['23'] | ||
}; | ||
assert.equal(t.getString(descr), '23 км'); | ||
}); | ||
it('Integration: properly gets fallback string when no dictionary loaded with _nt', () => { | ||
let t = getController(); | ||
let descr: Descriptor = { | ||
type: '_nt', | ||
factor: 0, | ||
msgid: '%% ТВ-канал', | ||
msgidPlural: '%% ТВ-каналов', | ||
allPlurals: ['%% ТВ-канал', '%% ТВ-канала', '%% ТВ-каналов'], | ||
msgstr: [], | ||
substitutions: [] | ||
}; | ||
descr.factor = 1; | ||
assert.equal(t.getString(descr), '1 ТВ-канал'); | ||
descr.factor = 3; | ||
assert.equal(t.getString(descr), '3 ТВ-канала'); | ||
descr.factor = 8; | ||
assert.equal(t.getString(descr), '8 ТВ-каналов'); | ||
}); | ||
it('Integration: properly gets fallback string when no dictionary loaded with _npt', () => { | ||
let t = getController(); | ||
let descr: Descriptor = { | ||
type: '_npt', | ||
factor: 0, | ||
msgid: '%% место', | ||
msgidPlural: '%% мест', | ||
allPlurals: ['%% место', '%% места', '%% мест'], | ||
msgctxt: 'Вместимость парковки', | ||
msgstr: [], | ||
substitutions: [] | ||
}; | ||
descr.factor = 1; | ||
assert.equal(t.getString(descr), '1 место'); | ||
descr.factor = 3; | ||
assert.equal(t.getString(descr), '3 места'); | ||
descr.factor = 8; | ||
assert.equal(t.getString(descr), '8 мест'); | ||
}); | ||
it('Integration: properly gets forced untranslated string from loaded dictionary with _t', (done) => { | ||
setTranslationGetter((name: string, onReady: (name: string, contents: string) => void) => onReady(name, testLocaleJson)); | ||
let t = getController(); | ||
t.setLocale('cs_cz', (name: string) => { | ||
let descr: Descriptor = { | ||
type: '_t', | ||
msgid: '"%1" не подключает по вашему адресу.', | ||
msgstr: '', | ||
substitutions: ['Provider'] | ||
}; | ||
assert.equal(t.getString(descr, true), '"Provider" не подключает по вашему адресу.'); | ||
done(); | ||
}, (err: any) => { | ||
assert.equal(err, undefined); | ||
done(); | ||
}); | ||
}); | ||
it('Integration: properly gets forced untranslated string from loaded dictionary with _pt', (done) => { | ||
setTranslationGetter((name: string, onReady: (name: string, contents: string) => void) => onReady(name, testLocaleJson)); | ||
let t = getController(); | ||
t.setLocale('cs_cz', (name: string) => { | ||
let descr: Descriptor = { | ||
type: '_pt', | ||
msgid: '%1 км', | ||
msgctxt: 'километры', | ||
msgstr: '', | ||
substitutions: ['23'] | ||
}; | ||
assert.equal(t.getString(descr, true), '23 км'); | ||
done(); | ||
}, (err: any) => { | ||
assert.equal(err, undefined); | ||
done(); | ||
}); | ||
}); | ||
it('Integration: properly gets forced untranslated string from loaded dictionary with _nt', (done) => { | ||
setTranslationGetter((name: string, onReady: (name: string, contents: string) => void) => onReady(name, testLocaleJson)); | ||
let t = getController(); | ||
t.setLocale('cs_cz', (name: string) => { | ||
let descr: Descriptor = { | ||
type: '_nt', | ||
factor: 0, | ||
msgid: '%% ТВ-канал', | ||
msgidPlural: '%% ТВ-каналов', | ||
allPlurals: ['%% ТВ-канал', '%% ТВ-канала', '%% ТВ-каналов'], | ||
msgstr: [], | ||
substitutions: [] | ||
}; | ||
descr.factor = 1; | ||
assert.equal(t.getString(descr, true), '1 ТВ-канал'); | ||
descr.factor = 3; | ||
assert.equal(t.getString(descr, true), '3 ТВ-канала'); | ||
descr.factor = 8; | ||
assert.equal(t.getString(descr, true), '8 ТВ-каналов'); | ||
done(); | ||
}, (err: any) => { | ||
assert.equal(err, undefined); | ||
done(); | ||
}); | ||
}); | ||
it('Integration: properly gets forced untranslated string from loaded dictionary with _npt', (done) => { | ||
setTranslationGetter((name: string, onReady: (name: string, contents: string) => void) => onReady(name, testLocaleJson)); | ||
let t = getController(); | ||
t.setLocale('cs_cz', (name: string) => { | ||
let descr: Descriptor = { | ||
type: '_npt', | ||
factor: 0, | ||
msgid: '%% место', | ||
msgidPlural: '%% мест', | ||
allPlurals: ['%% место', '%% места', '%% мест'], | ||
msgctxt: 'Вместимость парковки', | ||
msgstr: [], | ||
substitutions: [] | ||
}; | ||
descr.factor = 1; | ||
assert.equal(t.getString(descr, true), '1 место'); | ||
descr.factor = 3; | ||
assert.equal(t.getString(descr, true), '3 места'); | ||
descr.factor = 8; | ||
assert.equal(t.getString(descr, true), '8 мест'); | ||
done(); | ||
}, (err: any) => { | ||
assert.equal(err, undefined); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
194055
1468
1
+ Added@types/assert@^0.0.31
+ Added@types/assert@0.0.31(transitive)