Throw an error if providing translation for a language that is already supported

This commit is contained in:
Joseph Garrone 2024-09-21 18:17:43 +02:00
parent 969744f4cb
commit 2a3ad58c18
2 changed files with 17 additions and 2 deletions

View File

@ -16,7 +16,7 @@ export type GenericI18n_noJsx<MessageKey extends string, LanguageTag extends str
* Used to render a select that enable user to switch language.
* ex: https://user-images.githubusercontent.com/6702424/186044799-38801eec-4e89-483b-81dd-8e9233e8c0eb.png
* */
labelBySupportedLanguageTag: Record<string, string>;
labelBySupportedLanguageTag: Record<string /*LanguageTag*/, string>;
/**
*
* Examples assuming currentLanguageTag === "en"

View File

@ -7,7 +7,11 @@ import { FALLBACK_LANGUAGE_TAG } from "keycloakify/bin/shared/constants";
import { id } from "tsafe/id";
import { is } from "tsafe/is";
import { Reflect } from "tsafe/Reflect";
import type { LanguageTag as LanguageTag_defaultSet, MessageKey as MessageKey_defaultSet } from "../messages_defaultSet/types";
import {
type LanguageTag as LanguageTag_defaultSet,
type MessageKey as MessageKey_defaultSet,
languageTags as languageTags_defaultSet
} from "../messages_defaultSet/types";
import type { GenericI18n_noJsx } from "./GenericI18n_noJsx";
export type KcContextLike = {
@ -49,6 +53,17 @@ export function createGetI18n<
}): ReturnTypeOfCreateGetI18n<MessageKey_themeDefined, LanguageTag_notInDefaultSet> {
const { extraLanguageTranslations, messagesByLanguageTag_themeDefined } = params;
Object.keys(extraLanguageTranslations).forEach(languageTag_notInDefaultSet => {
if (id<readonly string[]>(languageTags_defaultSet).includes(languageTag_notInDefaultSet)) {
throw new Error(
[
`Language "${languageTag_notInDefaultSet}" is already in the default set, you don't need to provide your own base translations for it`,
`If you want to override some translations for this language, you can use the "withCustomTranslations" method`
].join(" ")
);
}
});
type LanguageTag = LanguageTag_defaultSet | LanguageTag_notInDefaultSet;
type MessageKey = MessageKey_defaultSet | MessageKey_themeDefined;