From 390a523278e2c0c94a4d63e5e9cda13e6db0a280 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 9 Mar 2021 14:43:52 +0100 Subject: [PATCH] Handle non-string values in Language.ts without logging debug messages --- ts/WoltLabSuite/Core/Date/Picker.ts | 4 ++-- ts/WoltLabSuite/Core/Date/Time/Relative.ts | 2 +- ts/WoltLabSuite/Core/Date/Util.ts | 8 ++++---- ts/WoltLabSuite/Core/Language.ts | 14 +++++++++++++- .../files/js/WoltLabSuite/Core/Language.js | 15 ++++++++++++++- 5 files changed, 34 insertions(+), 9 deletions(-) diff --git a/ts/WoltLabSuite/Core/Date/Picker.ts b/ts/WoltLabSuite/Core/Date/Picker.ts index a4fe0b67e0..bfb1ca2f7a 100644 --- a/ts/WoltLabSuite/Core/Date/Picker.ts +++ b/ts/WoltLabSuite/Core/Date/Picker.ts @@ -75,7 +75,7 @@ function createPicker() { monthYearContainer.appendChild(_dateMonth); let months = ""; - const monthNames = Language.get("__monthsShort"); + const monthNames = (Language.get("__monthsShort") as any) as string[]; for (let i = 0; i < 12; i++) { months += ``; } @@ -106,7 +106,7 @@ function createPicker() { item.className = "weekdays"; _dateGrid.appendChild(item); - const weekdays = Language.get("__daysShort"); + const weekdays = (Language.get("__daysShort") as any) as string[]; for (let i = 0; i < 7; i++) { let day = i + _firstDayOfWeek; if (day > 6) { diff --git a/ts/WoltLabSuite/Core/Date/Time/Relative.ts b/ts/WoltLabSuite/Core/Date/Time/Relative.ts index 507dbd404c..f7e268fd3a 100644 --- a/ts/WoltLabSuite/Core/Date/Time/Relative.ts +++ b/ts/WoltLabSuite/Core/Date/Time/Relative.ts @@ -86,7 +86,7 @@ function rebuild(element: HTMLTimeElement, date: Date, timestamp: number): void // get day of week const dateObj = DateUtil.getTimezoneDate(elTimestamp * 1000, parseInt(elOffset, 10) * 1000); const dow = dateObj.getDay(); - const day = Language.get("__days")[dow]; + const day = ((Language.get("__days") as any) as string[])[dow]; element.textContent = Language.get("wcf.date.relative.pastDays", { days: days, day: day, time: elTime }); } diff --git a/ts/WoltLabSuite/Core/Date/Util.ts b/ts/WoltLabSuite/Core/Date/Util.ts index 84fdd6f2b4..09c67b3689 100644 --- a/ts/WoltLabSuite/Core/Date/Util.ts +++ b/ts/WoltLabSuite/Core/Date/Util.ts @@ -117,11 +117,11 @@ export function format(date: Date, format: string): string { break; case "l": // `Monday` through `Sunday` (localized) - char = Language.get("__days")[date.getDay()]; + char = ((Language.get("__days") as any) as string[])[date.getDay()]; break; case "D": // `Mon` through `Sun` (localized) - char = Language.get("__daysShort")[date.getDay()]; + char = ((Language.get("__daysShort") as any) as string[])[date.getDay()]; break; case "S": // ignore english ordinal suffix @@ -139,11 +139,11 @@ export function format(date: Date, format: string): string { break; case "F": // `January` through `December` (localized) - char = Language.get("__months")[date.getMonth()]; + char = ((Language.get("__months") as any) as string[])[date.getMonth()]; break; case "M": // `Jan` through `Dec` (localized) - char = Language.get("__monthsShort")[date.getMonth()]; + char = ((Language.get("__monthsShort") as any) as string[])[date.getMonth()]; break; // year diff --git a/ts/WoltLabSuite/Core/Language.ts b/ts/WoltLabSuite/Core/Language.ts index e61b0e62ee..1a47988eea 100644 --- a/ts/WoltLabSuite/Core/Language.ts +++ b/ts/WoltLabSuite/Core/Language.ts @@ -27,7 +27,19 @@ export function addObject(object: LanguageItems): void { * Adds a single language item to the store. */ export function add(key: string, value: string): void { - addToStore(key, compile(value)); + if (typeof value === "string") { + addToStore(key, compile(value)); + } else { + // Historically a few items that are added to the language store do not represent actual phrases, but + // instead contain a collection (i.e. Array) of items. Most notably these are entries related to date + // processing, containg lists of localized month / weekday names. + // + // Despite this method technically only taking `string`s as the `value` we need to correctly handle + // them which we do by simply storing a function that returns the value as-is. + addToStore(key, function () { + return value; + }); + } } /** diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Language.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Language.js index ae76f0b437..9afa7c0729 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Language.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Language.js @@ -26,7 +26,20 @@ define(["require", "exports", "tslib", "./Template", "./Language/Store", "./Lang * Adds a single language item to the store. */ function add(key, value) { - Store_1.add(key, compile(value)); + if (typeof value === "string") { + Store_1.add(key, compile(value)); + } + else { + // Historically a few items that are added to the language store do not represent actual phrases, but + // instead contain a collection (i.e. Array) of items. Most notably these are entries related to date + // processing, containg lists of localized month / weekday names. + // + // Despite this method technically only taking `string`s as the `value` we need to correctly handle + // them which we do by simply storing a function that returns the value as-is. + Store_1.add(key, function () { + return value; + }); + } } exports.add = add; /** -- 2.20.1