@jupyterlab/translation-extension 4.0.0-alpha.19 → 4.0.0-alpha.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +9 -8
- package/src/index.ts +195 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jupyterlab/translation-extension",
|
|
3
|
-
"version": "4.0.0-alpha.
|
|
3
|
+
"version": "4.0.0-alpha.20",
|
|
4
4
|
"description": "JupyterLab - Translation services",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"jupyter",
|
|
@@ -28,7 +28,8 @@
|
|
|
28
28
|
"lib/**/*.{d.ts,eot,gif,html,jpg,js,js.map,json,png,svg,woff2,ttf}",
|
|
29
29
|
"schema/**/*.{json,}",
|
|
30
30
|
"style/**/*.{css,eot,gif,html,jpg,json,png,svg,woff2,ttf}",
|
|
31
|
-
"style/index.js"
|
|
31
|
+
"style/index.js",
|
|
32
|
+
"src/**/*.{ts,tsx}"
|
|
32
33
|
],
|
|
33
34
|
"scripts": {
|
|
34
35
|
"build": "tsc",
|
|
@@ -38,15 +39,15 @@
|
|
|
38
39
|
"watch": "tsc -w"
|
|
39
40
|
},
|
|
40
41
|
"dependencies": {
|
|
41
|
-
"@jupyterlab/application": "^4.0.0-alpha.
|
|
42
|
-
"@jupyterlab/apputils": "^4.0.0-alpha.
|
|
43
|
-
"@jupyterlab/mainmenu": "^4.0.0-alpha.
|
|
44
|
-
"@jupyterlab/settingregistry": "^4.0.0-alpha.
|
|
45
|
-
"@jupyterlab/translation": "^4.0.0-alpha.
|
|
42
|
+
"@jupyterlab/application": "^4.0.0-alpha.20",
|
|
43
|
+
"@jupyterlab/apputils": "^4.0.0-alpha.20",
|
|
44
|
+
"@jupyterlab/mainmenu": "^4.0.0-alpha.20",
|
|
45
|
+
"@jupyterlab/settingregistry": "^4.0.0-alpha.20",
|
|
46
|
+
"@jupyterlab/translation": "^4.0.0-alpha.20"
|
|
46
47
|
},
|
|
47
48
|
"devDependencies": {
|
|
48
49
|
"rimraf": "~3.0.0",
|
|
49
|
-
"typescript": "~
|
|
50
|
+
"typescript": "~5.0.0-beta"
|
|
50
51
|
},
|
|
51
52
|
"publishConfig": {
|
|
52
53
|
"access": "public"
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/* ----------------------------------------------------------------------------
|
|
2
|
+
| Copyright (c) Jupyter Development Team.
|
|
3
|
+
| Distributed under the terms of the Modified BSD License.
|
|
4
|
+
|----------------------------------------------------------------------------*/
|
|
5
|
+
/**
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
* @module translation-extension
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
ILabShell,
|
|
12
|
+
JupyterFrontEnd,
|
|
13
|
+
JupyterFrontEndPlugin
|
|
14
|
+
} from '@jupyterlab/application';
|
|
15
|
+
import { Dialog, ICommandPalette, showDialog } from '@jupyterlab/apputils';
|
|
16
|
+
import { IMainMenu } from '@jupyterlab/mainmenu';
|
|
17
|
+
import { ISettingRegistry } from '@jupyterlab/settingregistry';
|
|
18
|
+
import {
|
|
19
|
+
ITranslator,
|
|
20
|
+
requestTranslationsAPI,
|
|
21
|
+
TranslationManager
|
|
22
|
+
} from '@jupyterlab/translation';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Translation plugins
|
|
26
|
+
*/
|
|
27
|
+
const PLUGIN_ID = '@jupyterlab/translation-extension:plugin';
|
|
28
|
+
|
|
29
|
+
const translator: JupyterFrontEndPlugin<ITranslator> = {
|
|
30
|
+
id: '@jupyterlab/translation:translator',
|
|
31
|
+
autoStart: true,
|
|
32
|
+
requires: [JupyterFrontEnd.IPaths, ISettingRegistry],
|
|
33
|
+
optional: [ILabShell],
|
|
34
|
+
provides: ITranslator,
|
|
35
|
+
activate: async (
|
|
36
|
+
app: JupyterFrontEnd,
|
|
37
|
+
paths: JupyterFrontEnd.IPaths,
|
|
38
|
+
settings: ISettingRegistry,
|
|
39
|
+
labShell: ILabShell | null
|
|
40
|
+
) => {
|
|
41
|
+
const setting = await settings.load(PLUGIN_ID);
|
|
42
|
+
const currentLocale: string = setting.get('locale').composite as string;
|
|
43
|
+
let stringsPrefix: string = setting.get('stringsPrefix')
|
|
44
|
+
.composite as string;
|
|
45
|
+
const displayStringsPrefix: boolean = setting.get('displayStringsPrefix')
|
|
46
|
+
.composite as boolean;
|
|
47
|
+
stringsPrefix = displayStringsPrefix ? stringsPrefix : '';
|
|
48
|
+
const serverSettings = app.serviceManager.serverSettings;
|
|
49
|
+
const translationManager = new TranslationManager(
|
|
50
|
+
paths.urls.translations,
|
|
51
|
+
stringsPrefix,
|
|
52
|
+
serverSettings
|
|
53
|
+
);
|
|
54
|
+
await translationManager.fetch(currentLocale);
|
|
55
|
+
|
|
56
|
+
// Set translator to UI
|
|
57
|
+
if (labShell) {
|
|
58
|
+
labShell.translator = translationManager;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
Dialog.translator = translationManager;
|
|
62
|
+
|
|
63
|
+
return translationManager;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Initialization data for the extension.
|
|
69
|
+
*/
|
|
70
|
+
const langMenu: JupyterFrontEndPlugin<void> = {
|
|
71
|
+
id: PLUGIN_ID,
|
|
72
|
+
requires: [ISettingRegistry, ITranslator],
|
|
73
|
+
optional: [IMainMenu, ICommandPalette],
|
|
74
|
+
autoStart: true,
|
|
75
|
+
activate: (
|
|
76
|
+
app: JupyterFrontEnd,
|
|
77
|
+
settings: ISettingRegistry,
|
|
78
|
+
translator: ITranslator,
|
|
79
|
+
mainMenu: IMainMenu | null,
|
|
80
|
+
palette: ICommandPalette | null
|
|
81
|
+
) => {
|
|
82
|
+
const trans = translator.load('jupyterlab');
|
|
83
|
+
const { commands } = app;
|
|
84
|
+
let currentLocale: string;
|
|
85
|
+
/**
|
|
86
|
+
* Load the settings for this extension
|
|
87
|
+
*
|
|
88
|
+
* @param setting Extension settings
|
|
89
|
+
*/
|
|
90
|
+
function loadSetting(setting: ISettingRegistry.ISettings): void {
|
|
91
|
+
// Read the settings and convert to the correct type
|
|
92
|
+
currentLocale = setting.get('locale').composite as string;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
settings
|
|
96
|
+
.load(PLUGIN_ID)
|
|
97
|
+
.then(setting => {
|
|
98
|
+
// Read the settings
|
|
99
|
+
loadSetting(setting);
|
|
100
|
+
document.documentElement.lang = (currentLocale ?? '').replace('_', '-');
|
|
101
|
+
|
|
102
|
+
// Listen for your plugin setting changes using Signal
|
|
103
|
+
setting.changed.connect(loadSetting);
|
|
104
|
+
|
|
105
|
+
// Create a languages menu
|
|
106
|
+
const languagesMenu = mainMenu
|
|
107
|
+
? mainMenu.settingsMenu.items.find(
|
|
108
|
+
item =>
|
|
109
|
+
item.type === 'submenu' &&
|
|
110
|
+
item.submenu?.id === 'jp-mainmenu-settings-language'
|
|
111
|
+
)?.submenu
|
|
112
|
+
: null;
|
|
113
|
+
|
|
114
|
+
let command: string;
|
|
115
|
+
|
|
116
|
+
const serverSettings = app.serviceManager.serverSettings;
|
|
117
|
+
// Get list of available locales
|
|
118
|
+
requestTranslationsAPI<any>('', '', {}, serverSettings)
|
|
119
|
+
.then(data => {
|
|
120
|
+
for (const locale in data['data']) {
|
|
121
|
+
const value = data['data'][locale];
|
|
122
|
+
const displayName = value.displayName;
|
|
123
|
+
const nativeName = value.nativeName;
|
|
124
|
+
const toggled = displayName === nativeName;
|
|
125
|
+
const label = toggled
|
|
126
|
+
? `${displayName}`
|
|
127
|
+
: `${displayName} - ${nativeName}`;
|
|
128
|
+
|
|
129
|
+
// Add a command per language
|
|
130
|
+
command = `jupyterlab-translation:${locale}`;
|
|
131
|
+
commands.addCommand(command, {
|
|
132
|
+
label: label,
|
|
133
|
+
caption: label,
|
|
134
|
+
isEnabled: () => !toggled,
|
|
135
|
+
isVisible: () => true,
|
|
136
|
+
isToggled: () => toggled,
|
|
137
|
+
execute: () => {
|
|
138
|
+
return showDialog({
|
|
139
|
+
title: trans.__('Change interface language?'),
|
|
140
|
+
body: trans.__(
|
|
141
|
+
'After changing the interface language to %1, you will need to reload JupyterLab to see the changes.',
|
|
142
|
+
label
|
|
143
|
+
),
|
|
144
|
+
buttons: [
|
|
145
|
+
Dialog.cancelButton({ label: trans.__('Cancel') }),
|
|
146
|
+
Dialog.okButton({ label: trans.__('Change and reload') })
|
|
147
|
+
]
|
|
148
|
+
}).then(result => {
|
|
149
|
+
if (result.button.accept) {
|
|
150
|
+
setting
|
|
151
|
+
.set('locale', locale)
|
|
152
|
+
.then(() => {
|
|
153
|
+
window.location.reload();
|
|
154
|
+
})
|
|
155
|
+
.catch(reason => {
|
|
156
|
+
console.error(reason);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
// Add the language command to the menu
|
|
164
|
+
if (languagesMenu) {
|
|
165
|
+
languagesMenu.addItem({
|
|
166
|
+
command,
|
|
167
|
+
args: {}
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (palette) {
|
|
172
|
+
palette.addItem({
|
|
173
|
+
category: trans.__('Display Languages'),
|
|
174
|
+
command
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
})
|
|
179
|
+
.catch(reason => {
|
|
180
|
+
console.error(`Available locales errored!\n${reason}`);
|
|
181
|
+
});
|
|
182
|
+
})
|
|
183
|
+
.catch(reason => {
|
|
184
|
+
console.error(
|
|
185
|
+
`The jupyterlab translation extension appears to be missing.\n${reason}`
|
|
186
|
+
);
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Export the plugins as default.
|
|
193
|
+
*/
|
|
194
|
+
const plugins: JupyterFrontEndPlugin<any>[] = [translator, langMenu];
|
|
195
|
+
export default plugins;
|