Skip to content

Commit

Permalink
feat(i18n): improve i18n types
Browse files Browse the repository at this point in the history
  • Loading branch information
AliMD committed Dec 1, 2022
1 parent c090dbb commit cf7d1cb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
28 changes: 23 additions & 5 deletions packages/core/i18n/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ l10n.localeChangeSignal.addListener(
logger.logMethodArgs('localeChange', locale);
l10n.locale = locale;

if (l10n.resourceChangeSignal.value?._localCode !== locale.code) {
if (l10n.resourceChangeSignal.value?._code !== locale.code) {
l10n.resourceChangeSignal.expire();
if (l10n.config.autoFetchResources) {
l10n.resourceChangeSignal.request(locale);
Expand Down Expand Up @@ -124,7 +124,15 @@ l10n.resourceChangeSignal.setProvider(async (locale) => {
});

if (response.ok) {
return response.json();
const resource = (await response.json()) as L10Resource;
if (resource._code == null) {
logger.accident('resourceProvider', 'resource_not_valid', 'Key `_code` not defined in l10n resource', {
url,
resource,
});
throw new Error('resource_not_valid');
}
return resource;
}
else {
throw new Error('fetch_nok');
Expand All @@ -133,6 +141,7 @@ l10n.resourceChangeSignal.setProvider(async (locale) => {
catch (err) {
logger.error('resourceProvider', 'fetch_failed', (err as Error).stack || err, {locale, url});
// TODO: user error signal.
return;
}
});

Expand All @@ -141,8 +150,11 @@ l10n.resourceChangeSignal.setProvider(async (locale) => {
*
* @param key The String_Key of the translation resource to localize.
* @returns The localized string.
*
* return `…` if the translation resource is not yet loaded.
* return `(key)` if the key not defined in the translation resource.
*
* return `{key}` if the key not defined in the translation resource.
*
* return null if the key is null or undefined (for optional input).
*
* @example
Expand All @@ -154,8 +166,11 @@ function localize(key?: null): null;
*
* @param key The String_Key of the translation resource to localize.
* @returns The localized string.
*
* return `…` if the translation resource is not yet loaded.
* return `(key)` if the key not defined in the translation resource.
*
* return `{key}` if the key not defined in the translation resource.
*
* return null if the key is null or undefined (for optional input).
*
* @example
Expand All @@ -167,8 +182,11 @@ function localize(key: string): string;
*
* @param key The String_Key of the translation resource to localize.
* @returns The localized string.
*
* return `…` if the translation resource is not yet loaded.
* return `(key)` if the key not defined in the translation resource.
*
* return `{key}` if the key not defined in the translation resource.
*
* return null if the key is null or undefined (for optional input).
*
* @example
Expand Down
11 changes: 6 additions & 5 deletions packages/core/i18n/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ declare global {
}
}

export type L10Resource = {
[key: string]: string;
_localeCode: `${string}-${string}`;
export type LocalCode = `${Lowercase<string>}-${Uppercase<string>}`;

export type L10Resource = Record<string, string> & {
_code: LocalCode;
};

export type Locale = {
/**
* fa-IR, en-US, ...
*/
code: `${string}-${string}`;
code: LocalCode;

/**
* fa, en, ...
*/
language: string;
language: Lowercase<string>;

/**
* ltr, rtl
Expand Down

0 comments on commit cf7d1cb

Please sign in to comment.