Skip to content

Commit

Permalink
refactor(l10n/localize): export transformLocalizedFieldToLocalizedStr…
Browse files Browse the repository at this point in the history
…ing (#1904)

* refactor(l10n/localize): export transformLocalizedFieldToLocalizedString, update Options type

* chore: add changeset

* fix(localize): provide formattedLocalizedFallback when `locale` is no defined

* fix: revert removal of return type
  • Loading branch information
adnasa committed Nov 30, 2020
1 parent 63a0b9e commit 1ef03e3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-kiwis-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-frontend/l10n': patch
---

export `transformLocalizedFieldToLocalizedString`, update `localize` to allow `locale` to be `null`
1 change: 1 addition & 0 deletions packages/l10n/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ export {

export {
applyTransformedLocalizedFields,
transformLocalizedFieldToLocalizedString,
formatLocalizedString,
} from './localize';
30 changes: 20 additions & 10 deletions packages/l10n/src/localize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,32 @@ export const formatLocalizedString = <Input extends Record<string, unknown>>(
fallback = '',
}: FormatLocalizedStringOptions<Input>
): string => {
// if there is no entity to be localized or the field does not exist on the
// entity, then return the fallback
if (!entity || !entity[key]) return fallback;
const localizedString = entity[key] as LocalizedString;
const fallbackLocale = findFallbackLocale(localizedString, fallbackOrder);

const formattedLocalizedFallback = fallbackLocale
? formatLocalizedFallbackHint(
localizedString[fallbackLocale],
fallbackLocale
)
: fallback;

// GIVEN no `locale`
// THEN return formattedFallback by fallbackOrder
if (!locale) return formattedLocalizedFallback;

// GIVEN locale
// AND there is a value on `localizedString`
// THEN return value
if (localizedString[locale]) return localizedString[locale];

// see if we can fallback to the primary locale, eg. de for de-AT
// GIVEN locale
// AND there is a value on primary locale
// THEN return value on primary locale
const primaryLocale = locale && getPrimaryLocale(locale);
if (localizedString[primaryLocale]) return localizedString[primaryLocale];

const fallbackLanguage = findFallbackLocale(localizedString, fallbackOrder);
return fallbackLanguage
? formatLocalizedFallbackHint(
localizedString[fallbackLanguage],
fallbackLanguage
)
: fallback;
// use formattedFallback by fallbackOrder as last resort
return formattedLocalizedFallback;
};
2 changes: 1 addition & 1 deletion packages/l10n/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type FieldNameTranformationMapping = {

export type FormatLocalizedStringOptions<T> = {
key: keyof T;
locale: string;
locale: string | null;
fallbackOrder?: string[];
fallback?: string;
};

1 comment on commit 1ef03e3

@vercel
Copy link

@vercel vercel bot commented on 1ef03e3 Nov 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.