Skip to content

Instantly share code, notes, and snippets.

@aleksihakli
Last active August 29, 2015 14:01
Show Gist options
  • Save aleksihakli/c8c8c5cde83476dd484d to your computer and use it in GitHub Desktop.
Save aleksihakli/c8c8c5cde83476dd484d to your computer and use it in GitHub Desktop.
AngularJS localization for website
angular = require 'angular'
app = angular.module 'app', []
app.service 'LocalizationService', require './services/localizationservice.coffee'
app.directive 'tr', require './directives/localizationdirective.coffee'
module.exports = app
dictionary = {
fi: {
greeting: 'Tervetuloa'
switchLocale: 'In English'
}
en: {
greeting: 'Greetings'
switchLocale: 'Suomeksi'
}
}
module.exports = dictionary
doctype html
html(ng-app='app')
head
body
div
h1(tr='greeting')
script(src='js/app.js')
module.exports = (LocalizationService) ->
restrict: 'A',
link: (scope, elem, attrs) ->
scope.$watch(LocalizationService.locale, ->
elem.text(LocalizationService.localize(attrs.tr))
)
module.exports = ->
dictionary = require './dictionary.coffee'
config = {lang: 'en'}
service =
locale: () -> config.lang
setLocale: (lang) -> config.lang = lang
localize: (key) -> dictionary[config.lang][key]
switchLocale: () -> if config.lang is 'en' then config.lang = 'fi' else config.lang = 'en'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment