Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: default subKeys #37

Merged
merged 3 commits into from
May 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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