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

drop ns support - fixes #14 #15

Merged
merged 4 commits into from
Dec 18, 2015
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
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
language: node_js
sudo: false
node_js:
- 0.10
- 5.2
- 4.1
19 changes: 8 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ Usage:
var translate = require('translate.js');

var messages = {
translationKey: 'translationValue'
translationKey: 'Translation value'
};

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 All @@ -40,7 +39,6 @@ t('translationKey', count);
t('translationKey', {replaceKey: 'replacevalue'});
t('translationKey', count, {replaceKey: 'replacevalue'});
t('translationKey', {replaceKey: 'replacevalue'}, count);
t('moduleA::translationKey');

```

Expand Down Expand Up @@ -79,9 +77,6 @@ var messages = {

'Prosa Key': 'This is prosa!',

namespaceA: {
like: 'I like this namespace.',
}
}
```

Expand All @@ -97,9 +92,6 @@ And use it like this:
t('like') => 'I like this.'
t('Prosa Key') => 'This is prosa!'

//namespace support
t('namespaceA::like') => 'I like this namespace.'

//palceholders - named
t('likeThing', {thing: 'the Sun'}) => 'I like the Sun!'
//palceholders - array
Expand Down Expand Up @@ -131,7 +123,6 @@ t('like') => 'like' (No longer translated)
The translation options can similarily be changed or replaced via the `.opts` property.

```js
t.opts.namespaceSplitter = '/';
t.keys.foo = { bar:'baz' }
t('foo/bar'); => 'baz'
```
Expand Down Expand Up @@ -160,7 +151,7 @@ var pluralize_IS = function ( n, tarnslationKey ) {
// Icelandic rules: Numbers ending in 1 are singular - unless ending in 11.
return (n%10 !== 1 || n%100 === 11) ? 'p' : 's';
};
var t = translate( messages_IS, { pluralize:pluralize_IS });
var t = translate( messages_IS, { pluralize: pluralize_IS });

t('sheep', 0) => 'Engar kindur'
t('sheep', 1) => '1 kind'
Expand All @@ -177,3 +168,9 @@ 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. If you need it, use [version 0.2.2](https://github.com/StephanHoyer/translate.js/tree/v0.2.2).
39 changes: 7 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Microlib for translations with support for placeholders and multiple plural forms.
*
* v0.2.2
* v0.3.0
*
* Usage:
* var messages = {
Expand All @@ -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 All @@ -25,7 +24,6 @@
* t('translationKey', {replaceKey: 'replacevalue'})
* t('translationKey', count, {replaceKey: 'replacevalue'})
* t('translationKey', {replaceKey: 'replacevalue'}, count)
* t('moduleA::translationKey')
*
*
* @author Jonas Girnatis <dermusterknabe@gmail.com>
Expand All @@ -44,42 +42,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 +116,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 +129,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