Skip to content

Commit

Permalink
Improved flatten translations
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Nov 29, 2023
1 parent 7e8d9aa commit 8402e4d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Updated dependencies (#641)
- Updated `piral-cli` compatibility with Bun as runtime
- Updated generated code to fully use ES2020
- Added support for nested translations in `piral-translate` (#648)
- Added support for Angular 17 in `piral-ng`
- Added `piral-react` as converter for React
- Added possibility to publish emulator as a website (#644)
Expand Down
31 changes: 16 additions & 15 deletions src/plugins/piral-translate/src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import type { PiralPlugin } from 'piral-core';
import { createActions } from './actions';
import { Localizer } from './localize';
import { DefaultPicker } from './default';
import { PiletLocaleApi, LocalizationMessages, Localizable, PiralSelectLanguageEvent, NestedLocalizationMessages } from './types';
import { flattenTranslations } from './flatten-translations';
import type {
PiletLocaleApi,
LocalizationMessages,
Localizable,
PiralSelectLanguageEvent,
AnyLocalizationMessages,
} from './types';

export interface TranslationFallback {
(key: string, language: string): string;
Expand All @@ -23,7 +29,7 @@ export interface LocaleConfig {
* Sets the default (global) localization messages.
* @default {}
*/
messages?: LocalizationMessages | NestedLocalizationMessages;
messages?: AnyLocalizationMessages;
/**
* Sets the default language to use.
*/
Expand Down Expand Up @@ -72,18 +78,15 @@ export function createLocaleApi(localizer: Localizable = setupLocalizer()): Pira
return (api) => {
let localTranslations: LocalizationMessages = {};

const setTranslations = (messages: AnyLocalizationMessages) => {
localTranslations = flattenTranslations(messages);
};

return {
addTranslations(messages, isOverriding = true) {
const messagesToMerge = messages;

if (isOverriding) {
messagesToMerge.unshift(localizer.messages);
} else {
messagesToMerge.push(localizer.messages);
}

this.setTranslations(
deepmerge.all(messagesToMerge),
const current = localizer.messages;
setTranslations(
deepmerge.all<AnyLocalizationMessages>(isOverriding ? [current, ...messages] : [...messages, current]),
);
},
getCurrentLanguage(cb?: (l: string) => void): any {
Expand All @@ -100,9 +103,7 @@ export function createLocaleApi(localizer: Localizable = setupLocalizer()): Pira

return selected;
},
setTranslations(messages) {
localTranslations = flattenTranslations(messages);
},
setTranslations,
getTranslations() {
return localTranslations;
},
Expand Down
39 changes: 12 additions & 27 deletions src/plugins/piral-translate/src/flatten-translations.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
import { LocalizationMessages, NestedLocalizationMessages } from './types';

export function flattenTranslations(messages: LocalizationMessages | NestedLocalizationMessages): LocalizationMessages {
return Object.fromEntries(
Object
.entries(messages)
.map(([ language, translations ]) => {
return [
language,
flat(translations)
]
})
);
}
import type { LocalizationMessages, NestedLocalizationMessages } from './types';

function flat(source: Record<string, unknown>): Record<string, string> {
const target: Record<string, string> = {};

flatten(source, target);

return target;
}

Expand All @@ -29,18 +14,18 @@ function flatten(source: any, target: Record<string, string>, prop = '') {
}

if (typeof source === 'object' && source !== null) {
Object
.keys(source)
.forEach((key) => {
flatten(
source[key],
target,
prop
? `${prop}.${key}`
: key
);
});
Object.keys(source).forEach((key) => {
flatten(source[key], target, prop ? `${prop}.${key}` : key);
});

return;
}
}

export function flattenTranslations(messages: LocalizationMessages | NestedLocalizationMessages): LocalizationMessages {
return Object.fromEntries(
Object.entries(messages).map(([language, translations]) => {
return [language, flat(translations)];
}),
);
}
6 changes: 4 additions & 2 deletions src/plugins/piral-translate/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ export interface NestedLocalizationMessages {
[lang: string]: NestedTranslations;
}

export type AnyLocalizationMessages = LocalizationMessages | NestedLocalizationMessages;

export interface PiletLocaleApi {
/**
* Adds a list of translations to the existing translations.
Expand All @@ -143,7 +145,7 @@ export interface PiletLocaleApi {
* @param messagesList The list of messages that extend the existing translations
* @param [isOverriding=true] Indicates whether the new translations overwrite the existing translations
*/
addTranslations(messagesList: (LocalizationMessages | NestedLocalizationMessages)[], isOverriding?: boolean): void;
addTranslations(messagesList: Array<AnyLocalizationMessages>, isOverriding?: boolean): void;
/**
* Gets the currently selected language directly.
*/
Expand All @@ -165,7 +167,7 @@ export interface PiletLocaleApi {
* The translations will be exclusively used for retrieving translations for the pilet.
* @param messages The messages to use as translation basis.
*/
setTranslations(messages: LocalizationMessages | NestedLocalizationMessages): void;
setTranslations(messages: AnyLocalizationMessages): void;
/**
* Gets the currently provided translations by the pilet.
*/
Expand Down

0 comments on commit 8402e4d

Please sign in to comment.