Skip to content

Commit 7751b60

Browse files
feat(core): add locale and no-translate attributes to html element on startup (#16966)
- Set 'lang' attribute based on detected locale. - Prevent auto-translation by setting 'translate="no"' and adding '.notranslate' class. This ensures better i18n support and avoids issues with unintended UI translation. Signed-off-by: Alexander Taran <a.taran@outlook.com>
1 parent b968eb9 commit 7751b60

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

packages/core/src/browser/common-frontend-contribution.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,16 @@ export class CommonFrontendContribution implements FrontendApplicationContributi
291291
}
292292

293293
onStart(): void {
294+
this.setupHtmlLanguageAttributes(document.documentElement);
294295
this.storageService.getData<{ recent: Command[] }>(RECENT_COMMANDS_STORAGE_KEY, { recent: [] })
295296
.then(tasks => this.commandRegistry.recent = tasks.recent);
296297
}
297298

299+
protected setupHtmlLanguageAttributes(element: HTMLElement): void {
300+
nls.setHtmlLang(element);
301+
nls.setHtmlNoTranslate(element);
302+
}
303+
298304
onStop(): void {
299305
const recent = this.commandRegistry.recent;
300306
this.storageService.setData<{ recent: Command[] }>(RECENT_COMMANDS_STORAGE_KEY, { recent });

packages/core/src/browser/secondary-window-handler.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { Emitter } from '../common/event';
2323
import { isSecondaryWindow, SecondaryWindowRootWidget, SecondaryWindowService } from './window/secondary-window-service';
2424
import { KeybindingRegistry } from './keybinding';
2525
import { MAIN_AREA_ID, TheiaDockPanel } from './shell/theia-dock-panel';
26+
import { nls } from '../common/nls';
2627

2728
/** Widgets to be contained inside a DockPanel in the secondary window. */
2829
class SecondaryWindowDockPanelWidget extends SecondaryWindowRootWidget {
@@ -180,6 +181,8 @@ export class SecondaryWindowHandler {
180181
// See https://html.spec.whatwg.org/multipage/dom.html#document.title
181182
newWindow.document.title = `${widget.title.label}${mainWindowTitle}`;
182183

184+
this.setupHtmlLanguageAttributes(newWindow.document.documentElement);
185+
183186
const element = newWindow.document.getElementById('widget-host');
184187
if (!element) {
185188
console.error('Could not find dom element to attach to in secondary window');
@@ -218,6 +221,11 @@ export class SecondaryWindowHandler {
218221
});
219222
}
220223

224+
protected setupHtmlLanguageAttributes(element: HTMLElement): void {
225+
nls.setHtmlLang(element);
226+
nls.setHtmlNoTranslate(element);
227+
}
228+
221229
private onWidgetRemove(widget: Widget, newWindow: Window, rootWidget: SecondaryWindowRootWidget): void {
222230
// Close the window if the widget is disposed, e.g. by a command closing all widgets.
223231
this.onWillRemoveWidgetEmitter.fire([widget, newWindow]);

packages/core/src/common/nls.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,31 @@ export namespace nls {
6868
export function setLocale(id: string): void {
6969
window.localStorage.setItem(localeId, id);
7070
}
71+
72+
/**
73+
* Sets the 'lang' attribute on the given HTML element based on the current locale.
74+
* If no locale is set, defaults to 'en' (English).
75+
* Typically used with document.documentElement (the <html> tag) to set the page language.
76+
*
77+
* @param element The HTML element to set the language attribute on
78+
*/
79+
export function setHtmlLang(element: HTMLElement): void {
80+
const lang = locale?.split('_').at(0) || 'en';
81+
element.setAttribute('lang', lang);
82+
}
83+
84+
/**
85+
* Sets the 'translate' attribute to 'no' and adds the 'notranslate' class
86+
* to the given HTML element. This prevents translation tools from translating
87+
* the content of the element.
88+
* Typically used with document.documentElement (the <html> tag) to disable page translation.
89+
*
90+
* @param element The HTML element to set translation attributes on
91+
*/
92+
export function setHtmlNoTranslate(element: HTMLElement): void {
93+
element.setAttribute('translate', 'no');
94+
element.classList.add('notranslate');
95+
}
7196
}
7297

7398
interface NlsKeys {

0 commit comments

Comments
 (0)