From cd4a96030f389ed4c00db9470c2e5854e8ecfa5c Mon Sep 17 00:00:00 2001 From: mar Date: Fri, 31 Mar 2017 09:34:56 +0000 Subject: [PATCH 1/3] feat: Support `_` as a default value subKey --- README.md | 20 +++++++++++++++++++- index.js | 7 ++++--- test.js | 16 ++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ca380ba..9af8eea 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ var messages = { 1: '{n} Hit', 2: '{n} Hitse', //some slavic langs have multiple plural forms 3: '{n} Hitses', //some slavic langs have multiple plural forms - n: '{n} Hits', // default + n: '{n} Hits', // default for other numbers }, date: { 1: '{day}. January {year}', @@ -81,6 +81,12 @@ var messages = { label: 'Save', tooltip: 'Save unsaved changes', }, + simpleButton: 'Simple', + + hasDefaultSubkey: { + foo: 'Foo subkey value', + __: 'Default value', + }, 'Prosa Key': 'This is prosa!', @@ -109,6 +115,16 @@ t('likeTwoThings', ['Alice', 'Bob']) => 'I like Alice and Bob!' t('saveButton', 'label') => 'Save' t('saveButton', 'tooltip') => 'Save unsaved changes' +//simple translations ignore subkeys +t('simpleButton', 'label') => 'Simple' +t('simpleButton', 'tooltip') => 'Simple' + +//default '__' subkey +t('hasDefaultSubkey', 'foo') => 'Foo subkey value' +t('hasDefaultSubkey', 'missing') => 'Default value' +t('hasDefaultSubkey') => 'Default value' + + //numerical subkeys (count) t('simpleCounter', 25) => 'The count is 25' t('hits', 0) => 'No Hits' @@ -143,6 +159,8 @@ var t2 = function () { ``` + + ### Pluralization You can also do customized pluralization selection, like this: diff --git a/index.js b/index.js index 61680e7..3b01703 100644 --- a/index.js +++ b/index.js @@ -74,7 +74,8 @@ function translatejs(messageObject, options) { function tFunc(translationKey, subKey, replacements) { let translation = tFunc.keys[translationKey] - const complex = subKey != null || replacements != null + const translationIsObject = isObject(translation); + const complex = translationIsObject || subKey != null || replacements != null if (complex) { if (isObject(subKey)) { @@ -84,8 +85,8 @@ function translatejs(messageObject, options) { } replacements = replacements || {} - if (subKey !== null && isObject(translation)) { - const propValue = translation[subKey] + if (translationIsObject) { + const propValue = (subKey != null && translation[subKey]) || translation.__ if (propValue != null) { translation = propValue } else if (typeof subKey === 'number') { diff --git a/test.js b/test.js index cb8b4f4..e845241 100644 --- a/test.js +++ b/test.js @@ -38,12 +38,16 @@ describe('translate.js', function() { 10: '{day}. October {year}', 11: '{day}. November {year}', 12: '{day}. December {year}', + + __: 'WAT! {n}!?', + n: 'Is always overridden by __', }, 'Prosa Key': 'This is prosa!', comboCounter: '{name} is {n} years old.', translationWithSubkeys: { foo: 'FOO' }, + translationWithDefaultSubkey: { __: 'I am a default value' }, } var t = translate(translationsObject) @@ -193,6 +197,14 @@ describe('translate.js', function() { 'I like this.' ) }) + it('should return the "__" subkey value if no subkey is passed', function () { + expect(t3b('translationWithDefaultSubkey')).to.equal('I am a default value') + }) + it('should retry the "__" subkey value if passed subkey is missing', function () { + expect(t3b('translationWithDefaultSubkey', 'nonexistentsubkey') ).to.equal('I am a default value') + expect(t3b('date', 13, {day: '13', year: 2013}) ).to.equal('WAT! 13!?') + }) + // wrong arguments var t4 = translate(translationsObject, 'asd') @@ -310,6 +322,8 @@ describe('translate.js', function() { }) }) + + describe('Return array option', function() { it('should return replacement-token translations as Arrays, when t.arr() is called', function() { var t = translate({ @@ -346,6 +360,8 @@ describe('Return array option', function() { }) }) + + describe('alias usage', function() { it('should work with simple translations', function() { expect( From c195b05671e58ccca4ef528e1f7039a4b53c75a5 Mon Sep 17 00:00:00 2001 From: mar Date: Thu, 14 Sep 2017 12:52:11 +0000 Subject: [PATCH 2/3] feat: Make "*" the default subkey, instead of "__" --- README.md | 4 ++-- index.js | 2 +- test.js | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 9af8eea..015bc09 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ var messages = { hasDefaultSubkey: { foo: 'Foo subkey value', - __: 'Default value', + '*': 'Default value', }, 'Prosa Key': 'This is prosa!', @@ -119,7 +119,7 @@ t('saveButton', 'tooltip') => 'Save unsaved changes' t('simpleButton', 'label') => 'Simple' t('simpleButton', 'tooltip') => 'Simple' -//default '__' subkey +//default '*' subkey t('hasDefaultSubkey', 'foo') => 'Foo subkey value' t('hasDefaultSubkey', 'missing') => 'Default value' t('hasDefaultSubkey') => 'Default value' diff --git a/index.js b/index.js index 3b01703..803e47e 100644 --- a/index.js +++ b/index.js @@ -86,7 +86,7 @@ function translatejs(messageObject, options) { replacements = replacements || {} if (translationIsObject) { - const propValue = (subKey != null && translation[subKey]) || translation.__ + const propValue = (subKey != null && translation[subKey]) || translation['*'] if (propValue != null) { translation = propValue } else if (typeof subKey === 'number') { diff --git a/test.js b/test.js index e845241..9f142cc 100644 --- a/test.js +++ b/test.js @@ -39,15 +39,15 @@ describe('translate.js', function() { 11: '{day}. November {year}', 12: '{day}. December {year}', - __: 'WAT! {n}!?', - n: 'Is always overridden by __', + '*': 'WAT! {n}!?', + n: 'Is always overridden by "*"', }, 'Prosa Key': 'This is prosa!', comboCounter: '{name} is {n} years old.', translationWithSubkeys: { foo: 'FOO' }, - translationWithDefaultSubkey: { __: 'I am a default value' }, + translationWithDefaultSubkey: { '*': 'I am a default value' }, } var t = translate(translationsObject) @@ -197,10 +197,10 @@ describe('translate.js', function() { 'I like this.' ) }) - it('should return the "__" subkey value if no subkey is passed', function () { + it('should return the "*" subkey value if no subkey is passed', function () { expect(t3b('translationWithDefaultSubkey')).to.equal('I am a default value') }) - it('should retry the "__" subkey value if passed subkey is missing', function () { + it('should retry the "*" subkey value if passed subkey is missing', function () { expect(t3b('translationWithDefaultSubkey', 'nonexistentsubkey') ).to.equal('I am a default value') expect(t3b('date', 13, {day: '13', year: 2013}) ).to.equal('WAT! 13!?') }) From 10d3305848e6d706f48638964aaa12183fe864e8 Mon Sep 17 00:00:00 2001 From: mar Date: Fri, 4 May 2018 17:10:55 +0000 Subject: [PATCH 3/3] style: Prettier linting --- index.js | 8 +++++--- test.js | 15 ++++++--------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 803e47e..1103c26 100644 --- a/index.js +++ b/index.js @@ -74,8 +74,9 @@ function translatejs(messageObject, options) { function tFunc(translationKey, subKey, replacements) { let translation = tFunc.keys[translationKey] - const translationIsObject = isObject(translation); - const complex = translationIsObject || subKey != null || replacements != null + const translationIsObject = isObject(translation) + const complex = + translationIsObject || subKey != null || replacements != null if (complex) { if (isObject(subKey)) { @@ -86,7 +87,8 @@ function translatejs(messageObject, options) { replacements = replacements || {} if (translationIsObject) { - const propValue = (subKey != null && translation[subKey]) || translation['*'] + const propValue = + (subKey != null && translation[subKey]) || translation['*'] if (propValue != null) { translation = propValue } else if (typeof subKey === 'number') { diff --git a/test.js b/test.js index 9f142cc..7cea859 100644 --- a/test.js +++ b/test.js @@ -197,15 +197,16 @@ describe('translate.js', function() { 'I like this.' ) }) - it('should return the "*" subkey value if no subkey is passed', function () { + it('should return the "*" subkey value if no subkey is passed', function() { expect(t3b('translationWithDefaultSubkey')).to.equal('I am a default value') }) - it('should retry the "*" subkey value if passed subkey is missing', function () { - expect(t3b('translationWithDefaultSubkey', 'nonexistentsubkey') ).to.equal('I am a default value') - expect(t3b('date', 13, {day: '13', year: 2013}) ).to.equal('WAT! 13!?') + it('should retry the "*" subkey value if passed subkey is missing', function() { + expect(t3b('translationWithDefaultSubkey', 'nonexistentsubkey')).to.equal( + 'I am a default value' + ) + expect(t3b('date', 13, { day: '13', year: 2013 })).to.equal('WAT! 13!?') }) - // wrong arguments var t4 = translate(translationsObject, 'asd') it('should return a translated string with the correct plural form and replaced placeholders: t(key, count, replacements) [wrong optio arg]', function() { @@ -322,8 +323,6 @@ describe('translate.js', function() { }) }) - - describe('Return array option', function() { it('should return replacement-token translations as Arrays, when t.arr() is called', function() { var t = translate({ @@ -360,8 +359,6 @@ describe('Return array option', function() { }) }) - - describe('alias usage', function() { it('should work with simple translations', function() { expect(