Skip to content

Commit

Permalink
added filters
Browse files Browse the repository at this point in the history
  • Loading branch information
David Kudera committed Feb 3, 2014
1 parent f05e40d commit 5750f85
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 6 deletions.
13 changes: 13 additions & 0 deletions README.md
Expand Up @@ -199,6 +199,19 @@ translator.addReplacement('role', user.getRole());
translator.translate('admin.%role%');
```

## Filters (eg. markdown)

If you want some special formatting in your translations (for example markdown syntax or others) you can add filter
which will automatically transform all successfully translated messages.

```
translator.addFilter(function(message) {
return message.split('').reverse().join('');
});
console.log(translator.translate('homepage.title')); // output is reversed title in homepage dictionary
```

## List of translations

Sometimes you may want to display list of texts but don't want to create translations with these names: item1, item2,
Expand Down
34 changes: 32 additions & 2 deletions lib/Translator.js

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

29 changes: 28 additions & 1 deletion src/Translator.coffee
Expand Up @@ -23,6 +23,8 @@ class Translator

replacements: null

filters: null

data: null

cache: null
Expand All @@ -31,6 +33,7 @@ class Translator
constructor: (pathOrLoader) ->
@plurals = {}
@replacements = {}
@filters = []
@data = {}

if !pathOrLoader
Expand Down Expand Up @@ -119,6 +122,24 @@ class Translator
return @


addFilter: (fn) ->
@filters.push(fn)
return @


applyFilters: (translation) ->
if Object.prototype.toString.call(translation) == '[object Array]'
for t, i in translation
translation[i] = @applyFilters(t)

return translation

for filter in @filters
translation = filter(translation)

return translation


loadCategory: (_path, name, language = @language) ->
categoryName = _path + '/' + name
if typeof @data[categoryName] == 'undefined'
Expand Down Expand Up @@ -191,6 +212,7 @@ class Translator
count = params[1]
args = params[2]
language = @language
found = false

if typeof message != 'string' then return message

Expand All @@ -206,7 +228,7 @@ class Translator
message = match[2]

if language == null
throw new Error 'You have to set language'
throw new Error 'You have to set language.'

num = null
if (match = message.match(/(.+)\[(\d+)\]$/)) != null
Expand All @@ -216,6 +238,8 @@ class Translator
message = @applyReplacements(message, args)
translation = @findTranslation(message, language)

found = @hasTranslation(message, language)

if num != null
if !@isList(translation)
throw new Error 'Translation ' + message + ' is not a list.'
Expand All @@ -230,6 +254,9 @@ class Translator

message = @prepareTranslation(message, args)

if found
message = @applyFilters(message)

return message


Expand Down
34 changes: 32 additions & 2 deletions test/browser/application.js
Expand Up @@ -268,6 +268,8 @@

Translator.prototype.replacements = null;

Translator.prototype.filters = null;

Translator.prototype.data = null;

Translator.prototype.cache = null;
Expand All @@ -276,6 +278,7 @@
var config, data, language, stack, _config;
this.plurals = {};
this.replacements = {};
this.filters = [];
this.data = {};
if (!pathOrLoader) {
throw new Error('You have to set path to base directory or to config file or loader.');
Expand Down Expand Up @@ -373,6 +376,28 @@
return this;
};

Translator.prototype.addFilter = function(fn) {
this.filters.push(fn);
return this;
};

Translator.prototype.applyFilters = function(translation) {
var filter, i, t, _i, _j, _len, _len1, _ref;
if (Object.prototype.toString.call(translation) === '[object Array]') {
for (i = _i = 0, _len = translation.length; _i < _len; i = ++_i) {
t = translation[i];
translation[i] = this.applyFilters(t);
}
return translation;
}
_ref = this.filters;
for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) {
filter = _ref[_j];
translation = filter(translation);
}
return translation;
};

Translator.prototype.loadCategory = function(_path, name, language) {
var categoryName, conds, data, file;
if (language == null) {
Expand Down Expand Up @@ -467,7 +492,7 @@
};

Translator.prototype.translate = function(message, count, args) {
var language, match, num, params, translation;
var found, language, match, num, params, translation;
if (count == null) {
count = null;
}
Expand All @@ -479,6 +504,7 @@
count = params[1];
args = params[2];
language = this.language;
found = false;
if (typeof message !== 'string') {
return message;
}
Expand All @@ -496,7 +522,7 @@
message = match[2];
}
if (language === null) {
throw new Error('You have to set language');
throw new Error('You have to set language.');
}
num = null;
if ((match = message.match(/(.+)\[(\d+)\]$/)) !== null) {
Expand All @@ -505,6 +531,7 @@
}
message = this.applyReplacements(message, args);
translation = this.findTranslation(message, language);
found = this.hasTranslation(message, language);
if (num !== null) {
if (!this.isList(translation)) {
throw new Error('Translation ' + message + ' is not a list.');
Expand All @@ -519,6 +546,9 @@
}
}
message = this.prepareTranslation(message, args);
if (found) {
message = this.applyFilters(message);
}
return message;
};

Expand Down
26 changes: 25 additions & 1 deletion test/node/lib/Translator.js

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

28 changes: 28 additions & 0 deletions test/node/src/Translator.coffee
Expand Up @@ -232,6 +232,34 @@ describe 'Translator', ->
it 'should return original text if text is eclosed in \':\'', ->
expect(translator.translate(':cs|do.not.translate.me:')).to.be.equal('do.not.translate.me')

it 'should not apply filters to not translated messages', ->
translator.addFilter( (message) ->
return message.split('').reverse().join('')
)
expect(translator.translate('unknown.title')).to.be.equal('unknown.title')

it 'should not apply filters to not translatable messages', ->
translator.addFilter( (message) ->
return message.split('').reverse().join('')
)
expect(translator.translate(':web.pages.homepage.simple.title:')).to.be.equal('web.pages.homepage.simple.title')

it 'should apply filters to simple translations', ->
translator.addFilter( (message) ->
return message.split('').reverse().join('')
)
expect(translator.translate('web.pages.homepage.simple.title')).to.be.equal('xob omorp fo eltiT')

it 'should apply filters for lists of translations', ->
translator.addFilter( (message) ->
return message.split('').reverse().join('')
)
expect(translator.translate('web.pages.homepage.promo.fruits', 3)).to.be.eql([
'sananab 3'
'snortic 3'
'segnaro 3'
])

describe '#translatePairs()', ->

it 'should throw an error if message to translate are not arrays', ->
Expand Down

0 comments on commit 5750f85

Please sign in to comment.