-
Notifications
You must be signed in to change notification settings - Fork 2
/
lingua.js
75 lines (67 loc) · 2.17 KB
/
lingua.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
depends on jed.js (i18n)
depends on python/pybabel/jinja2 for xgettext equivalent extractions
babel.cfg:
[javascript:*.js]
encoding = utf-8
[jinja2: *.html]
encoding = utf-8
usage: >pybabel extract -F babel.cfg -k _n:1,2 -k _ -o messages.pot . partials js
using poedit the tabs for plural translations does not always appear, see https://drupal.org/node/17564
>npm install -g po2json
and per locale:
>po2json translations/sv-se.po l_sv-se.json
*/
var Lingua = {
init:function(doc,cb) {
"use strict";
var locale = localStorage.getItem('locale');
console.log(locale);
if (locale) {
var s = doc.createElement('script');
s.setAttribute('src', "//code.angularjs.org/1.1.5/i18n/angular-locale_"+locale+".js");
doc.body.appendChild(s);
} else {
locale = "en-us";
}
microAjax('l_'+locale+'.json',function(data) {
data = JSON.parse(data);
var i18n = new Jed({
"domain" : locale,
"missing_key_callback" : function(key) {
console.error("Missing key " + key + " for " + locale);
},
locale_data : data
});
console.log(i18n);
window.i18n = i18n;
cb();
});
}
};
angular.module('lingua',[]);
angular.module('lingua').factory('linguaService',function() {
"use strict";
var linguaService = {
_:function(singular, vars) {
return i18n.translate(singular).fetch(vars);
},
_n:function(singular, plural,n, vars) {
if (n) {
return i18n.translate(singular).ifPlural(n, plural).fetch(n);
} else {
return i18n.translate(singular).fetch(vars);
}
}
};
return linguaService;
});
angular.module('lingua').controller('linguaController',['$scope', '$window',function linguaController($scope,$window) {
"use strict";
$scope.changeLocale = function(locale) {
// so, only way to reload $locale is on full page reload
// and load angular-locale_xx-yy.js
localStorage.setItem('locale',locale);
$window.location.reload();
};
}]);