Skip to content

Commit

Permalink
optimize getBestMatchFromCodes for i18next/i18next-browser-languageDe…
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Mar 8, 2024
1 parent 4685bea commit f7b8176
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 23.10.1

- optimize getBestMatchFromCodes for https://github.com/i18next/i18next-browser-languageDetector/issues/281

## 23.10.0

- Add 'isInitializing' property so we're able to detect init() was already called [2141](https://github.com/i18next/i18next/issues/2141)
Expand Down
3 changes: 2 additions & 1 deletion i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,8 @@
found = this.options.supportedLngs.find(supportedLng => {
if (supportedLng === lngOnly) return supportedLng;
if (supportedLng.indexOf('-') < 0 && lngOnly.indexOf('-') < 0) return;
if (supportedLng.indexOf(lngOnly) === 0) return supportedLng;
if (supportedLng.indexOf('-') > 0 && lngOnly.indexOf('-') < 0 && supportedLng.substring(0, supportedLng.indexOf('-')) === lngOnly) return supportedLng;
if (supportedLng.indexOf(lngOnly) === 0 && lngOnly.length > 1) return supportedLng;
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion i18next.min.js

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions src/LanguageUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,16 @@ class LanguageUtil {
found = this.options.supportedLngs.find((supportedLng) => {
if (supportedLng === lngOnly) return supportedLng;
if (supportedLng.indexOf('-') < 0 && lngOnly.indexOf('-') < 0) return;
if (supportedLng.indexOf(lngOnly) === 0) return supportedLng;
if (
supportedLng.indexOf('-') > 0 &&
lngOnly.indexOf('-') < 0 &&
supportedLng.substring(0, supportedLng.indexOf('-')) === lngOnly
)
return supportedLng;
if (supportedLng.indexOf(lngOnly) === 0 && lngOnly.length > 1) return supportedLng;
});
});
}

// if nothing found, use fallbackLng
if (!found) found = this.getFallbackCodes(this.options.fallbackLng)[0];

Expand Down
2 changes: 2 additions & 0 deletions test/runtime/languageUtils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ describe('LanguageUtils', () => {
{ args: [['ru', 'en-GB']], expected: 'en' },
{ args: [['de-CH']], expected: 'de-DE' },
{ args: [['ru']], expected: 'en' },
{ args: [['c']], expected: 'en' },
{ args: [['e']], expected: 'en' },
{ args: [[]], expected: 'en' },
];

Expand Down

0 comments on commit f7b8176

Please sign in to comment.