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

Adds nested namespace support #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
39 changes: 25 additions & 14 deletions build/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
* translationKey: 'value123'
* }
* }
*
*
* var options = {
* // These are the defaults:
* debug: false, //[Boolean]: Logs missing translations to console and adds @@-markers around output.
* namespaceSplitter: '::' //[String|RegExp]: You can customize the part which splits namespace and translationKeys.
* }
*
*
* var t = libTranslate.getTranslationFunction(messages, [options])
*
*
* t('translationKey')
* t('translationKey', count)
* t('translationKey', {replaceKey: 'replacevalue'})
Expand All @@ -39,6 +39,23 @@
var isObject = function(obj) { return typeof obj === 'object' && obj !== null; };
var isString = function(obj) { return Object.prototype.toString.call(obj) === '[object String]'; };

var deepAccessObject = function(object, keys) {
if (!object) {
return;
}

// copy the array as we operate on it directly, later
keys = keys.slice(0);

var key = keys.shift();
object = object[key];

if (keys.length > 0) {
return deepAccessObject(object, keys);
}
return object;
};

window.libTranslate = {
getTranslationFunction: function(messageObject, options) {
options = isObject(options) ? options : {};
Expand All @@ -47,19 +64,13 @@
var namespaceSplitter = options.namespaceSplitter || '::';

function getTranslationValue(translationKey) {
if(messageObject[translationKey]) {
return messageObject[translationKey];
}
var components = translationKey.split(namespaceSplitter);
var result = deepAccessObject(messageObject, components);

var components = translationKey.split(namespaceSplitter); //@todo make this more robust. maybe support more levels?
var namespace = components[0];
var key = components[1];

if(messageObject[namespace] && messageObject[namespace][key]) {
return messageObject[namespace][key];
if (!result) {
return null;
}

return null;
return result;
}

function getPluralValue(translation, count) {
Expand Down
6 changes: 3 additions & 3 deletions build/translate.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"devDependencies": {
"grunt": "~0.4.4",
"grunt-contrib-jasmine": "~0.6.0",
"grunt-contrib-jasmine": "~1.0.0",
"grunt-contrib-jshint": "~0.8.0",
"grunt-contrib-uglify": "~0.4.0",
"grunt-contrib-watch": "~0.5.3",
Expand Down
Loading