-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalize.ts
55 lines (48 loc) · 1.73 KB
/
localize.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { getCurrentLocale } from './CurrentLocale.ts';
import { interpolate } from './interpolate.ts';
import type { LocalizationFailure } from './LocalizationFailure.ts';
import type { LocalizationOptions } from './LocalizationOptions.ts';
import type { LocalizedUnit } from './LocalizedUnit.ts';
/**
The `localize()` function returns the localized string for the given `msg` and `param` in the current locale. The 'current locale' is determined by the `getCurrentLocale()` function, whose implementation depends on the configuration that the consuming app must provide, unless the default implementation is sufficient.
This function has to be generic to make the end-to-end static typing work.
*/
export const localize = <Locales extends string>(
unit: LocalizedUnit<Locales>,
options: LocalizationOptions<Locales> = {},
): string =>
{
const { locale, interpolationParameters, escapeParameters, skipInterpolation } = options;
const resolvedLocale = locale ?? getCurrentLocale(unit);
const localizedValue = unit[resolvedLocale];
if (localizedValue === undefined || localizedValue === null)
{
const failure: LocalizationFailure<Locales> = {
locale: resolvedLocale,
unit: unit,
error: new Error('localized value not found'),
};
if (options.failureHandler)
{
options.failureHandler(failure);
}
else
{
// FIXME: invoke the failure handler here
console.error('localization failure', failure);
}
return '[error: localized value not found]';
}
if (skipInterpolation)
{
return localizedValue;
}
return interpolate<Locales>(
localizedValue,
unit,
resolvedLocale,
interpolationParameters,
escapeParameters ?? true,
options.failureHandler,
);
};