Skip to content

Commit

Permalink
Drop namespace support - fixes #14
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanHoyer committed Dec 18, 2015
1 parent 3e6e226 commit acf1e1d
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 212 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,30 @@ var t = translate( messages_IS, { pluralize:pluralize_IS });
```

Here's a large list of [pluralization algorithms by language](http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html?id=l10n/pluralforms).

## namespace-Support

Namespace support was dropped in version 0.3.0 since it can easily be
accomplished without

With namespace support (old):
```
messages = {
namespaceA: {
foo: 'bar'
}
}
t('namespaceA::foo');
```

Without namespace support (new):
```
messages = {
'namespaceA::foo': 'bar'
}
t('namespaceA::foo');
```

If you really need it, use version 0.2.2.
36 changes: 6 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* var options = {
* // These are the defaults:
* debug: false, //[Boolean]: Logs missing translations to console and add "@@" around output if `true`.
* namespaceSplitter: '::', //[String|RegExp]: Customizes the translationKey namespace splitter.
* pluralize: function(n,translKey){ return Math.abs(n); } //[Function(count,translationKey)]: Provides a custom pluralization mapping function.
* }
*
Expand Down Expand Up @@ -44,42 +43,19 @@

var debug = options && options.debug;

function getTranslationValue(translationKey) {
var transValue = tFunc.keys[translationKey];
if( transValue == null ) {
var path = translationKey.split( (tFunc.opts && tFunc.opts.namespaceSplitter) || '::' );
var i = 0;
var len = path.length;
if ( len > 1 )
{
// Start walking
transValue = tFunc.keys;
while ( len > i ) {
transValue = transValue[ path[i] || '' ];
if ( transValue == null ) {
break;
}
i++;
}
}
}
return transValue;
}


function getPluralValue(translation, count) {
// Opinionated assumption: Pluralization rules are the same for negative and positive values.
// By normalizing all values to positive, pluralization functions become simpler, and less error-prone by accident.
var mappedCount = Math.abs(count);

if(translation[mappedCount] != null){
if(translation[mappedCount] !== undefined){
translation = translation[mappedCount];
} else {
var plFunc = (tFunc.opts||{}).pluralize;
mappedCount = plFunc ? plFunc( mappedCount, translation ) : mappedCount;
if(translation[mappedCount] != null){
if(translation[mappedCount] !== undefined){
translation = translation[mappedCount];
} else if(translation.n != null) {
} else if(translation.n !== undefined) {
translation = translation.n;
} else {
debug && console.warn('No plural forms found for "' + count + '" in', translation);
Expand Down Expand Up @@ -141,8 +117,8 @@
}

var tFunc = function (translationKey, count, replacements) {
var translation = getTranslationValue(translationKey);
var complex = count!==undefined || replacements!==undefined;
var translation = tFunc.keys[translationKey];
var complex = count !== undefined || replacements !== undefined;

if ( complex )
{
Expand All @@ -154,7 +130,7 @@
replacements = replacements || {};
count = typeof count === 'number' ? count : null;

if ( count != null && isObject(translation) ) {
if ( count !== null && isObject(translation) ) {
//get appropriate plural translation string
translation = getPluralValue(translation, count);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "translate.js",
"version": "0.2.3",
"version": "0.3.0",
"description": "Javascript micro library for translations (i18n) with support for placeholders and multiple plural forms.",
"devDependencies": {
"benchmark": "^1.0.0",
Expand Down
Loading

0 comments on commit acf1e1d

Please sign in to comment.