-
Notifications
You must be signed in to change notification settings - Fork 276
/
translation.ts
57 lines (53 loc) 路 1.64 KB
/
translation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { PageConfig, URLExt } from '@jupyterlab/coreutils';
import { JSONObject } from '@lumino/coreutils';
/**
* A fake locale to retrieve all the language packs.
*/
const ALL = 'all';
/**
* A class to handle requests to /api/translations
*/
export class Translation {
/**
* Get the translation data for the given locale
* @param locale The locale
* @returns
*/
async get(locale: string): Promise<JSONObject> {
const apiURL = URLExt.join(
PageConfig.getBaseUrl(),
`api/translations/${locale}.json`,
);
try {
const response = await fetch(apiURL);
const json = JSON.parse(await response.text());
if (this._prevLocale !== ALL && locale === ALL) {
// TODO: fix this logic upstream?
// the upstream translation plugin relies on the comparison between
// the display name and the native name to enable or disable the commands:
// https://github.com/jupyterlab/jupyterlab/blob/befa831ffef36321b87f352a48fbe2439df6c872/packages/translation-extension/src/index.ts#L117
const prev = this._prevLocale;
json.data[prev].displayName = json.data[prev].nativeName;
if (prev !== 'en') {
json.data['en'].displayName = `${json.data['en'].nativeName} (default)`;
}
}
this._prevLocale = locale;
return json;
} catch (e) {
if (locale) {
return {
data: {},
message: `Language pack '${locale}' not installed!`,
};
}
return {
data: {
en: { displayName: 'English', nativeName: 'English' },
},
message: '',
};
}
}
private _prevLocale = '';
}