| 1 | 'use strict'; |
| 2 | |
| 3 | /** |
| 4 | * Player-facing translations for Meteors (t1141). |
| 5 | * Default locale is English. The player's choice is stored in |
| 6 | * localStorage and, when they are logged in, on their server profile. |
| 7 | */ |
| 8 | |
| 9 | var I18n = { |
| 10 | LOCALES: [ |
| 11 | { id: 'en', flag: 'gb', endonym: 'English', htmlLang: 'en' }, |
| 12 | { id: 'nl', flag: 'nl', endonym: 'Nederlands', htmlLang: 'nl' }, |
| 13 | { id: 'de', flag: 'de', endonym: 'Deutsch', htmlLang: 'de' }, |
| 14 | { id: 'fr', flag: 'fr', endonym: 'Français', htmlLang: 'fr' }, |
| 15 | { id: 'ja', flag: 'jp', endonym: '日本語', htmlLang: 'ja' }, |
| 16 | { id: 'zh', flag: 'cn', endonym: '简体中文', htmlLang: 'zh-Hans' }, |
| 17 | { id: 'ko', flag: 'kr', endonym: '한국어', htmlLang: 'ko' }, |
| 18 | { id: 'pl', flag: 'pl', endonym: 'Polski', htmlLang: 'pl' }, |
| 19 | { id: 'ru', flag: 'ru', endonym: 'Русский', htmlLang: 'ru' }, |
| 20 | { id: 'es', flag: 'es', endonym: 'Español', htmlLang: 'es' } |
| 21 | ], |
| 22 | DEFAULT: 'en', |
| 23 | STORAGE_KEY: 'meteors-locale', |
| 24 | EXPLICIT_KEY: 'meteors-locale-chosen', |
| 25 | COOKIE: 'meteors-locale', |
| 26 | locale: 'en', |
| 27 | _catalog: null, |
| 28 | _listeners: [], |
| 29 | _pickerOpen: false |
| 30 | }; |
| 31 | |
| 32 | I18n._load = function () |
| 33 | { |
| 34 | if (this._catalog) |
| 35 | { return this._catalog; } |
| 36 | var data = null; |
| 37 | if (typeof I18N_CATALOG !== 'undefined' && I18N_CATALOG) |
| 38 | { data = I18N_CATALOG; } |
| 39 | else if (typeof require !== 'undefined') |
| 40 | { |
| 41 | try |
| 42 | { data = require('../server/lib/i18n.json'); } |
| 43 | catch (e) { data = null; } |
| 44 | } |
| 45 | this._catalog = data && data.locales ? data : { locales: { en: {} }, errors: {} }; |
| 46 | return this._catalog; |
| 47 | }; |
| 48 | |
| 49 | I18n.known = function (id) |
| 50 | { |
| 51 | if (!id) |
| 52 | { return false; } |
| 53 | for (var i = 0; i < this.LOCALES.length; i++) |
| 54 | { |
| 55 | if (this.LOCALES[i].id === id) |
| 56 | { return true; } |
| 57 | } |
| 58 | return false; |
| 59 | }; |
| 60 | |
| 61 | /** |
| 62 | * Map a BCP 47 auth claim (zh-Hans) or a stored id to a Meteors locale. |
| 63 | * Unknown values return null. |
| 64 | */ |
| 65 | I18n.normalize = function (id) |
| 66 | { |
| 67 | if (id == null || id === '') |
| 68 | { return null; } |
| 69 | var s = String(id).trim().toLowerCase().replace(/_/g, '-'); |
| 70 | if (s === 'zh-hans' || s === 'zh-cn' || s === 'zh-sg' || s === 'zh-my') |
| 71 | { return 'zh'; } |
| 72 | if (this.known(s)) |
| 73 | { return s; } |
| 74 | var primary = s.split('-')[0]; |
| 75 | return this.known(primary) ? primary : null; |
| 76 | }; |
| 77 | |
| 78 | I18n.hasExplicitChoice = function () |
| 79 | { |
| 80 | try |
| 81 | { |
| 82 | if (typeof localStorage !== 'undefined') |
| 83 | { return localStorage.getItem(this.EXPLICIT_KEY) === '1'; } |
| 84 | } |
| 85 | catch (e) { /* private mode */ } |
| 86 | return false; |
| 87 | }; |
| 88 | |
| 89 | I18n.markExplicit = function (on) |
| 90 | { |
| 91 | try |
| 92 | { |
| 93 | if (typeof localStorage === 'undefined') |
| 94 | { return; } |
| 95 | if (on === false) |
| 96 | { localStorage.removeItem(this.EXPLICIT_KEY); } |
| 97 | else |
| 98 | { localStorage.setItem(this.EXPLICIT_KEY, '1'); } |
| 99 | } |
| 100 | catch (e) { /* ignore */ } |
| 101 | }; |
| 102 | |
| 103 | I18n.info = function (id) |
| 104 | { |
| 105 | id = id || this.locale; |
| 106 | for (var i = 0; i < this.LOCALES.length; i++) |
| 107 | { |
| 108 | if (this.LOCALES[i].id === id) |
| 109 | { return this.LOCALES[i]; } |
| 110 | } |
| 111 | return this.LOCALES[0]; |
| 112 | }; |
| 113 | |
| 114 | I18n.t = function (key, params, fallback) |
| 115 | { |
| 116 | this._load(); |
| 117 | var loc = this._catalog.locales[this.locale] || {}; |
| 118 | var en = this._catalog.locales.en || {}; |
| 119 | var s = loc[key]; |
| 120 | if (s == null || s === '') |
| 121 | { s = en[key]; } |
| 122 | if (s == null || s === '') |
| 123 | { s = fallback != null ? fallback : key; } |
| 124 | s = String(s); |
| 125 | if (params) |
| 126 | { |
| 127 | s = s.replace(/\{(\w+)\}/g, function (_, k) |
| 128 | { |
| 129 | return params[k] != null ? String(params[k]) : ''; |
| 130 | }); |
| 131 | } |
| 132 | return s; |
| 133 | }; |
| 134 | |
| 135 | /** Slavic-style plural: one / few / many (falls back to other). */ |
| 136 | I18n.pluralForm = function (n, locale) |
| 137 | { |
| 138 | n = Math.abs(parseInt(n, 10) || 0); |
| 139 | locale = locale || this.locale; |
| 140 | if (locale === 'ru') |
| 141 | { |
| 142 | var mod10 = n % 10, mod100 = n % 100; |
| 143 | if (mod10 === 1 && mod100 !== 11) |
| 144 | { return 'one'; } |
| 145 | if (mod10 >= 2 && mod10 <= 4 && (mod100 < 12 || mod100 > 14)) |
| 146 | { return 'few'; } |
| 147 | return 'many'; |
| 148 | } |
| 149 | if (locale === 'pl') |
| 150 | { |
| 151 | if (n === 1) |
| 152 | { return 'one'; } |
| 153 | var mod10 = n % 10, mod100 = n % 100; |
| 154 | if (mod10 >= 2 && mod10 <= 4 && (mod100 < 12 || mod100 > 14)) |
| 155 | { return 'few'; } |
| 156 | return 'many'; |
| 157 | } |
| 158 | if (locale === 'fr' || locale === 'es') |
| 159 | { return (n === 0 || n === 1) ? 'one' : 'other'; } |
| 160 | if (locale === 'ja' || locale === 'zh' || locale === 'ko') |
| 161 | { return 'other'; } |
| 162 | return n === 1 ? 'one' : 'other'; |
| 163 | }; |
| 164 | |
| 165 | I18n.plural = function (base, n, params) |
| 166 | { |
| 167 | params = params || {}; |
| 168 | params.n = n; |
| 169 | var form = this.pluralForm(n); |
| 170 | var key = base + '.' + form; |
| 171 | this._load(); |
| 172 | var loc = this._catalog.locales[this.locale] || {}; |
| 173 | var en = this._catalog.locales.en || {}; |
| 174 | if (loc[key] == null && en[key] == null) |
| 175 | { |
| 176 | if (form === 'few' || form === 'many') |
| 177 | { key = base + '.other'; } |
| 178 | } |
| 179 | if ((loc[key] == null && en[key] == null) && form !== 'one') |
| 180 | { key = base + '.other'; } |
| 181 | return this.t(key, params); |
| 182 | }; |
| 183 | |
| 184 | I18n.place = function (n) |
| 185 | { |
| 186 | return this.t('podium.place.' + (n === 1 ? '1' : n === 2 ? '2' : n === 3 ? '3' : 'n'), { n: n }, String(n)); |
| 187 | }; |
| 188 | |
| 189 | I18n.planetName = function (planet) |
| 190 | { |
| 191 | if (!planet) |
| 192 | { return ''; } |
| 193 | if (planet.custom) |
| 194 | { return planet.name || ''; } |
| 195 | return this.t('planet.' + planet.id + '.name', null, planet.name || planet.id); |
| 196 | }; |
| 197 | |
| 198 | I18n.peopleName = function (planet) |
| 199 | { |
| 200 | if (!planet) |
| 201 | { return ''; } |
| 202 | if (planet.custom) |
| 203 | { return planet.people || ''; } |
| 204 | return this.t('planet.' + planet.id + '.people', null, planet.people || ''); |
| 205 | }; |
| 206 | |
| 207 | I18n.blockName = function (id, fallback) |
| 208 | { |
| 209 | if (!id) |
| 210 | { return fallback || ''; } |
| 211 | return this.t('block.' + id, null, fallback || id); |
| 212 | }; |
| 213 | |
| 214 | I18n.error = function (msg) |
| 215 | { |
| 216 | if (!msg) |
| 217 | { return ''; } |
| 218 | this._load(); |
| 219 | var map = this._catalog.errors || {}; |
| 220 | if (map[msg]) |
| 221 | { return this.t(map[msg]); } |
| 222 | var m = /^Deze lobby is vol \(maximaal (\d+) spelers\)\.$/.exec(msg); |
| 223 | if (m) |
| 224 | { return this.t('error.lobbyFull', { n: m[1] }); } |
| 225 | m = /^Je hebt het maximum van (\d+) planeten bereikt\.$/.exec(msg); |
| 226 | if (m) |
| 227 | { return this.t('error.maxPlanets', { n: m[1] }); } |
| 228 | m = /^Vul een (.+) in\.$/.exec(msg); |
| 229 | if (m) |
| 230 | { |
| 231 | var field = m[1]; |
| 232 | if (field === 'planeetnaam') |
| 233 | { field = this.t('error.fieldPlanet'); } |
| 234 | else if (field === 'bevolkingsnaam') |
| 235 | { field = this.t('error.fieldPeople'); } |
| 236 | return this.t('error.fillField', { field: field }); |
| 237 | } |
| 238 | m = /^De server gaf (\d+)$/.exec(msg); |
| 239 | if (m) |
| 240 | { return this.t('error.serverStatus', { status: m[1] }); } |
| 241 | return msg; |
| 242 | }; |
| 243 | |
| 244 | I18n.readStored = function () |
| 245 | { |
| 246 | try |
| 247 | { |
| 248 | if (typeof localStorage !== 'undefined') |
| 249 | { |
| 250 | var v = localStorage.getItem(this.STORAGE_KEY); |
| 251 | if (this.known(v)) |
| 252 | { return v; } |
| 253 | } |
| 254 | } |
| 255 | catch (e) { /* private mode */ } |
| 256 | return this.DEFAULT; |
| 257 | }; |
| 258 | |
| 259 | I18n._writeStored = function (id) |
| 260 | { |
| 261 | try |
| 262 | { |
| 263 | if (typeof localStorage !== 'undefined') |
| 264 | { localStorage.setItem(this.STORAGE_KEY, id); } |
| 265 | } |
| 266 | catch (e) { /* ignore */ } |
| 267 | try |
| 268 | { |
| 269 | if (typeof document !== 'undefined') |
| 270 | { |
| 271 | document.cookie = this.COOKIE + '=' + encodeURIComponent(id) |
| 272 | + ';path=/;max-age=31536000;SameSite=Lax'; |
| 273 | } |
| 274 | } |
| 275 | catch (e) { /* ignore */ } |
| 276 | }; |
| 277 | |
| 278 | I18n.setLocale = function (id, opts) |
| 279 | { |
| 280 | opts = opts || {}; |
| 281 | id = this.normalize(id); |
| 282 | if (!id) |
| 283 | { id = this.DEFAULT; } |
| 284 | var changed = id !== this.locale; |
| 285 | this.locale = id; |
| 286 | if (opts.persist !== false) |
| 287 | { this._writeStored(id); } |
| 288 | if (opts.explicit !== false && opts.persist !== false) |
| 289 | { this.markExplicit(); } |
| 290 | if (typeof document !== 'undefined' && document.documentElement) |
| 291 | { |
| 292 | document.documentElement.lang = this.info(id).htmlLang; |
| 293 | document.documentElement.setAttribute('data-locale', id); |
| 294 | } |
| 295 | this.apply(); |
| 296 | this.renderPicker(); |
| 297 | if (changed || opts.force) |
| 298 | { |
| 299 | for (var i = 0; i < this._listeners.length; i++) |
| 300 | { |
| 301 | try |
| 302 | { this._listeners[i](id); } |
| 303 | catch (e) { /* listener errors must not break i18n */ } |
| 304 | } |
| 305 | } |
| 306 | if (opts.sync !== false && typeof Online !== 'undefined' && Online.me && Online.setLocale) |
| 307 | { Online.setLocale(id); } |
| 308 | return id; |
| 309 | }; |
| 310 | |
| 311 | I18n.onChange = function (fn) |
| 312 | { |
| 313 | if (typeof fn === 'function') |
| 314 | { this._listeners.push(fn); } |
| 315 | }; |
| 316 | |
| 317 | I18n.apply = function (root) |
| 318 | { |
| 319 | if (typeof document === 'undefined') |
| 320 | { return; } |
| 321 | root = root || document; |
| 322 | var nodes = root.querySelectorAll('[data-i18n]'); |
| 323 | for (var i = 0; i < nodes.length; i++) |
| 324 | { |
| 325 | var el = nodes[i]; |
| 326 | var key = el.getAttribute('data-i18n'); |
| 327 | if (!key) |
| 328 | { continue; } |
| 329 | // Fall back to English (inside t) then the existing label so a |
| 330 | // missing/stale key never replaces visible text with the raw key. |
| 331 | el.textContent = this.t(key, null, el.textContent); |
| 332 | } |
| 333 | nodes = root.querySelectorAll('[data-i18n-html]'); |
| 334 | for (var i = 0; i < nodes.length; i++) |
| 335 | { |
| 336 | var el = nodes[i]; |
| 337 | var key = el.getAttribute('data-i18n-html'); |
| 338 | if (!key) |
| 339 | { continue; } |
| 340 | el.innerHTML = this.t(key, null, el.innerHTML); |
| 341 | } |
| 342 | nodes = root.querySelectorAll('[data-i18n-placeholder]'); |
| 343 | for (var i = 0; i < nodes.length; i++) |
| 344 | { |
| 345 | var el = nodes[i]; |
| 346 | el.setAttribute('placeholder', this.t(el.getAttribute('data-i18n-placeholder'), null, el.getAttribute('placeholder'))); |
| 347 | } |
| 348 | nodes = root.querySelectorAll('[data-i18n-aria]'); |
| 349 | for (var i = 0; i < nodes.length; i++) |
| 350 | { |
| 351 | var el = nodes[i]; |
| 352 | el.setAttribute('aria-label', this.t(el.getAttribute('data-i18n-aria'), null, el.getAttribute('aria-label'))); |
| 353 | } |
| 354 | nodes = root.querySelectorAll('[data-i18n-title]'); |
| 355 | for (var i = 0; i < nodes.length; i++) |
| 356 | { |
| 357 | var el = nodes[i]; |
| 358 | el.setAttribute('title', this.t(el.getAttribute('data-i18n-title'), null, el.getAttribute('title'))); |
| 359 | } |
| 360 | }; |
| 361 | |
| 362 | I18n.flagUrl = function (id) |
| 363 | { |
| 364 | var info = this.info(id); |
| 365 | return 'images/flags/' + info.flag + '.svg'; |
| 366 | }; |
| 367 | |
| 368 | I18n.togglePicker = function (force) |
| 369 | { |
| 370 | if (typeof force === 'boolean') |
| 371 | { this._pickerOpen = force; } |
| 372 | else |
| 373 | { this._pickerOpen = !this._pickerOpen; } |
| 374 | this.renderPicker(); |
| 375 | }; |
| 376 | |
| 377 | I18n.renderPicker = function () |
| 378 | { |
| 379 | if (typeof document === 'undefined') |
| 380 | { return; } |
| 381 | var wrap = document.getElementById('lang-picker'); |
| 382 | var btn = document.getElementById('lang-current'); |
| 383 | var list = document.getElementById('lang-list'); |
| 384 | if (!wrap || !btn || !list) |
| 385 | { return; } |
| 386 | var info = this.info(); |
| 387 | var img = btn.querySelector('img'); |
| 388 | if (img) |
| 389 | { |
| 390 | img.src = this.flagUrl(this.locale); |
| 391 | img.alt = info.endonym; |
| 392 | } |
| 393 | btn.setAttribute('aria-label', this.t('lang.current', { name: info.endonym })); |
| 394 | btn.setAttribute('aria-expanded', this._pickerOpen ? 'true' : 'false'); |
| 395 | btn.title = info.endonym; |
| 396 | list.innerHTML = ''; |
| 397 | if (!this._pickerOpen) |
| 398 | { |
| 399 | list.hidden = true; |
| 400 | wrap.classList.remove('open'); |
| 401 | return; |
| 402 | } |
| 403 | wrap.classList.add('open'); |
| 404 | list.hidden = false; |
| 405 | for (var i = 0; i < this.LOCALES.length; i++) |
| 406 | { |
| 407 | var loc = this.LOCALES[i]; |
| 408 | var opt = document.createElement('button'); |
| 409 | opt.type = 'button'; |
| 410 | opt.className = 'lang-option' + (loc.id === this.locale ? ' current' : ''); |
| 411 | opt.setAttribute('data-locale', loc.id); |
| 412 | opt.setAttribute('aria-label', loc.endonym); |
| 413 | opt.title = loc.endonym; |
| 414 | opt.innerHTML = '<img src="' + this.flagUrl(loc.id) + '" alt="" width="36" height="24">' |
| 415 | + '<span>' + loc.endonym + '</span>'; |
| 416 | (function (id) |
| 417 | { |
| 418 | opt.addEventListener('click', function (e) |
| 419 | { |
| 420 | e.stopPropagation(); |
| 421 | I18n.setLocale(id); |
| 422 | I18n.togglePicker(false); |
| 423 | }); |
| 424 | })(loc.id); |
| 425 | list.appendChild(opt); |
| 426 | } |
| 427 | }; |
| 428 | |
| 429 | I18n.bindPicker = function () |
| 430 | { |
| 431 | if (typeof document === 'undefined') |
| 432 | { return; } |
| 433 | var btn = document.getElementById('lang-current'); |
| 434 | var list = document.getElementById('lang-list'); |
| 435 | if (btn) |
| 436 | { |
| 437 | btn.addEventListener('click', function (e) |
| 438 | { |
| 439 | e.stopPropagation(); |
| 440 | I18n.togglePicker(); |
| 441 | }); |
| 442 | } |
| 443 | document.addEventListener('click', function (e) |
| 444 | { |
| 445 | var wrap = document.getElementById('lang-picker'); |
| 446 | if (!wrap || !I18n._pickerOpen) |
| 447 | { return; } |
| 448 | if (!wrap.contains(e.target)) |
| 449 | { I18n.togglePicker(false); } |
| 450 | }); |
| 451 | document.addEventListener('keydown', function (e) |
| 452 | { |
| 453 | if (e.key === 'Escape' && I18n._pickerOpen) |
| 454 | { I18n.togglePicker(false); } |
| 455 | }); |
| 456 | this.renderPicker(); |
| 457 | }; |
| 458 | |
| 459 | I18n.init = function () |
| 460 | { |
| 461 | this._load(); |
| 462 | this.locale = this.readStored(); |
| 463 | if (typeof document !== 'undefined' && document.documentElement) |
| 464 | { |
| 465 | document.documentElement.lang = this.info().htmlLang; |
| 466 | document.documentElement.setAttribute('data-locale', this.locale); |
| 467 | } |
| 468 | this.apply(); |
| 469 | }; |
| 470 | |
| 471 | I18n.applyFromProfile = function (locale) |
| 472 | { |
| 473 | this.applyPreferred({ locale: locale, explicit: true }); |
| 474 | }; |
| 475 | |
| 476 | /** |
| 477 | * t1250: an explicit in-game choice always wins. Otherwise a logged-in |
| 478 | * player follows the auth OIDC locale (mapped, e.g. zh-Hans → zh). |
| 479 | * Guests are left to init()/localStorage. |
| 480 | */ |
| 481 | I18n.applyPreferred = function (opts) |
| 482 | { |
| 483 | opts = opts || {}; |
| 484 | var chosen = this.normalize(opts.locale); |
| 485 | var auth = this.normalize(opts.authLocale); |
| 486 | var explicit = !!(opts.explicit && chosen) || this.hasExplicitChoice(); |
| 487 | if (explicit) |
| 488 | { |
| 489 | var id = chosen || this.readStored(); |
| 490 | if (this.known(id)) |
| 491 | { |
| 492 | this.setLocale(id, { sync: false, persist: true, explicit: true }); |
| 493 | if (opts.syncExplicit && typeof Online !== 'undefined' && Online.me && Online.setLocale |
| 494 | && Online.locale !== id) |
| 495 | { Online.setLocale(id); } |
| 496 | } |
| 497 | return id; |
| 498 | } |
| 499 | if (auth) |
| 500 | { return this.setLocale(auth, { persist: false, sync: false, explicit: false }); } |
| 501 | return this.locale; |
| 502 | }; |
| 503 | |
| 504 | if (typeof module !== 'undefined' && module.exports) |
| 505 | { module.exports = I18n; } |
| 506 | else if (typeof globalThis !== 'undefined') |
| 507 | { globalThis.I18n = I18n; } |
projects-timfalken-com meteors