Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix logging in i18n #445

Merged
merged 11 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,18 @@ Changelog

_Note: Gaps between patch versions are faulty, broken or test releases._

## v3.101.1 (2024-10-07)

#### :bug: Bug Fix

* `core/prelude/i18n/helpers`
* Fix logging bug in `pluralizeText`.
* Add logging info in i18n helpers.

## v3.101.0 (2024-09-25)

#### :boom: breaking change
#### :boom: Breaking Change

* `core/prelude/i18n/helpers`
* changed `i18n` translations format.
* added `intl` support for pluralization.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "lib/core/index.js",
"typings": "index.d.ts",
"license": "MIT",
"version": "3.101.0",
"version": "3.101.1",
"author": "kobezzza <[email protected]> (https://github.com/kobezzza)",
"repository": {
"type": "git",
Expand Down
9 changes: 8 additions & 1 deletion src/core/prelude/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ Changelog
> - :house: [Internal]
> - :nail_care: [Polish]

## v3.101.1 (2024-10-07)

#### :bug: Bug Fix

* Fix logging bug in `pluralizeText`.
* Add logging info in i18n helpers.

## v3.101.0 (2024-09-25)

#### :boom: breaking change
#### :boom: Breaking Change

* changed `i18n` translations format.
* added `intl` support for pluralization.
Expand Down
34 changes: 23 additions & 11 deletions src/core/prelude/i18n/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import extend from 'core/prelude/extend';
import langPacs, { Translation, PluralTranslation } from 'lang';

import { locale } from 'core/prelude/i18n/const';
import type { I18nOpts, PluralizationCount } from 'core/prelude/i18n/interface';
import type { I18nOpts, PluralizationCount, I18nMeta } from 'core/prelude/i18n/interface';

/** @see [[i18n]] */
extend(globalThis, 'i18n', i18nFactory);
Expand Down Expand Up @@ -54,18 +54,19 @@ export function i18nFactory(
const
key = Object.isString(value) ? value : value[0],
correctKeyset = keysetNames.find((keysetName) => langPacs[resolvedLocale]?.[keysetName]?.[key]),
translateValue = langPacs[resolvedLocale]?.[correctKeyset ?? '']?.[key];
translateValue = langPacs[resolvedLocale]?.[correctKeyset ?? '']?.[key],
meta: I18nMeta = {language: resolvedLocale, keyset: correctKeyset, key};

if (translateValue != null && translateValue !== '') {
return resolveTemplate(translateValue, params, {pluralRules});
return resolveTemplate(translateValue, params, {pluralRules, meta});
}

logger.error(
'Translation for the given key is not found',
`Key: ${key}, KeysetNames: ${keysetNames.join(', ')}, LocaleName: ${resolvedLocale}, available locales: ${Object.keys(langPacs).join(', ')}`
);

return resolveTemplate(key, params, {pluralRules});
return resolveTemplate(key, params, {pluralRules, meta});
};
}

Expand All @@ -74,6 +75,7 @@ export function i18nFactory(
*
* @param value - a string for the default case, or an array of strings for the plural case
* @param params - a dictionary with parameters for internationalization
* @params [opts] - additional options for current translation
*
* @example
* ```typescript
Expand All @@ -93,7 +95,7 @@ export function i18nFactory(
*/
export function resolveTemplate(value: Translation, params?: I18nParams, opts: I18nOpts = {}): string {
const
template = Object.isPlainObject(value) ? pluralizeText(value, params?.count, opts.pluralRules) : value;
template = Object.isPlainObject(value) ? pluralizeText(value, params?.count, opts) : value;

return template.replace(/{([^}]+)}/g, (_, key) => {
if (params?.[key] == null) {
Expand All @@ -110,7 +112,7 @@ export function resolveTemplate(value: Translation, params?: I18nParams, opts: I
*
* @param pluralTranslation - list of translation variants
* @param count - the value on the basis of which the form of pluralization will be selected
* @param rules - Intl plural rules for selected locale
* @params [opts] - additional options for current translation
*
* @example
* ```typescript
Expand All @@ -120,16 +122,18 @@ export function resolveTemplate(value: Translation, params?: I18nParams, opts: I
* many: {count} products,
* zero: {count} products,
* other: {count} products,
* }, 5, new Intl.PluralRulse('en'));
* }, 5, {pluralRules: new Intl.PluralRulse('en')});
*
* console.log(result); // '{count} products'
* ```
*/
export function pluralizeText(
pluralTranslation: PluralTranslation,
count: CanUndef<PluralizationCount>,
rules: CanUndef<Intl.PluralRules>
opts: I18nOpts = {}
): string {
const {pluralRules, meta} = opts;

let normalizedCount;

if (Object.isNumber(count)) {
Expand All @@ -144,16 +148,24 @@ export function pluralizeText(
}

if (normalizedCount == null) {
logger.error('Invalid value of the `count` parameter for string pluralization', `String: ${pluralTranslation[0]}`);
logger.error(
'Invalid value of the `count` parameter for string pluralization',
`Count: ${count}, Key: ${meta?.key}, Language: ${meta?.language}, Keyset: ${meta?.keyset}`
);

normalizedCount = 1;
}

const
pluralFormName = getPluralFormName(normalizedCount, rules),
pluralFormName = getPluralFormName(normalizedCount, pluralRules),
translation = pluralTranslation[pluralFormName];

if (translation == null) {
logger.error(`Plural form ${pluralFormName} doesn't exist.`, `String: ${pluralTranslation[0]}`);
logger.error(
`Plural form ${pluralFormName} doesn't exist.`,
`Key: ${meta?.key}, Language: ${meta?.language}, Keyset: ${meta?.keyset}`
);

return pluralTranslation.one;
}

Expand Down
8 changes: 8 additions & 0 deletions src/core/prelude/i18n/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ export interface LocaleKVStorage {

export type PluralizationCount = StringPluralizationForms | number;

export interface I18nMeta {
language: string;
key: string;
keyset?: string;
}

export interface I18nOpts {
pluralRules?: Intl.PluralRules;
meta?: I18nMeta;
}

15 changes: 13 additions & 2 deletions src/core/prelude/i18n/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('core/prelude/i18n', () => {
describe('text pluralization', () => {
it('using pluralization constants to choose the right form', () => {
formNames.forEach((form) => {
expect(pluralizeText(forms, form, rules)).toBe(forms[form]);
expect(pluralizeText(forms, form, {pluralRules: rules})).toBe(forms[form]);
});
});

Expand All @@ -53,7 +53,18 @@ describe('core/prelude/i18n', () => {
};

[forms.one, forms.other, forms.other, forms.other].forEach((form, index) => {
expect(pluralizeText(input.forms, input.count[index], rules)).toBe(form);
expect(pluralizeText(input.forms, input.count[index], {pluralRules: rules})).toBe(form);
});
});

it('return one form if correct form does not exists', () => {
const input = {
forms,
count: [1, 2, 100, 0]
};

[forms.one, forms.one, forms.one, forms.one].forEach((form, index) => {
expect(pluralizeText({one: input.forms.one}, input.count[index], {pluralRules: rules})).toBe(form);
});
});
});
Expand Down
Loading