Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Restore localized date formatting in the UI (#3382)

Koenaa3240240433
diff --git a/ui/app/shared/locales/de/or.json b/ui/app/shared/locales/de/or.json
index e35030426..653dc8cef 100644
--- a/ui/app/shared/locales/de/or.json
+++ b/ui/app/shared/locales/de/or.json
@@ -101,7 +101,7 @@
"cancel": "Abbrechen",
"discard": "Verwerfen",
"createdOn": "Erstellt am:",
- "createdOnWithDate": "Erstellt am: {{-date, LLLL}}",
+ "createdOnWithDate": "Erstellt am: {{-date, llll(weekday: long)}}",
"updatedWithDate": "Aktualisiert am: {{-date, llll}}",
"accessPublicRead": "Alle dürfen lesen",
"type": "Typ",
diff --git a/ui/component/core/src/index.ts b/ui/component/core/src/index.ts
index a205df1f9..435dd09eb 100644
--- a/ui/component/core/src/index.ts
+++ b/ui/component/core/src/index.ts
@@ -21,7 +21,7 @@ import { Console } from "./console";
import rest from "@openremote/rest";
import type { InternalAxiosRequestConfig } from "axios";
import { type EventProvider, type EventProviderFactory, EventProviderStatus, WebSocketEventProvider } from "./event";
-import i18next, { type InitOptions } from "i18next";
+import i18next, { type i18n, type InitOptions } from "i18next";
import i18nextBackend from "i18next-http-backend";
import moment from "moment";
import { AssetModelUtil, Auth, type ConsoleAppConfig, EventProviderType, type ManagerConfig } from "@openremote/model";
@@ -103,6 +103,26 @@ export const I18NEXT_TO_MOMENT_LOCALE: Record<string, string> = {
cn: "zh-cn",
};
+/**
+ * Registers the formats the translations interpolate with on an initialised i18next instance.
+ * i18next lowercases a format name, so the Moment formats that only differ in case take an option.
+ */
+export function addTranslationFormats(i18n: i18n): void {
+ i18n.services.formatter!.add("uppercase", (value: unknown) =>
+ typeof value === "string" ? value.toUpperCase() : String(value)
+ );
+ i18n.services.formatter!.add("lll", (value, lng: string | undefined) =>
+ moment(value)
+ .locale(I18NEXT_TO_MOMENT_LOCALE[lng!] ?? lng!)
+ .format("lll")
+ );
+ i18n.services.formatter!.add("llll", (value, lng: string | undefined, options: { weekday?: string }) =>
+ moment(value)
+ .locale(I18NEXT_TO_MOMENT_LOCALE[lng!] ?? lng!)
+ .format(options?.weekday === "long" ? "LLLL" : "llll")
+ );
+}
+
export function normaliseConfig(config: ManagerConfig): ManagerConfig {
const normalisedConfig: ManagerConfig = config ? Object.assign({}, config) : {};
@@ -482,15 +502,6 @@ export class Manager implements EventProviderFactory {
defaultNS: "app",
fallbackNS: "or",
ns: this.config.loadTranslations,
- interpolation: {
- format: (value, format, lng) => {
- if (format === "uppercase") return value.toUpperCase();
- if (value instanceof Date) {
- return moment(value).format(format);
- }
- return value;
- },
- },
backend: {
loadPath: (langs: string[], namespaces: string[]) => {
if (namespaces.length === 1 && namespaces[0] === "or") {
@@ -512,6 +523,7 @@ export class Manager implements EventProviderFactory {
try {
await i18next.use(i18nextBackend).init(initOptions);
+ addTranslationFormats(i18next);
} catch (e) {
console.error(e);
this._setError(ORError.TRANSLATION_ERROR);
diff --git a/ui/component/or-translate/test/date-interpolation.test.ts b/ui/component/or-translate/test/date-interpolation.test.ts
new file mode 100644
index 000000000..99424859e
--- /dev/null
+++ b/ui/component/or-translate/test/date-interpolation.test.ts
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2026, OpenRemote Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+import { ct, expect } from "@openremote/test";
+
+import { OrTranslate } from "@openremote/or-translate";
+
+import { localisedDates } from "./fixtures/data/localised-dates";
+
+// Built in local time so the rendered clock is the same in every time zone
+const DATE = new Date(2026, 11, 15, 13, 45);
+
+ct.beforeEach(async ({ shared }) => {
+ // Only the date is interpolated, so the language decides the formatting and nothing else
+ await shared.locales(
+ Object.fromEntries(
+ localisedDates.map(({ language }) => [
+ language,
+ {
+ test: {
+ date: "{{-date, lll}}",
+ weekday: "{{-date, llll}}",
+ weekdayLong: "{{-date, llll(weekday: long)}}",
+ },
+ },
+ ])
+ )
+ );
+});
+
+for (const { language, expected } of localisedDates) {
+ ct(`should format an interpolated date in ${language}`, async ({ mount }) => {
+ const date = DATE;
+ const component = await mount(OrTranslate, {
+ props: { value: "date", options: { ns: "test", lng: language, date } },
+ });
+
+ // Moment reaches its locale data through an aliased require. Where the bundler does not follow
+ // that, the data is absent, Moment falls back to `en` and every language renders the English
+ // date while the surrounding text stays translated.
+ await expect(component).toHaveText(expected);
+ });
+}
+
+// i18next lowercases a format name, so `llll` and `LLLL` would be the same format. The weekday
+// option keeps them apart.
+ct("should format an interpolated date with an abbreviated weekday", async ({ mount }) => {
+ const date = DATE;
+ const component = await mount(OrTranslate, {
+ props: { value: "weekday", options: { ns: "test", lng: "de", date } },
+ });
+ await expect(component).toHaveText("Di., 15. Dez. 2026 13:45");
+});
+
+ct("should format an interpolated date with a full weekday", async ({ mount }) => {
+ const date = DATE;
+ const component = await mount(OrTranslate, {
+ props: { value: "weekdayLong", options: { ns: "test", lng: "de", date } },
+ });
+ await expect(component).toHaveText("Dienstag, 15. Dezember 2026 13:45");
+});
diff --git a/ui/component/or-translate/test/fixtures/data/localised-dates.ts b/ui/component/or-translate/test/fixtures/data/localised-dates.ts
new file mode 100644
index 000000000..5d4d17905
--- /dev/null
+++ b/ui/component/or-translate/test/fixtures/data/localised-dates.ts
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2026, OpenRemote Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+export interface LocalisedDate {
+ /** i18next language code. */
+ language: string;
+ /** The `lll` rendering of 15 December 2026 13:45 in that language. */
+ expected: string;
+}
+
+/**
+ * One entry per shipped translation. Each rendering is distinct from the English one, so a language
+ * whose Moment locale data never made it into the bundle renders the wrong string.
+ */
+export const localisedDates: LocalisedDate[] = [
+ { language: "en", expected: "Dec 15, 2026 1:45 PM" },
+ { language: "ar", expected: "١٥ ديسمبر ٢٠٢٦ ١٣:٤٥" },
+ { language: "cn", expected: "2026年12月15日 13:45" },
+ { language: "cs", expected: "15. pro 2026 13:45" },
+ { language: "de", expected: "15. Dez. 2026 13:45" },
+ { language: "es", expected: "15 de dic. de 2026 13:45" },
+ { language: "fr", expected: "15 déc. 2026 13:45" },
+ { language: "it", expected: "15 dic 2026 13:45" },
+ { language: "nl", expected: "15 dec. 2026 13:45" },
+ { language: "pl", expected: "15 gru 2026 13:45" },
+ { language: "pt", expected: "15 de dez de 2026 13:45" },
+ { language: "ro", expected: "15 dec. 2026 13:45" },
+ { language: "uk", expected: "15 груд 2026 р., 13:45" },
+];
diff --git a/ui/test/fixtures/component.ts b/ui/test/fixtures/component.ts
index 6a03f3c9e..2eca2709b 100644
--- a/ui/test/fixtures/component.ts
+++ b/ui/test/fixtures/component.ts
@@ -27,6 +27,7 @@ import type { Asset } from "@openremote/model";
declare global {
interface Window {
_i18next: i18n;
+ _addTranslationFormats: (i18n: i18n) => void;
_assets: Asset[];
}
}
@@ -105,6 +106,7 @@ export class CtShared extends Shared {
loadPath: "/shared/locales/{{lng}}/{{ns}}.json",
},
});
+ window._addTranslationFormats(window._i18next);
if (resources) {
Object.entries(resources).forEach(([locale, r]) =>
Object.entries(r).forEach(([ns, translations]) => {
diff --git a/ui/test/playwright/index.js b/ui/test/playwright/index.js
index a6e9d59c8..c5f3dfa91 100644
--- a/ui/test/playwright/index.js
+++ b/ui/test/playwright/index.js
@@ -17,7 +17,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import themeCss from "@openremote/theme";
-import manager from "@openremote/core";
+import manager, { addTranslationFormats } from "@openremote/core";
import { thingAssetInfo, metaItemDescriptors, valueDescriptors } from "@openremote/test/data";
import i18next from "i18next";
@@ -50,6 +50,10 @@ IconSets.addIconSet("or", createSvgIconSet(OrIconSet.size, OrIconSet.icons));
window._i18next = i18next.use(HttpBackend);
+// Functions cannot cross the `page.evaluate` boundary, so the `locales` fixture reaches the
+// manager's translation formats through the window rather than passing them in
+window._addTranslationFormats = addTranslationFormats;
+
/**
* Mimics subscribing to asset events without connecting to an actual WebSocket.
* @param {string[] | AttributeRef[] | undefined} ids - The asset ids to consider
diff --git a/ui/util/rspack.util.js b/ui/util/rspack.util.js
index 3d194b37e..974cb42cf 100644
--- a/ui/util/rspack.util.js
+++ b/ui/util/rspack.util.js
@@ -25,6 +25,11 @@ const MomentLocalesPlugin = require("moment-locales-webpack-plugin");
function getStandardModuleRules() {
return {
rules: [
+ {
+ // moment resolves its locale data through a require it has assigned to a variable
+ test: /moment[\\/]moment\.js$/,
+ parser: { requireAlias: true },
+ },
{
test: /(maplibre|@material|gridstack|@mdi).*\.css$/, // output css as strings
type: "asset/source",