1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports["default"] = exports.getLanguage = exports.getLocale = exports.getTranslation = void 0;
|
7 |
|
8 | var _global = require("sharyn/util/global");
|
9 |
|
10 | var _sprintfJs = require("sprintf-js");
|
11 |
|
12 |
|
13 | var getTranslation = function getTranslation(translations, language, key) {
|
14 | var match;
|
15 |
|
16 | try {
|
17 | match = translations[key][language];
|
18 | } catch (_unused) {
|
19 | console.warn("Could not find a translation for key \"".concat(key, "\" in language \"").concat(language, "\"."));
|
20 | return key;
|
21 | }
|
22 |
|
23 | for (var _len = arguments.length, params = new Array(_len > 3 ? _len - 3 : 0), _key = 3; _key < _len; _key++) {
|
24 | params[_key - 3] = arguments[_key];
|
25 | }
|
26 |
|
27 | return (0, _sprintfJs.vsprintf)(match, params);
|
28 | };
|
29 |
|
30 | exports.getTranslation = getTranslation;
|
31 |
|
32 | var translate = function translate() {
|
33 | for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
34 | args[_key2] = arguments[_key2];
|
35 | }
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | try {
|
44 |
|
45 | if (!TRANSLATIONS) {
|
46 | return null;
|
47 | }
|
48 | } catch (_unused2) {
|
49 | return args[0];
|
50 | }
|
51 |
|
52 | var language = (0, _global.getGlobal)('language');
|
53 | var translations = (0, _global.getGlobal)('translations');
|
54 |
|
55 | if (!language || !translations) {
|
56 | throw Error('No i18n globalConfig set, you need to import autoconfig in your client entrypoint file');
|
57 | }
|
58 |
|
59 | return getTranslation.apply(void 0, [translations, language].concat(args));
|
60 | };
|
61 |
|
62 | var getLocale = function getLocale() {
|
63 |
|
64 | try {
|
65 | var _window, _ref, _userLanguage, _navigator;
|
66 |
|
67 | var localStorage = (_window = window) === null || _window === void 0 ? void 0 : _window.localStorage;
|
68 | var userLanguage = localStorage && localStorage.getItem('user-language');
|
69 | var locale = (_ref = (_userLanguage = userLanguage) !== null && _userLanguage !== void 0 ? _userLanguage : (_navigator = navigator) === null || _navigator === void 0 ? void 0 : _navigator.language) !== null && _ref !== void 0 ? _ref : 'en-GB';
|
70 | return locale;
|
71 | } catch (_unused3) {
|
72 | return 'en-GB';
|
73 | }
|
74 | };
|
75 |
|
76 | exports.getLocale = getLocale;
|
77 |
|
78 | var getLanguage = function getLanguage() {
|
79 | return getLocale().substr(0, 2);
|
80 | };
|
81 |
|
82 | exports.getLanguage = getLanguage;
|
83 | var _default = translate;
|
84 | exports["default"] = _default; |
\ | No newline at end of file |