Skip to content

Commit 4311771

Browse files
Portal language selector (#2520)
* Create README.md Creation of the README.md file with the description of the component, what it is for, how to use it, and an example * Create language-selector.html Added HTML template for dropdown with flags * Create language-selector.css Added dropdown styling * Create language-selector.js Added client script to update sys_user.language via REST API * Delete Modern Development/Service Portal Widgets/Language Selector/language-selector.js * Create language-selector.client.js Added client script to update sys_user.language via REST API * Create language-selector.server.js Added server script to provide current language
1 parent 3ba5909 commit 4311771

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Language Selector with Flags
2+
3+
A language selector widget for the Portal.
4+
The user can change the instance language without having to leave the Portal.
5+
6+
<img width="255" height="95" alt="image" src="https://github.com/user-attachments/assets/af130ec4-d724-4b07-a38f-afd858b7eba2" />
7+
<img width="234" height="195" alt="image" src="https://github.com/user-attachments/assets/a2de8161-a922-4376-904d-b16f81dcc573" />
8+
9+
10+
## What it does
11+
- Displays a dropdown with flags and language names.
12+
- Automatically updates the user's language in the `sys_user` table.
13+
- Reloads the page to apply the new language immediately.
14+
15+
## Files
16+
- **HTML Template:** renders the dropdown with flag emojis and labels.
17+
- **Client Script:** handles language selection and sends the PATCH request.
18+
- **Server Script:** provides the current user ID and stored language.
19+
20+
## Example
21+
When the user selects **🇪🇸 Spanish**, the widget updates their user record and reloads the Portal in Spanish.
22+
23+
## Prerequisites
24+
- The language selected **must be installed and active** in the instance.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function($http) {
2+
var c = this;
3+
4+
c.languages = [
5+
{ code: 'en', label: 'English', flag: '🇬🇧' },
6+
{ code: 'pb', label: 'Portuguese (Brazil)', flag: '🇧🇷' },
7+
{ code: 'es', label: 'Spanish', flag: '🇪🇸' },
8+
{ code: 'fr', label: 'French', flag: '🇫🇷' },
9+
{ code: 'de', label: 'German', flag: '🇩🇪' },
10+
{ code: 'it', label: 'Italian', flag: '🇮🇹' }
11+
];
12+
13+
c.userId = c.data.user_id;
14+
c.selected = c.data.language || 'en';
15+
16+
c.changeLang = function() {
17+
$http.patch('/api/now/table/sys_user/' + c.userId, { preferred_language: c.selected })
18+
.then(function(response) {
19+
location.reload();
20+
});
21+
};
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.lang-selector {
2+
display: flex;
3+
align-items: center;
4+
gap: 8px;
5+
}
6+
select, button {
7+
padding: 6px 8px;
8+
border-radius: 6px;
9+
border: 1px solid #ccc;
10+
}
11+
button {
12+
cursor: pointer;
13+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<div class="lang-selector">
2+
<select ng-model="c.selected"
3+
ng-options="l.code as (l.flag + ' ' + l.label) for l in c.languages"
4+
ng-change="c.changeLang()">
5+
</select>
6+
</div>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(function() {
2+
var user = gs.getUser();
3+
data.user_id = user.getID();
4+
5+
var grUser = new GlideRecord('sys_user');
6+
if (grUser.get(data.user_id)) {
7+
data.language = grUser.getValue('preferred_language') || 'en';
8+
}
9+
})();

0 commit comments

Comments
 (0)