Skip to content

Commit

Permalink
Merge pull request #1171 from ElwynVdb/user-language-functions
Browse files Browse the repository at this point in the history
Introduce language functions on User model
  • Loading branch information
sherwinski authored Jan 6, 2025
2 parents ba6db97 + 42e28a7 commit 0e299a6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
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 @@ -363,4 +363,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

0 comments on commit 0e299a6

Please sign in to comment.