Skip to content

Commit

Permalink
Merge pull request #17 from flexponsive/fix/key-containg-dot-character
Browse files Browse the repository at this point in the history
fix: trans should translate keys containg dot character
  • Loading branch information
mikield authored Jul 25, 2024
2 parents e0393e7 + 3ace99a commit f0a69d8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
10 changes: 6 additions & 4 deletions dist/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ export const trans = (key, replace, pluralize, config) => {
let translation = null

try {
translation = key
.split('.')
translation =
Lingua.translations[locale].php?.[key] ??
key.split('.')
.reduce((t, i) => t[i] || null, Lingua.translations[locale].php)

if (translation) {
Expand All @@ -16,8 +17,9 @@ export const trans = (key, replace, pluralize, config) => {
}

try {
translation = key
.split('.')
translation =
Lingua.translations[locale].json?.[key] ??
key.split('.')
.reduce((t, i) => t[i] || null, Lingua.translations[locale].json)

if (translation) {
Expand Down
13 changes: 12 additions & 1 deletion tests/trans.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const Lingua = {
},
pl: {
php: {
dashboard: 'Panel'
dashboard: 'Panel',
"Please enter your email address.": "Proszę podaj swój adres email.",
}
}
}
Expand All @@ -40,6 +41,16 @@ test('trans is translating nested object', () => {
expect(trans('settings.title', {}, false, config)).toBe('Settings')
});

test("trans is translating key containing dot character", () => {
const configPl = {
Lingua: Lingua,
locale: "pl",
};
expect(trans("Please enter your email address.", {}, false, configPl)).toBe(
"Proszę podaj swój adres email."
);
});

test('trans is replacing key', () => {
expect(trans('replace', { user: 'World' }, false, config)).toBe('Welcome, World')
});
Expand Down

0 comments on commit f0a69d8

Please sign in to comment.