Transformations @localizer/transform 1.1.1
Sometimes, you may need to perform additional processing on the results of formatting or translation. Common use cases include altering the character case. Since character case transformations can depend on locale-specific rules, the library offers a suite of transformation utilities that seamlessly integrate with its core functionality.
The primary method for applying transformations is the transform()
function. This utility allows you to modify the output of a Localizable
object by applying a sequence of transformations. Each transformation is applied in the order specified, enabling flexible and powerful customization of localized content.
import { getLocalizer } from '@localizer/core';
import { countryName } from '@localizer/format';
import { transform } from '@localizer/transform';
const localizer = getLocalizer('en-US');
const someLocalizable = countryName('FI');
const transformed = transform(someLocalizable, [upperCase]);
console.log(localizer(transformed));
FINLAND
You can also apply transform()
directly to a value formatter. This creates a new value formatter that automatically applies the specified transformations to its output:
import { getLocalizer } from '@localizer/core';
import { countryName } from '@localizer/format';
import { transform } from '@localizer/transform';
const localizer = getLocalizer('en-US');
const transformedValueFormatter = transform(countryName, [upperCase]);
console.log(localizer(transformedValueFormatter('FI')));
FINLAND
The second argument of transform()
method is an array of transformers. These transformers are applied sequentially to modify the result, offering a flexible way to customize localized content.
String transformations
Locale Transformations
Unlike string transformations, locale transformations enable you to override the locale used during the localization process. These transformations are particularly useful when you need to ensure consistent formatting or behavior across different locales, regardless of the user's selected language or region.
Using primary locale preview
This transformer enables the use of the primary language for a country instead of the provided locale. This can be particularly useful in scenarios where consistent data formatting is preferred, regardless of the selected spoken language.
const fiFI = getLocalizer('fi-FI');
const svFI = getLocalizer('sv-FI');
const localizedDate = date(new Date());
console.log(fiFI(localizedDate));
console.log(svFI(localizedDate));
14.7.2025
2025-07-14
In Finland, the standard localization for the Swedish language may lead to unexpected results for some users. The usePrimaryLocale
transformer helps ensure consistency by applying the primary locale for the region, making the localized output more intuitive and contextually appropriate.
const fiFI = getLocalizer('fi-FI');
const svFI = getLocalizer('sv-FI');
const fixedDate = transform(date, [usePrimaryLocale]);
const localizedDate = date(new Date());
const localizedDate = fixedDate(new Date());
console.log(fiFI(localizedDate));
console.log(svFI(localizedDate));
14.7.2025
14.7.2025