Skip to content

Commit

Permalink
Move nls localization namespace to common
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew committed Oct 21, 2021
1 parent 38e2ba3 commit 4f1decb
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<a name="breaking_changes_1.19.0">[Breaking Changes:](#breaking_changes_1.19.0)</a>

- [core] moved `nls` localization namespace from `browser` to `common`. [#10153](https://github.com/eclipse-theia/theia/pull/10153)
- [output] moved `output-channel` from `common` to `browser` [#10154](https://github.com/eclipse-theia/theia/pull/10154)
- [output] moved `output-preferences` from `common` to `browser` [#10154](https://github.com/eclipse-theia/theia/pull/10154)
- [view-container] `ViewContainerPart` constructor takes new 2 parameters: `originalContainerId` and `originalContainerTitle`. The existing `viewContainerId` parameter has been renamed to `currentContainerId` to enable drag & drop views. [#9644](https://github.com/eclipse-theia/theia/pull/9644)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ FrontendApplicationConfigProvider.set(${this.prettyStringify(this.pck.props.fron
const { ThemeService } = require('@theia/core/lib/browser/theming');
ThemeService.get().loadUserTheme();
const nls = require('@theia/core/lib/browser/nls');
const nls = require('@theia/core/lib/browser/nls-loader');
// nls translations MUST be loaded before requiring any code that uses them
module.exports = nls.loadTranslations().then(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/browser/common-frontend-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import { AuthenticationService } from './authentication-service';
import { FormatType } from './saveable';
import { QuickInputService, QuickPick, QuickPickItem } from './quick-input';
import { AsyncLocalizationProvider } from '../common/i18n/localization';
import { nls } from './nls';
import { nls } from '../common/nls';

export namespace CommonMenus {

Expand Down
26 changes: 26 additions & 0 deletions packages/core/src/browser/nls-loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/********************************************************************************
* Copyright (C) 2021 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { nls } from '../common/nls';
import { Endpoint } from './endpoint';

export async function loadTranslations(): Promise<void> {
if (nls.locale) {
const endpoint = new Endpoint({ path: '/i18n/' + nls.locale }).getRestUrl().toString();
const response = await fetch(endpoint);
nls.localization = await response.json();
}
}
3 changes: 1 addition & 2 deletions packages/core/src/common/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import { injectable, inject, named } from 'inversify';
import { Event, Emitter, WaitUntilEvent } from './event';
import { Disposable, DisposableCollection } from './disposable';
import { ContributionProvider } from './contribution-provider';
// eslint-disable-next-line @theia/runtime-import-check
import { nls } from '../browser/nls';
import { nls } from './nls';

/**
* A command is a unique identifier of a function
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export * from './strings';
export * from './application-error';
export * from './lsp-types';
export * from './contribution-filter';
export * from './nls';

import { environment } from '@theia/application-package/lib/environment';
export { environment };
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,33 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { Localization } from '../common/i18n/localization';
import { Endpoint } from './endpoint';

let localization: Localization | undefined;

function format(message: string, args: string[]): string {
let result = message;
if (args.length > 0) {
result = message.replace(/\{(\d+)\}/g, (match, rest) => {
const index = rest[0];
const arg = args[index];
let replacement = match;
if (typeof arg === 'string') {
replacement = arg;
} else if (typeof arg === 'number' || typeof arg === 'boolean' || !arg) {
replacement = String(arg);
}
return replacement;
});
}
return result;
}
import { Localization } from './i18n/localization';

export namespace nls {

export let localization: Localization | undefined;

export const localeId = 'localeId';

export const locale = typeof window === 'object' && window && window.localStorage.getItem('localeId') || undefined;
export const locale = typeof window === 'object' && window && window.localStorage.getItem(localeId) || undefined;

function format(message: string, args: string[]): string {
let result = message;
if (args.length > 0) {
result = message.replace(/\{(\d+)\}/g, (match, rest) => {
const index = rest[0];
const arg = args[index];
let replacement = match;
if (typeof arg === 'string') {
replacement = arg;
} else if (typeof arg === 'number' || typeof arg === 'boolean' || !arg) {
replacement = String(arg);
}
return replacement;
});
}
return result;
}

export function localize(key: string, defaultValue: string, ...args: string[]): string {
let value = defaultValue;
Expand All @@ -55,11 +54,3 @@ export namespace nls {
return format(value, args);
}
}

export async function loadTranslations(): Promise<void> {
if (nls.locale) {
const endpoint = new Endpoint({ path: '/i18n/' + nls.locale }).getRestUrl().toString();
const response = await fetch(endpoint);
localization = await response.json();
}
}
2 changes: 1 addition & 1 deletion packages/monaco/src/browser/monaco-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

/* eslint-disable @typescript-eslint/no-explicit-any */

import { nls } from '@theia/core/lib/browser/nls';
import { nls } from '@theia/core/lib/common/nls';

export function loadVsRequire(context: any): Promise<any> {
// Monaco uses a custom amd loader that over-rides node's require.
Expand Down

0 comments on commit 4f1decb

Please sign in to comment.