Skip to content

Commit 4d834cf

Browse files
authored
Merge pull request #190 from Staffbase/cc-sbc-client-sdk-add-getusercontentlanguage-method
feat: add getusercontentlanguage to client sdk
2 parents 4edc9cf + c7cd832 commit 4d834cf

File tree

11 files changed

+83
-5
lines changed

11 files changed

+83
-5
lines changed

docs/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ To run the tests a simple `# yarn jest` command in the root directory will suffi
4343

4444
## License
4545

46-
Copyright 2018 Staffbase GmbH.
46+
Copyright 2024 Staffbase GmbH.
4747

4848
Licensed under the Apache License, Version 2.0: <http://www.apache.org/licenses/LICENSE-2.0>
4949

@@ -54,8 +54,8 @@ Licensed under the Apache License, Version 2.0: <http://www.apache.org/licenses/
5454
</td>
5555
<td>
5656
<b>Staffbase GmbH</b>
57-
<br />Staffbase is an internal communications platform built to revolutionize the way you work and unite your company. Staffbase is hiring: <a href="https://jobs.staffbase.com" target="_blank" rel="noreferrer">jobs.staffbase.com</a>
58-
<br /><a href="https://github.com/Staffbase" target="_blank" rel="noreferrer">GitHub</a> | <a href="https://staffbase.com/" target="_blank" rel="noreferrer">Website</a> | <a href="https://jobs.staffbase.com" target="_blank" rel="noreferrer">Jobs</a>
57+
<br />Staffbase is an internal communications platform built to revolutionize the way you work and unite your company. Staffbase is hiring: <a href="https://staffbase.com/jobs/" target="_blank" rel="noreferrer">jobs.staffbase.com</a>
58+
<br /><a href="https://github.com/Staffbase" target="_blank" rel="noreferrer">GitHub</a> | <a href="https://staffbase.com/" target="_blank" rel="noreferrer">Website</a> | <a href="https://staffbase.com/jobs/" target="_blank" rel="noreferrer">Jobs</a>
5959
</td>
6060
</tr>
6161
</table>

docs/api.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ should be used.</p>
5050
<dt><a href="#getPreferredContentLocale">getPreferredContentLocale(content)</a> ⇒ <code>Promise.&lt;string&gt;</code></dt>
5151
<dd><p>Gets the chosen language from a given content object</p>
5252
</dd>
53+
<dt><a href="#getUserContentLocale">getUserContentLocale()</a> ⇒ <code>Promise.&lt;any&gt;</code></dt>
54+
<dd><p>Get the current user&#39;s content locale, fallback to branch default locale</p>
55+
</dd>
5356
</dl>
5457

5558
<a name="openNativeShareDialog"></a>
@@ -178,3 +181,9 @@ Gets the chosen language from a given content object
178181
getPreferredContentLocale(['de_DE', 'en_EN']) // => 'de_DE'
179182
getPreferredContentLocale({'de_DE': {1,'eins'}, 'en_EN': {1: 'one'}}) // => 'de_DE'
180183
```
184+
<a name="getUserContentLocale"></a>
185+
186+
## getUserContentLocale() ⇒ <code>Promise.&lt;any&gt;</code>
187+
Get the current user's content locale, fallback to branch default locale
188+
189+
**Kind**: global function

docs/usage.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,17 @@ As a developer you can request various informations from the Staffbase app.
164164
}
165165
```
166166

167+
1. `getUserContentLocale` -> string
168+
169+
the content locale which is set for current user. Fallback is the branch default locale
170+
171+
```js
172+
// example for user with german content locale in an english app
173+
getUserContentLocale().then(function (locale) {
174+
console.log(locale); // 'de_DE'
175+
})
176+
```
177+
167178
### Invoking native methods
168179

169180
With the SDK you can invoke methods, which are in the scope of the native app.

resources/index.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,11 @@ <h5>
613613
>
614614
<pre id="isBranchDefaultLang"></pre>
615615
<br />
616+
<strong
617+
>getUserContentLocale() - User Content Locale:</strong
618+
>
619+
<pre id="isUserContentLocale"></pre>
620+
<br />
616621
<strong>getContentLanguages() - Content Languages:</strong>
617622
<pre id="isContentLangs"></pre>
618623
<br />
@@ -799,6 +804,19 @@ <h5>Native Share Dialog</h5>
799804
error
800805
);
801806
});
807+
PluginSDK.getUserContentLocale()
808+
.then(function (isUserContentLocale) {
809+
console.log("User Content Language: ", isUserContentLocale);
810+
document.getElementById(
811+
"isUserContentLocale"
812+
).textContent = JSON.stringify(isUserContentLocale, undefined, 2);
813+
})
814+
.catch(function (error) {
815+
console.warn(
816+
"Something went wrong with getUserContentLocale(): ",
817+
error
818+
);
819+
});
802820
PluginSDK.getContentLanguages()
803821
.then(function (isContentLangs) {
804822
console.log("Content Languages: ", isContentLangs);

src/lib/app.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,13 @@ export const getPreferredContentLocale = async (content) => {
148148
log.debug('app/getPreferredContentLocale');
149149
return sendMessage(cmd.prefContentLang, content);
150150
};
151+
152+
/**
153+
* Get the default content language configured for the branch.
154+
*
155+
* @return {Promise<Object>}
156+
*/
157+
export const getUserContentLocale = async () => {
158+
log.debug('app/getUserContentLocale');
159+
return sendMessage(cmd.langInfos).then((res) => res.userContentLocale);
160+
};

src/lib/connection/commands.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export const commands = {
2222
nativeShare: 'nativeShareDialog',
2323
langInfos: 'getLanguageInfos',
2424
branchDefaultLang: 'getBranchDefaultLanguage',
25-
prefContentLang: 'getPreferredContentLocale'
25+
prefContentLang: 'getPreferredContentLocale',
26+
userContentLocale: 'getUserContentLocale'
2627
};
2728

2829
/**

src/lib/connection/connector/fallback-handlers.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,15 @@ export const getPreferredContentLocale = (content) => {
163163
return keys[index] || keys[0];
164164
}
165165
};
166+
167+
/**
168+
* Get the current user's content locale, fallback to branch default locale
169+
*
170+
* @return {String} the user's content locale
171+
*/
172+
export const getUserContentLocale = () => {
173+
log.debug('fallback/getUserContentLocale');
174+
const locale = getBranchDefaultLanguage().locale;
175+
176+
return locale;
177+
};

src/lib/connection/connector/fallback.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ export const sendMessage = async (cmd, ...payload) => {
6868
return fallbacks.getBranchDefaultLanguage();
6969
case action.prefContentLang:
7070
return fallbacks.getPreferredContentLocale.apply(null, payload);
71+
case action.userContentLocale:
72+
return fallbacks.getUserContentLocale();
7173
case action.nativeUpload:
7274
case action.nativeShare:
7375
return fallbacks.unSupported(cmd);

src/main.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ export const getContentLanguages = async () => app.getContentLanguages();
117117
*/
118118
export const getPreferredContentLocale = async (content) => app.getPreferredContentLocale(content);
119119

120+
/**
121+
* Get the current user's content locale, fallback to branch default locale
122+
* @function
123+
* @return {Promise<any>}
124+
*/
125+
export const getUserContentLocale = async () => app.getUserContentLocale();
126+
120127
/**
121128
* Open a share dialog on native devices
122129
*

test/lib/app.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ const langInfos = {
2323
branchLanguages: branchLanguages,
2424
branchDefaultLanguage: branchLanguages.de,
2525
deviceLanguage: branchLanguages.en,
26-
contentLanguages: branchLanguages
26+
contentLanguages: branchLanguages,
27+
userContentLocale: branchLanguages.en.locale
2728
};
2829

2930
const mockVersion = '3.6-dev';
@@ -80,6 +81,11 @@ describe('app', () => {
8081
expect(await App.getPreferredContentLocale(['de_DE', 'EN_US'])).toEqual('de_DE');
8182
});
8283

84+
it('should get the current user content locale', async () => {
85+
messageStub.changeMsg(langInfoMsg);
86+
expect(await App.getUserContentLocale()).toEqual(langInfos.userContentLocale);
87+
});
88+
8389
it('should open links', async () => {
8490
messageStub.changeMsg(['SUCCESS', 0, true]);
8591
expect(await App.openLink('https://test.de')).toEqual(true);

0 commit comments

Comments
 (0)