Skip to content

Commit

Permalink
Merge pull request #6 from viattik/master
Browse files Browse the repository at this point in the history
Fallback locale functionality
  • Loading branch information
4vanger committed Jun 29, 2015
2 parents af3689c + 91dec37 commit 9bc29da
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
bower_components
.idea
tmp
48 changes: 34 additions & 14 deletions build/l10n-with-tools.js

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

4 changes: 2 additions & 2 deletions build/l10n-with-tools.min.js

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

48 changes: 34 additions & 14 deletions build/l10n.js

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

4 changes: 2 additions & 2 deletions build/l10n.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 @@ -20,7 +20,7 @@
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-coffee": "~0.6.4",
"grunt-contrib-uglify": "~0.1.x",
"grunt-contrib-uglify": "^0.9.1",
"grunt-contrib-watch": "*",
"karma-chrome-launcher": "~0.1.4",
"karma-jasmine": "~0.1.5",
Expand Down
33 changes: 24 additions & 9 deletions src/l10n.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ angular.module('l10n', ['ngLocale'])
.provider('l10n',
db: {}
locale: null
fallbackLocale: null
localeMessages: {}
# transform messages using function. Must return object with messages
transform: null
Expand Down Expand Up @@ -37,24 +38,38 @@ angular.module('l10n', ['ngLocale'])

@db.getLocale = => locale.id

@db.get = (key, substitutions...) ->
return '' unless key
originalKey = key
# protection against method redefine
key = '$' + key if angular.isFunction @[key]
parent = @
# get value from hash
@db.setFallbackLocale = (localeCode) =>
@fallbackLocale = localeCode

@db.getFallbackLocale = => @fallbackLocale

# get value from hash
findValue = (key, messagesRoot) =>
parent = messagesRoot
while (pos = key.indexOf('.')) > 0
rest = key.substr(pos + 1)
key = key.substr(0, pos)

if typeof parent[key] != 'undefined'
parent = parent[key]
else
return originalKey
return null
key = rest
value = parent[key]
value = originalKey unless value?
value = null unless value?
value

@db.get = (key, substitutions...) ->
return '' unless key
allLocales = @getAllLocales()
fallbackLocale = @getFallbackLocale()
# protection against method redefine
key = '$' + key if angular.isFunction @[key]
value = findValue(key, @)
# using fallback locale key if no message exists for current locale
if !value && fallbackLocale && allLocales?[fallbackLocale]
value = findValue(key, allLocales[fallbackLocale])
value = key unless value

if typeof value == 'string'
# expand @referenced values
Expand Down
8 changes: 8 additions & 0 deletions test/unit/service.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ describe 'l10n.get method', ->
'one': '1 person is viewing.'
'other': '{} people are viewing.'

l10nProvider.add 'en-CA',
'not-existing-message': 'This exists only in en-CA'

module 'test-get'
inject (l10n) ->
service = l10n
Expand Down Expand Up @@ -93,6 +96,11 @@ describe 'l10n.get method', ->
expect(service.get('subs.hello2', 'name', 'name2', 'name3' )).toBe 'Hello, name and name2'
expect(service.get('%1 %2 %3 %4 %5 %6 %7 %8 %9 %10', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)).toBe '1 2 3 4 5 6 7 8 9 10'

it 'should fallback to fallback language if no locale for current language is present', ->
expect(service.get('not-existing-message')).toBe 'not-existing-message'
service.setFallbackLocale 'en-CA'
expect(service.get('not-existing-message')).toBe 'This exists only in en-CA'

describe 'l10n directives', ->
compile = null
scope = null
Expand Down

0 comments on commit 9bc29da

Please sign in to comment.