File tree 1 file changed +7
-2
lines changed
1 file changed +7
-2
lines changed Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ class Internationalization<T extends string> {
16
16
/**
17
17
* ClientInternationalization utility package for working with languages
18
18
*
19
- * @param supportedLanguages { Array<string> } - List of all supported languages
19
+ * @param supportedLanguages { Array<string> } - List of all supported languages as two-letter codes (ISO 639-1)
20
20
* @param fallbackLanguage { string } - Fallback language if no language matches
21
21
* @param cookieName { string } - Optional: Set a preferred cookie name
22
22
*/
@@ -43,9 +43,14 @@ class Internationalization<T extends string> {
43
43
// If the DOM isn't accessible the website may be rendered on a server
44
44
if ( canUseDOM ( ) ) {
45
45
// Get browser language
46
- const browserLanguage =
46
+ let browserLanguage =
47
47
( navigator . languages && navigator . languages [ 0 ] ) || navigator . language
48
48
49
+ // Check if the browser language is in the format of xx-XX, extract the first part
50
+ if ( / ^ [ a - z A - z ] { 2 } \- / . test ( browserLanguage ) ) {
51
+ browserLanguage = browserLanguage . substring ( 0 , 2 )
52
+ }
53
+
49
54
return (
50
55
this . supportedLanguages . find ( ( lang ) =>
51
56
isEqualCaseInsensitive ( lang , browserLanguage ) ,
You can’t perform that action at this time.
0 commit comments