Skip to content

Commit be0b15f

Browse files
committed
simplify get language code
1 parent c3e4598 commit be0b15f

File tree

4 files changed

+12
-28
lines changed

4 files changed

+12
-28
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
},
8585
{
8686
"path": "./build/releases/OneSignalSDK.page.es6.js",
87-
"limit": "45.364 kB",
87+
"limit": "45.322 kB",
8888
"gzip": true
8989
},
9090
{

src/core/executors/LoginUserOperationExecutor.ts

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -335,34 +335,16 @@ export class LoginUserOperationExecutor implements IOperationExecutor {
335335
}
336336
}
337337

338-
const TRADITIONAL_CHINESE_LANGUAGE_TAG = ['tw', 'hant'];
339338
const SIMPLIFIED_CHINESE_LANGUAGE_TAG = ['cn', 'hans'];
340339

341340
const getLanguage = () => {
342-
let languageTag = navigator.language;
343-
if (languageTag) {
344-
languageTag = languageTag.toLowerCase();
345-
const languageSubtags = languageTag.split('-');
346-
if (languageSubtags[0] == 'zh') {
347-
// The language is zh-?
348-
// We must categorize the language as either zh-Hans (simplified) or zh-Hant (traditional);
349-
// OneSignal only supports these two Chinese variants
350-
for (const traditionalSubtag of TRADITIONAL_CHINESE_LANGUAGE_TAG) {
351-
if (languageSubtags.indexOf(traditionalSubtag) !== -1) {
352-
return 'zh-Hant';
353-
}
354-
}
355-
for (const simpleSubtag of SIMPLIFIED_CHINESE_LANGUAGE_TAG) {
356-
if (languageSubtags.indexOf(simpleSubtag) !== -1) {
357-
return 'zh-Hans';
358-
}
359-
}
360-
return 'zh-Hant'; // Return Chinese traditional by default
361-
} else {
362-
// Return the language subtag (it can be three characters, so truncate it down to 2 just to be sure)
363-
return languageSubtags[0].substring(0, 2);
364-
}
365-
} else {
366-
return 'en';
367-
}
341+
const languageTag = navigator.language?.toLowerCase();
342+
if (!languageTag) return 'en';
343+
344+
const [primary, ...subtags] = languageTag.split('-');
345+
if (primary !== 'zh') return primary.substring(0, 2);
346+
347+
if (SIMPLIFIED_CHINESE_LANGUAGE_TAG.some((tag) => subtags.includes(tag)))
348+
return 'zh-Hans';
349+
return 'zh-Hant';
368350
};

src/onesignal/PushSubscriptionNamespace.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from 'src/shared/listeners';
1515
import { IDManager } from 'src/shared/managers/IDManager';
1616
import type { EventsMap } from 'src/shared/services/types';
17+
import type { UserSubscription } from 'src/shared/subscriptions/types';
1718
import { EventListenerBase } from '../page/userModel/EventListenerBase';
1819
import { isCompleteSubscriptionObject } from '../shared/managers/utils';
1920
import {

src/onesignal/UserNamespace.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { UserEventsMap } from 'src/shared/services/types';
2+
import type { UserSubscription } from 'src/shared/subscriptions/types';
23
import type { UserChangeEvent } from '../page/models/UserChangeEvent';
34
import { EventListenerBase } from '../page/userModel/EventListenerBase';
45
import Emitter from '../shared/libraries/Emitter';

0 commit comments

Comments
 (0)