'use strict'; /** * Player-facing translations for Meteors (t1141). * Default locale is English. The player's choice is stored in * localStorage and, when they are logged in, on their server profile. */ var I18n = { LOCALES: [ { id: 'en', flag: 'gb', endonym: 'English', htmlLang: 'en' }, { id: 'nl', flag: 'nl', endonym: 'Nederlands', htmlLang: 'nl' }, { id: 'de', flag: 'de', endonym: 'Deutsch', htmlLang: 'de' }, { id: 'fr', flag: 'fr', endonym: 'Français', htmlLang: 'fr' }, { id: 'ja', flag: 'jp', endonym: '日本語', htmlLang: 'ja' }, { id: 'zh', flag: 'cn', endonym: '简体中文', htmlLang: 'zh-Hans' }, { id: 'ko', flag: 'kr', endonym: '한국어', htmlLang: 'ko' }, { id: 'pl', flag: 'pl', endonym: 'Polski', htmlLang: 'pl' }, { id: 'ru', flag: 'ru', endonym: 'Русский', htmlLang: 'ru' }, { id: 'es', flag: 'es', endonym: 'Español', htmlLang: 'es' } ], DEFAULT: 'en', STORAGE_KEY: 'meteors-locale', COOKIE: 'meteors-locale', locale: 'en', _catalog: null, _listeners: [], _pickerOpen: false }; I18n._load = function () { if (this._catalog) { return this._catalog; } var data = null; if (typeof I18N_CATALOG !== 'undefined' && I18N_CATALOG) { data = I18N_CATALOG; } else if (typeof require !== 'undefined') { try { data = require('../server/lib/i18n.json'); } catch (e) { data = null; } } this._catalog = data && data.locales ? data : { locales: { en: {} }, errors: {} }; return this._catalog; }; I18n.known = function (id) { if (!id) { return false; } for (var i = 0; i < this.LOCALES.length; i++) { if (this.LOCALES[i].id === id) { return true; } } return false; }; I18n.info = function (id) { id = id || this.locale; for (var i = 0; i < this.LOCALES.length; i++) { if (this.LOCALES[i].id === id) { return this.LOCALES[i]; } } return this.LOCALES[0]; }; I18n.t = function (key, params, fallback) { this._load(); var loc = this._catalog.locales[this.locale] || {}; var en = this._catalog.locales.en || {}; var s = loc[key]; if (s == null || s === '') { s = en[key]; } if (s == null || s === '') { s = fallback != null ? fallback : key; } s = String(s); if (params) { s = s.replace(/\{(\w+)\}/g, function (_, k) { return params[k] != null ? String(params[k]) : ''; }); } return s; }; /** Slavic-style plural: one / few / many (falls back to other). */ I18n.pluralForm = function (n, locale) { n = Math.abs(parseInt(n, 10) || 0); locale = locale || this.locale; if (locale === 'ru') { var mod10 = n % 10, mod100 = n % 100; if (mod10 === 1 && mod100 !== 11) { return 'one'; } if (mod10 >= 2 && mod10 <= 4 && (mod100 < 12 || mod100 > 14)) { return 'few'; } return 'many'; } if (locale === 'pl') { if (n === 1) { return 'one'; } var mod10 = n % 10, mod100 = n % 100; if (mod10 >= 2 && mod10 <= 4 && (mod100 < 12 || mod100 > 14)) { return 'few'; } return 'many'; } if (locale === 'fr' || locale === 'es') { return (n === 0 || n === 1) ? 'one' : 'other'; } if (locale === 'ja' || locale === 'zh' || locale === 'ko') { return 'other'; } return n === 1 ? 'one' : 'other'; }; I18n.plural = function (base, n, params) { params = params || {}; params.n = n; var form = this.pluralForm(n); var key = base + '.' + form; this._load(); var loc = this._catalog.locales[this.locale] || {}; var en = this._catalog.locales.en || {}; if (loc[key] == null && en[key] == null) { if (form === 'few' || form === 'many') { key = base + '.other'; } } if ((loc[key] == null && en[key] == null) && form !== 'one') { key = base + '.other'; } return this.t(key, params); }; I18n.place = function (n) { return this.t('podium.place.' + (n === 1 ? '1' : n === 2 ? '2' : n === 3 ? '3' : 'n'), { n: n }, String(n)); }; I18n.planetName = function (planet) { if (!planet) { return ''; } if (planet.custom) { return planet.name || ''; } return this.t('planet.' + planet.id + '.name', null, planet.name || planet.id); }; I18n.peopleName = function (planet) { if (!planet) { return ''; } if (planet.custom) { return planet.people || ''; } return this.t('planet.' + planet.id + '.people', null, planet.people || ''); }; I18n.blockName = function (id, fallback) { if (!id) { return fallback || ''; } return this.t('block.' + id, null, fallback || id); }; I18n.error = function (msg) { if (!msg) { return ''; } this._load(); var map = this._catalog.errors || {}; if (map[msg]) { return this.t(map[msg]); } var m = /^Deze lobby is vol \(maximaal (\d+) spelers\)\.$/.exec(msg); if (m) { return this.t('error.lobbyFull', { n: m[1] }); } m = /^Je hebt het maximum van (\d+) planeten bereikt\.$/.exec(msg); if (m) { return this.t('error.maxPlanets', { n: m[1] }); } m = /^Vul een (.+) in\.$/.exec(msg); if (m) { var field = m[1]; if (field === 'planeetnaam') { field = this.t('error.fieldPlanet'); } else if (field === 'bevolkingsnaam') { field = this.t('error.fieldPeople'); } return this.t('error.fillField', { field: field }); } m = /^De server gaf (\d+)$/.exec(msg); if (m) { return this.t('error.serverStatus', { status: m[1] }); } return msg; }; I18n.readStored = function () { try { if (typeof localStorage !== 'undefined') { var v = localStorage.getItem(this.STORAGE_KEY); if (this.known(v)) { return v; } } } catch (e) { /* private mode */ } return this.DEFAULT; }; I18n._writeStored = function (id) { try { if (typeof localStorage !== 'undefined') { localStorage.setItem(this.STORAGE_KEY, id); } } catch (e) { /* ignore */ } try { if (typeof document !== 'undefined') { document.cookie = this.COOKIE + '=' + encodeURIComponent(id) + ';path=/;max-age=31536000;SameSite=Lax'; } } catch (e) { /* ignore */ } }; I18n.setLocale = function (id, opts) { opts = opts || {}; if (!this.known(id)) { id = this.DEFAULT; } var changed = id !== this.locale; this.locale = id; if (opts.persist !== false) { this._writeStored(id); } if (typeof document !== 'undefined' && document.documentElement) { document.documentElement.lang = this.info(id).htmlLang; document.documentElement.setAttribute('data-locale', id); } this.apply(); this.renderPicker(); if (changed || opts.force) { for (var i = 0; i < this._listeners.length; i++) { try { this._listeners[i](id); } catch (e) { /* listener errors must not break i18n */ } } } if (opts.sync !== false && typeof Online !== 'undefined' && Online.me && Online.setLocale) { Online.setLocale(id); } return id; }; I18n.onChange = function (fn) { if (typeof fn === 'function') { this._listeners.push(fn); } }; I18n.apply = function (root) { if (typeof document === 'undefined') { return; } root = root || document; var nodes = root.querySelectorAll('[data-i18n]'); for (var i = 0; i < nodes.length; i++) { var el = nodes[i]; var key = el.getAttribute('data-i18n'); if (!key) { continue; } el.textContent = this.t(key); } nodes = root.querySelectorAll('[data-i18n-html]'); for (var i = 0; i < nodes.length; i++) { var el = nodes[i]; var key = el.getAttribute('data-i18n-html'); if (!key) { continue; } el.innerHTML = this.t(key); } nodes = root.querySelectorAll('[data-i18n-placeholder]'); for (var i = 0; i < nodes.length; i++) { nodes[i].setAttribute('placeholder', this.t(nodes[i].getAttribute('data-i18n-placeholder'))); } nodes = root.querySelectorAll('[data-i18n-aria]'); for (var i = 0; i < nodes.length; i++) { nodes[i].setAttribute('aria-label', this.t(nodes[i].getAttribute('data-i18n-aria'))); } nodes = root.querySelectorAll('[data-i18n-title]'); for (var i = 0; i < nodes.length; i++) { nodes[i].setAttribute('title', this.t(nodes[i].getAttribute('data-i18n-title'))); } }; I18n.flagUrl = function (id) { var info = this.info(id); return 'images/flags/' + info.flag + '.svg'; }; I18n.togglePicker = function (force) { if (typeof force === 'boolean') { this._pickerOpen = force; } else { this._pickerOpen = !this._pickerOpen; } this.renderPicker(); }; I18n.renderPicker = function () { if (typeof document === 'undefined') { return; } var wrap = document.getElementById('lang-picker'); var btn = document.getElementById('lang-current'); var list = document.getElementById('lang-list'); if (!wrap || !btn || !list) { return; } var info = this.info(); var img = btn.querySelector('img'); if (img) { img.src = this.flagUrl(this.locale); img.alt = info.endonym; } btn.setAttribute('aria-label', this.t('lang.current', { name: info.endonym })); btn.setAttribute('aria-expanded', this._pickerOpen ? 'true' : 'false'); btn.title = info.endonym; list.innerHTML = ''; if (!this._pickerOpen) { list.hidden = true; wrap.classList.remove('open'); return; } wrap.classList.add('open'); list.hidden = false; for (var i = 0; i < this.LOCALES.length; i++) { var loc = this.LOCALES[i]; var opt = document.createElement('button'); opt.type = 'button'; opt.className = 'lang-option' + (loc.id === this.locale ? ' current' : ''); opt.setAttribute('data-locale', loc.id); opt.setAttribute('aria-label', loc.endonym); opt.title = loc.endonym; opt.innerHTML = '' + '' + loc.endonym + ''; (function (id) { opt.addEventListener('click', function (e) { e.stopPropagation(); I18n.setLocale(id); I18n.togglePicker(false); }); })(loc.id); list.appendChild(opt); } }; I18n.bindPicker = function () { if (typeof document === 'undefined') { return; } var btn = document.getElementById('lang-current'); var list = document.getElementById('lang-list'); if (btn) { btn.addEventListener('click', function (e) { e.stopPropagation(); I18n.togglePicker(); }); } document.addEventListener('click', function (e) { var wrap = document.getElementById('lang-picker'); if (!wrap || !I18n._pickerOpen) { return; } if (!wrap.contains(e.target)) { I18n.togglePicker(false); } }); document.addEventListener('keydown', function (e) { if (e.key === 'Escape' && I18n._pickerOpen) { I18n.togglePicker(false); } }); this.renderPicker(); }; I18n.init = function () { this._load(); this.locale = this.readStored(); if (typeof document !== 'undefined' && document.documentElement) { document.documentElement.lang = this.info().htmlLang; document.documentElement.setAttribute('data-locale', this.locale); } this.apply(); }; I18n.applyFromProfile = function (locale) { if (this.known(locale)) { this.setLocale(locale, { sync: false, persist: true }); } }; if (typeof module !== 'undefined' && module.exports) { module.exports = I18n; } else if (typeof globalThis !== 'undefined') { globalThis.I18n = I18n; }