Skip to content

Commit

Permalink
Dump translations by domain as modules to use with AssetMapper
Browse files Browse the repository at this point in the history
  • Loading branch information
maelanleborgne committed Jul 2, 2024
1 parent fed00e2 commit a370cb7
Show file tree
Hide file tree
Showing 23 changed files with 1,363 additions and 226 deletions.
2 changes: 2 additions & 0 deletions src/Translator/assets/dist/translator.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type LocaleOf<M> = M extends Message<TranslationsType, infer Locale> ? Lo
export type ParametersOf<M, D extends DomainType> = M extends Message<infer Translations, LocaleType> ? Translations[D] extends {
parameters: infer Parameters;
} ? Parameters : never : never;
export type RegisteredTranslationsType = Record<DomainType, Record<LocaleType, Record<string, string>>>;
export interface Message<Translations extends TranslationsType, Locale extends LocaleType> {
id: string;
translations: {
Expand All @@ -24,3 +25,4 @@ export declare function getLocale(): LocaleType;
export declare function setLocaleFallbacks(localeFallbacks: Record<LocaleType, LocaleType>): void;
export declare function getLocaleFallbacks(): Record<LocaleType, LocaleType>;
export declare function trans<M extends Message<TranslationsType, LocaleType>, D extends DomainsOf<M>, P extends ParametersOf<M, D>>(...args: P extends NoParametersType ? [message: M, parameters?: P, domain?: RemoveIntlIcuSuffix<D>, locale?: LocaleOf<M>] : [message: M, parameters: P, domain?: RemoveIntlIcuSuffix<D>, locale?: LocaleOf<M>]): string;
export declare function registerDomain(domainTranslations: RegisteredTranslationsType): void;
28 changes: 27 additions & 1 deletion src/Translator/assets/dist/translator_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ function getPluralizationRule(number, locale) {

let _locale = null;
let _localeFallbacks = {};
let _registeredTranslations = {};
function setLocale(locale) {
_locale = locale;
}
Expand All @@ -212,6 +213,9 @@ function trans(message, parameters = {}, domain = 'messages', locale = null) {
if (typeof locale === 'undefined' || null === locale) {
locale = getLocale();
}
if (typeof message === 'string') {
message = getRegisteredMessage(message, domain);
}
if (typeof message.translations === 'undefined') {
return message.id;
}
Expand Down Expand Up @@ -242,5 +246,27 @@ function trans(message, parameters = {}, domain = 'messages', locale = null) {
}
return message.id;
}
function registerDomain(domainTranslations) {
for (const [domainName, translationsByLocale] of Object.entries(domainTranslations)) {
_registeredTranslations[domainName] = translationsByLocale;
}
}
function getRegisteredMessage(key, domain) {
var _a;
var _b;
let message = { id: key, translations: {} };
for (const domainName of [domain, domain + '+intl-icu']) {
if (typeof _registeredTranslations[domainName] === 'undefined') {
continue;
}
for (const [locale, translations] of Object.entries(_registeredTranslations[domainName])) {
if (typeof translations[key] !== 'undefined') {
(_a = (_b = message.translations)[domainName]) !== null && _a !== void 0 ? _a : (_b[domainName] = {});
message.translations[domainName][locale] = translations[key];
}
}
}
return message;
}

export { getLocale, getLocaleFallbacks, setLocale, setLocaleFallbacks, trans };
export { getLocale, getLocaleFallbacks, registerDomain, setLocale, setLocaleFallbacks, trans };
36 changes: 35 additions & 1 deletion src/Translator/assets/src/translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export type ParametersOf<M, D extends DomainType> = M extends Message<infer Tran
: never
: never;

export type RegisteredTranslationsType = Record<DomainType, Record<LocaleType, Record<string, string>>>;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export interface Message<Translations extends TranslationsType, Locale extends LocaleType> {
id: string;
Expand All @@ -41,6 +43,8 @@ import { format } from './formatters/formatter';
let _locale: LocaleType | null = null;
let _localeFallbacks: Record<LocaleType, LocaleType> = {};

let _registeredTranslations: RegisteredTranslationsType = {};

export function setLocale(locale: LocaleType | null) {
_locale = locale;
}
Expand Down Expand Up @@ -113,7 +117,7 @@ export function trans<
: [message: M, parameters: P, domain?: RemoveIntlIcuSuffix<D>, locale?: LocaleOf<M>]
): string;
export function trans<
M extends Message<TranslationsType, LocaleType>,
M extends Message<TranslationsType, LocaleType> | string,
D extends DomainsOf<M>,
P extends ParametersOf<M, D>
>(
Expand All @@ -130,6 +134,10 @@ export function trans<
locale = getLocale() as LocaleOf<M>;
}

if (typeof message === 'string') {
message = getRegisteredMessage(message, domain);
}

if (typeof message.translations === 'undefined') {
return message.id;
}
Expand Down Expand Up @@ -166,3 +174,29 @@ export function trans<

return message.id;
}

export function registerDomain(domainTranslations: RegisteredTranslationsType) {
for (const [domainName, translationsByLocale] of Object.entries(domainTranslations)) {
_registeredTranslations[domainName] = translationsByLocale;
}
}

function getRegisteredMessage(key: string, domain: string): Message<TranslationsType, LocaleType>
{
let message: Message<TranslationsType, LocaleType> = {id: key, translations: {}};

for (const domainName of [domain, domain+'+intl-icu']) {
if (typeof _registeredTranslations[domainName] === 'undefined') {
continue;
}

for (const [locale, translations] of Object.entries(_registeredTranslations[domainName])) {
if (typeof translations[key] !== 'undefined') {
message.translations[domainName] ??= {};
message.translations[domainName][locale] = translations[key];
}
}
}

return message;
}
Loading

0 comments on commit a370cb7

Please sign in to comment.