Last active
August 29, 2015 14:01
-
-
Save aleksihakli/c8c8c5cde83476dd484d to your computer and use it in GitHub Desktop.
AngularJS localization for website
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular = require 'angular' | |
app = angular.module 'app', [] | |
app.service 'LocalizationService', require './services/localizationservice.coffee' | |
app.directive 'tr', require './directives/localizationdirective.coffee' | |
module.exports = app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dictionary = { | |
fi: { | |
greeting: 'Tervetuloa' | |
switchLocale: 'In English' | |
} | |
en: { | |
greeting: 'Greetings' | |
switchLocale: 'Suomeksi' | |
} | |
} | |
module.exports = dictionary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
doctype html | |
html(ng-app='app') | |
head | |
body | |
div | |
h1(tr='greeting') | |
script(src='js/app.js') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = (LocalizationService) -> | |
restrict: 'A', | |
link: (scope, elem, attrs) -> | |
scope.$watch(LocalizationService.locale, -> | |
elem.text(LocalizationService.localize(attrs.tr)) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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