Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce language functions on User model #1171

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions __test__/unit/user/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,20 @@ describe('User tests', () => {

expect(tags).toBe(tagsSample);
});


test('getLanguage should return the correct user language', async () => {
await TestEnvironment.initialize();

const languageSample = 'fr'

const propertyModel = getDummyPropertyOSModel();
propertyModel.set('language', languageSample);
OneSignal.coreDirector.add(ModelName.Properties, propertyModel);

const user = User.createOrGetInstance();
const language = user.getLanguage();

expect(language).toBe(languageSample);
});
});
23 changes: 23 additions & 0 deletions src/onesignal/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,4 +341,27 @@ export default class User {

return OneSignal.coreDirector.getPropertiesModel()?.data?.tags;
}

public setLanguage(language: string): void {
logMethodCall('setLanguage', { language });

if (typeof language !== 'string') {
throw new InvalidArgumentError(
'language',
InvalidArgumentReason.WrongType,
);
}

if (!language) {
throw new InvalidArgumentError('language', InvalidArgumentReason.Empty);
}

const propertiesModel = OneSignal.coreDirector.getPropertiesModel();
propertiesModel?.set('language', language);
}

public getLanguage(): string {
logMethodCall('getLanguage');
return OneSignal.coreDirector.getPropertiesModel()?.data?.language;
}
}
8 changes: 8 additions & 0 deletions src/onesignal/UserNamespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ export default class UserNamespace extends EventListenerBase {
return this._currentUser?.getTags() || {};
}

public setLanguage(language: string): void {
this._currentUser?.setLanguage(language);
}

public getLanguage(): string {
return this._currentUser?.getLanguage() || '';
}

addEventListener(
event: 'change',
listener: (userChange: UserChangeEvent) => void,
Expand Down