Skip to content

Commit

Permalink
Merge pull request #37 from StephanHoyer/feature/default_subkeys
Browse files Browse the repository at this point in the history
Feature: default subKeys
  • Loading branch information
maranomynet committed May 4, 2018
2 parents d2835d0 + 10d3305 commit 10f7840
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}',
Expand All @@ -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!',

Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -143,6 +159,8 @@ var t2 = function () {
```




### Pluralization

You can also do customized pluralization selection, like this:
Expand Down
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ 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)) {
Expand All @@ -84,8 +86,9 @@ 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') {
Expand Down
13 changes: 13 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -193,6 +197,15 @@ 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')
Expand Down

0 comments on commit 10f7840

Please sign in to comment.