Skip to content

Commit

Permalink
Merge pull request #863 from aray12/master
Browse files Browse the repository at this point in the history
Return namespace in cimode with appendNamespaceToCIMode option
  • Loading branch information
jamuhl committed Jan 21, 2017
2 parents f888eb7 + a7b4380 commit 6aa81cd
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/Translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,25 @@ class Translator extends EventEmitter {
if (typeof keys === 'number') keys = String(keys);
if (typeof keys === 'string') keys = [keys];

// return key on CIMode
let lng = options.lng || this.language;
if (lng && lng.toLowerCase() === 'cimode') return keys[keys.length - 1];

// separators
let keySeparator = options.keySeparator || this.options.keySeparator || '.';

// get namespace(s)
let { key, namespaces } = this.extractFromKey(keys[keys.length - 1], options);
let namespace = namespaces[namespaces.length - 1];

// return key on CIMode
let lng = options.lng || this.language;
let appendNamespaceToCIMode = options.appendNamespaceToCIMode || this.options.appendNamespaceToCIMode;
if (lng && lng.toLowerCase() === 'cimode') {
if (appendNamespaceToCIMode) {
let nsSeparator = options.nsSeparator || this.options.nsSeparator
return namespace + nsSeparator + key;
}

return key;
}

// resolve from store
let res = this.resolve(keys, options);

Expand Down Expand Up @@ -96,7 +104,7 @@ class Translator extends EventEmitter {
// string, empty or null
else {
let usedDefault = false,
usedKey = false;
usedKey = false;

// fallback value
if (!this.isValidLookup(res) && options.defaultValue !== undefined) {
Expand Down Expand Up @@ -167,9 +175,9 @@ class Translator extends EventEmitter {
let postProcessorNames = typeof postProcess === 'string' ? [postProcess] : postProcess;

if (res !== undefined &&
postProcessorNames &&
postProcessorNames.length &&
options.applyPostProcessor !== false) {
postProcessorNames &&
postProcessorNames.length &&
options.applyPostProcessor !== false) {
res = postProcessor.handle(postProcessorNames, res, key, options, this);
}

Expand Down Expand Up @@ -229,8 +237,8 @@ class Translator extends EventEmitter {

isValidLookup(res) {
return res !== undefined &&
!(!this.options.returnNull && res === null) &&
!(!this.options.returnEmptyString && res === '');
!(!this.options.returnNull && res === null) &&
!(!this.options.returnEmptyString && res === '');
}

getResource(code, ns, key, options = {}) {
Expand Down
1 change: 1 addition & 0 deletions src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function get() {
returnedObjectHandler: function() {}, // function(key, value, options) triggered if key returns object but returnObjects is set to false
parseMissingKeyHandler: false, // function(key) parsed a key that was not found in t() before returning
appendNamespaceToMissingKey: false,
appendNamespaceToCIMode: false,
overloadTranslationOptionHandler: function(args) {
return { defaultValue: args[1] };
},
Expand Down
55 changes: 55 additions & 0 deletions test/translator/translator.cimode.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Translator from '../../src/Translator';
import ResourceStore from '../../src/ResourceStore.js';
import LanguageUtils from '../../src/LanguageUtils';
import PluralResolver from '../../src/PluralResolver';
import Interpolator from '../../src/Interpolator';

describe('Translator', () => {

describe('translate() in cimode', () => {
var t;

before(() => {
const rs = new ResourceStore({
en: {
translation: {
test: 'test_en'
}
},
de: {
translation: {
test: 'test_de'
}
}
});
const lu = new LanguageUtils({ fallbackLng: 'en' });
t = new Translator({
resourceStore: rs,
languageUtils: lu,
pluralResolver: new PluralResolver(lu, {prepend: '_'}),
interpolator: new Interpolator()
}, {
interpolation: {
interpolateResult: true,
interpolateDefaultValue: true,
interpolateKey: true
}
});
t.changeLanguage('cimode');
});

var tests = [
{args: ['translation:test', { appendNamespaceToCIMode: false, ns: 'translation', nsSeparator: ':' }], expected: 'test'},
{args: ['test', { appendNamespaceToCIMode: false, ns: 'translation', nsSeparator: ':' }], expected: 'test'},
{args: ['translation:test', { appendNamespaceToCIMode: true, ns: 'translation', nsSeparator: ':' }], expected: 'translation:test'},
{args: ['test', { appendNamespaceToCIMode: true, ns: 'translation', nsSeparator: ':' }], expected: 'translation:test'},
];

tests.forEach((test) => {
it('correctly return key for ' + JSON.stringify(test.args) + ' args in cimode', () => {
expect(t.translate.apply(t, test.args)).to.eql(test.expected);
});
});
});

});

0 comments on commit 6aa81cd

Please sign in to comment.