summaryrefslogtreecommitdiffhomepage
path: root/gui/src/shared/gettext.ts
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@mullvad.net>2019-04-09 15:42:36 +0200
committerAndrej Mihajlov <and@mullvad.net>2019-04-09 15:42:36 +0200
commitb427aa1adca8fcfeca7f777499f0b4e1c84e3c6e (patch)
treeb096c6eb2a2bf3885436910894249452ccb1d813 /gui/src/shared/gettext.ts
parent88031a653167df93396433fcf53a1422c2f995fd (diff)
parent6405e1eebbe12313bed10f3f8bd1a0f051e83d24 (diff)
downloadmullvadvpn-b427aa1adca8fcfeca7f777499f0b4e1c84e3c6e.tar.xz
mullvadvpn-b427aa1adca8fcfeca7f777499f0b4e1c84e3c6e.zip
Merge branch 'multi-catalogue'
Diffstat (limited to 'gui/src/shared/gettext.ts')
-rw-r--r--gui/src/shared/gettext.ts62
1 files changed, 37 insertions, 25 deletions
diff --git a/gui/src/shared/gettext.ts b/gui/src/shared/gettext.ts
index 45ff8d3637..07a560bdd8 100644
--- a/gui/src/shared/gettext.ts
+++ b/gui/src/shared/gettext.ts
@@ -5,23 +5,9 @@ import Gettext from 'node-gettext';
import path from 'path';
const SOURCE_LANGUAGE = 'en';
-let SELECTED_LANGUAGE = SOURCE_LANGUAGE;
const LOCALES_DIR = path.resolve(__dirname, '../../locales');
-// `{debug: false}` option prevents Gettext from printing the warnings to console in development
-// the errors are handled separately in the "error" handler below
-const catalogue = new Gettext({ debug: false });
-catalogue.setTextDomain('messages');
-catalogue.on('error', (error) => {
- // Filter out the "no translation was found" errors for the source language
- if (SELECTED_LANGUAGE === SOURCE_LANGUAGE && error.indexOf('No translation was found') !== -1) {
- return;
- }
-
- log.warn(`Gettext error: ${error}`);
-});
-
-export function loadTranslations(currentLocale: string) {
+export function loadTranslations(currentLocale: string, catalogue: Gettext) {
// First look for exact match of the current locale
const preferredLocales = [];
@@ -36,17 +22,18 @@ export function loadTranslations(currentLocale: string) {
}
for (const locale of preferredLocales) {
- if (parseTranslation(locale, 'messages')) {
+ // NOTE: domain is not publicly exposed
+ const domain = (catalogue as any).domain;
+
+ if (parseTranslation(locale, domain, catalogue)) {
log.info(`Loaded translations for ${locale}`);
catalogue.setLocale(locale);
-
- SELECTED_LANGUAGE = locale;
return;
}
}
}
-function parseTranslation(locale: string, domain: string): boolean {
+function parseTranslation(locale: string, domain: string, catalogue: Gettext): boolean {
const filename = path.join(LOCALES_DIR, locale, `${domain}.po`);
let buffer: Buffer;
@@ -72,9 +59,34 @@ function parseTranslation(locale: string, domain: string): boolean {
return true;
}
-export const gettext = (msgid: string): string => {
- return catalogue.gettext(msgid);
-};
-export const pgettext = (msgctx: string, msgid: string): string => {
- return catalogue.pgettext(msgctx, msgid);
-};
+function setErrorHandler(catalogue: Gettext) {
+ catalogue.on('error', (error) => {
+ // NOTE: locale is not publicly exposed
+ const catalogueLocale = (catalogue as any).locale;
+
+ // Filter out the "no translation was found" errors for the source language
+ if (catalogueLocale === SOURCE_LANGUAGE && error.indexOf('No translation was found') !== -1) {
+ return;
+ }
+
+ log.warn(`Gettext error: ${error}`);
+ });
+}
+
+// `{debug: false}` option prevents Gettext from printing the warnings to console in development
+// the errors are handled separately in the "error" handler below
+export const messages = new Gettext({ debug: false });
+messages.setTextDomain('messages');
+setErrorHandler(messages);
+
+export const countries = new Gettext({ debug: false });
+countries.setTextDomain('countries');
+setErrorHandler(countries);
+
+export const cities = new Gettext({ debug: false });
+cities.setTextDomain('cities');
+setErrorHandler(cities);
+
+export const relayLocations = new Gettext({ debug: false });
+relayLocations.setTextDomain('relay-locations');
+setErrorHandler(relayLocations);