1 | import $ from 'jquery';
|
2 | import Jed from 'jed';
|
3 | import { global } from '../contexts';
|
4 |
|
5 | let dictionaries = {};
|
6 |
|
7 | export function gettext(msgid) {
|
8 | var currentDictionary = dictionaries[global.language];
|
9 | if (currentDictionary) {
|
10 | return currentDictionary.gettext(msgid);
|
11 | }
|
12 | return msgid;
|
13 | }
|
14 |
|
15 | export function ngettext(singular, plural, count) {
|
16 | var currentDictionary = dictionaries[global.language];
|
17 | if (currentDictionary) {
|
18 | return currentDictionary.ngettext(singular, plural, count);
|
19 | }
|
20 | return count === 1 ? singular : plural;
|
21 | }
|
22 |
|
23 | export function gettext_noop(msgid) {
|
24 | return msgid;
|
25 | }
|
26 |
|
27 | export function pgettext(context, msgid) {
|
28 | var currentDictionary = dictionaries[global.language];
|
29 | if (currentDictionary) {
|
30 | return currentDictionary.pgettext(context, msgid);
|
31 | }
|
32 | return msgid;
|
33 | }
|
34 |
|
35 | export function npgettext(context, singular, plural, count) {
|
36 | var currentDictionary = dictionaries[global.language];
|
37 | if (currentDictionary) {
|
38 | return currentDictionary.npgettext(context, singular, plural, count);
|
39 | }
|
40 | return count === 1 ? singular : plural;
|
41 | }
|
42 |
|
43 | export function activate(language) {
|
44 |
|
45 | return new Promise((resolve) => {
|
46 | if (dictionaries[language]) {
|
47 | global.language = language;
|
48 | resolve();
|
49 | } else {
|
50 | $.ajax(`/locale/${language}.json`, {
|
51 | success: (data) => {
|
52 | dictionaries[language] = new Jed(data);
|
53 | global.language = language;
|
54 | },
|
55 | complete: () => {
|
56 | resolve();
|
57 | }
|
58 | });
|
59 | }
|
60 | });
|
61 | }
|