diff --git a/app/controllers/language_picker.coffee b/app/controllers/language_picker.coffee new file mode 100644 index 0000000..d72df66 --- /dev/null +++ b/app/controllers/language_picker.coffee @@ -0,0 +1,43 @@ +$ = require 'jqueryify' +translate = require 't7e' +enUs = require '../translations/en_us' + +DEFAULT = '$DEFAULT' + +class LanguagePicker + @DEFAULT = DEFAULT + + languages: + 'English': DEFAULT + 'Polski': 'pl_pl' + + el: null + className: 'language-picker' + + constructor: -> + preferredLanguage = localStorage.preferredLanguage || DEFAULT + + @el = $("") + + for language, code of @languages + option = $("") + option.attr 'selected', true if language is preferredLanguage + @el.append option + + @el.on 'change', => @onChange arguments... + + @el.trigger 'change' unless @el.val() is DEFAULT + + onChange: -> + preferredLanguage = @el.val() + + if preferredLanguage is DEFAULT + translate.load enUs + translate.refresh() + else + $.getJSON "./translations/#{preferredLanguage}.json", (data) -> + console?.log? "Got translations for #{preferredLanguage}", data + translate.load data + translate.refresh() + +module.exports = LanguagePicker diff --git a/app/index.coffee b/app/index.coffee index 35eac2e..9484edd 100644 --- a/app/index.coffee +++ b/app/index.coffee @@ -26,6 +26,11 @@ bc.check() Map::tilesId = 53589 Map::apiKey = '21a5504123984624a5e1a856fc00e238' +LanguagePicker = require 'controllers/language_picker' + +languagePicker = new LanguagePicker +languagePicker.el.appendTo document.body + googleAnalytics.init account: 'UA-1224199-36' domain: 'snapshotserengeti.org' diff --git a/app/lib/setup.coffee b/app/lib/setup.coffee index 01f30e9..91249dd 100644 --- a/app/lib/setup.coffee +++ b/app/lib/setup.coffee @@ -4,14 +4,6 @@ $ = require 'jqueryify' translate.load enUs -preferredLanguage = localStorage.preferredLanguage - -if preferredLanguage? - $.getJSON "./translations/#{language}.json", (data) -> - console.log "Got translations for #{language}", data - translate.load data - translate.refresh() - require 'json2ify' require 'es5-shimify' diff --git a/public/index.html b/public/index.html index 61d6e63..1afeb10 100644 --- a/public/index.html +++ b/public/index.html @@ -37,7 +37,7 @@