Skip to content

Commit a75d996

Browse files
committed
simplify get language code
1 parent a7a8722 commit a75d996

7 files changed

+21
-50
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/IdentityOperationExecutor.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from '__test__/support/helpers/requests';
1010
import { updateIdentityModel } from '__test__/support/helpers/setup';
1111
import { ExecutionResult } from 'src/core/types/operation';
12+
import { UnknownOpError } from 'src/shared/errors/common';
1213
import type { MockInstance } from 'vitest';
1314
import { OPERATION_NAME } from '../constants';
1415
import { RebuildUserService } from '../modelRepo/RebuildUserService';
@@ -82,11 +83,7 @@ describe('IdentityOperationExecutor', () => {
8283
// with invalid ops
8384
const ops = [setAliasOp, deleteAliasOp, someOp];
8485
const result = executor._execute(ops);
85-
await expect(() => result).rejects.toThrow(
86-
`Unrecognized operation(s)! Attempted operations:\n${JSON.stringify(
87-
ops,
88-
)}`,
89-
);
86+
await expect(() => result).rejects.toThrow(UnknownOpError(ops));
9087

9188
// with both set and delete alias ops
9289
const ops2 = [setAliasOp, deleteAliasOp];

src/core/executors/LoginUserOperationExecutor.test.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
updatePropertiesModel,
2525
} from '__test__/support/helpers/setup';
2626
import { getPushToken, setPushToken } from 'src/shared/database/subscription';
27+
import { UnknownOpError } from 'src/shared/errors/common';
2728
import {
2829
NotificationType,
2930
SubscriptionType,
@@ -97,9 +98,7 @@ describe('LoginUserOperationExecutor', () => {
9798
// with invalid ops
9899
const ops = [someOp];
99100
const result = executor._execute(ops);
100-
await expect(() => result).rejects.toThrow(
101-
`Unrecognized operation: ${someOp._name}`,
102-
);
101+
await expect(() => result).rejects.toThrow(UnknownOpError(someOp));
103102
});
104103

105104
describe('create user', () => {
@@ -124,9 +123,7 @@ describe('LoginUserOperationExecutor', () => {
124123
const someOp = new SomeOperation();
125124
const ops2 = [loginOp, transferSubOp, someOp];
126125
const res2 = executor._execute(ops2);
127-
await expect(res2).rejects.toThrow(
128-
`Unrecognized operation: ${someOp._name}`,
129-
);
126+
await expect(res2).rejects.toThrow(UnknownOpError(someOp));
130127
});
131128

132129
test('can create user if there is no onesignal id or externalId', async () => {

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/core/executors/RefreshUserOperationExecutor.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from '__test__/support/helpers/requests';
1616
import { updateIdentityModel } from '__test__/support/helpers/setup';
1717
import { setPushToken } from 'src/shared/database/subscription';
18+
import { UnknownOpError } from 'src/shared/errors/common';
1819
import {
1920
NotificationType,
2021
SubscriptionType,
@@ -83,11 +84,7 @@ describe('RefreshUserOperationExecutor', () => {
8384
const ops = [someOp];
8485

8586
const result = executor._execute(ops);
86-
await expect(() => result).rejects.toThrow(
87-
`Unrecognized operation(s)! Attempted operations:\n${JSON.stringify(
88-
ops,
89-
)}`,
90-
);
87+
await expect(() => result).rejects.toThrow(UnknownOpError(ops));
9188
});
9289

9390
describe('getUser', () => {

src/core/executors/SubscriptionOperationExecutor.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
} from '__test__/support/helpers/requests';
1818
import { setupSubscriptionModel } from '__test__/support/helpers/setup';
1919
import { setPushToken } from 'src/shared/database/subscription';
20+
import { UnknownOpError } from 'src/shared/errors/common';
2021
import {
2122
NotificationType,
2223
SubscriptionType,
@@ -100,9 +101,7 @@ describe('SubscriptionOperationExecutor', () => {
100101
const ops = [someOp];
101102

102103
const res1 = executor._execute(ops);
103-
await expect(() => res1).rejects.toThrow(
104-
`Unrecognized operation: ${ops[0]}`,
105-
);
104+
await expect(() => res1).rejects.toThrow(UnknownOpError(ops));
106105

107106
const deleteOp = new DeleteSubscriptionOperation(
108107
APP_ID,

src/core/executors/UpdateUserOperationExecutor.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
setUpdateUserResponse,
77
} from '__test__/support/helpers/requests';
88
import { updateIdentityModel } from '__test__/support/helpers/setup';
9+
import { UnknownOpError } from 'src/shared/errors/common';
910
import { type MockInstance } from 'vitest';
1011
import { OPERATION_NAME } from '../constants';
1112
import { RebuildUserService } from '../modelRepo/RebuildUserService';
@@ -70,9 +71,7 @@ describe('UpdateUserOperationExecutor', () => {
7071
const ops = [someOp];
7172

7273
const result = executor._execute(ops);
73-
await expect(() => result).rejects.toThrow(
74-
`Unrecognized operation: ${ops[0]}`,
75-
);
74+
await expect(() => result).rejects.toThrow(UnknownOpError(someOp));
7675
});
7776

7877
describe('SetPropertyOperation', () => {

0 commit comments

Comments
 (0)