UNPKG

3.02 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports["default"] = exports.getLanguage = exports.getLocale = exports.getTranslation = void 0;
7
8var _global = require("sharyn/util/global");
9
10var _sprintfJs = require("sprintf-js");
11
12/* eslint-disable no-undef */
13var 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
30exports.getTranslation = getTranslation;
31
32var 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 // Super Hacky Warning: This whole try/catch block is only used to detect if the code is being
38 // executed by Webpack when it is compiling the bundle (in which case TRANSLATIONS is a ReferenceError
39 // that needs to be caught or it breaks the compilation).
40 // We need to do this because when Webpack crawls files to analyze the project and build the bundle,
41 // it actually executes t() if they are at the root level (this is the case for titles in route
42 // declarations for instance). We return here to avoid actually looking up a translation.
43 try {
44 // flow-disable-next-line
45 if (!TRANSLATIONS) {
46 return null;
47 }
48 } catch (_unused2) {
49 return args[0]; // Use the translation key as the fallback string for non-Webpack environments
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
62var getLocale = function getLocale() {
63 // Try/catch to prevent the Webpack build to break on window or navigator
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
76exports.getLocale = getLocale;
77
78var getLanguage = function getLanguage() {
79 return getLocale().substr(0, 2);
80};
81
82exports.getLanguage = getLanguage;
83var _default = translate;
84exports["default"] = _default;
\No newline at end of file