Custom extension internationalization

Hi, all.
I have installed language pack like Chinese (Simplified) Language Pack in Theia, all of the default content have been changed to Chinese, but how can custom extension do?

like this: Sample Menu is not internationalized.
In my custom extenions, I do add nls.localize to code like doc said, but still not work.

Hi @navono,

what parts of the documentation do not work for you? Do you mind sharing a bit more information your setup, so we can help you fix this?

Be aware that nls.localize alone does not automatically translate your content, you’ll have to add your translated text as a LocalizationContribution in the backend.

@msujew thank you for your reply.

Yes, I added LocalizationContribution in the backend with translated json file. Full demo please check this repo. nls.localize in sample-menu-contribution.ts and LocalizationContribution in the backend.

I tried to debug, it appears that nls key not found in localization.translations, seems not merge custom extension LocalizationContribution

Hi @navono,

Thank you for sharing an example. So, there are two small issues which prevent this from working. You’re really close:

  1. Your LocalizationContribution is not decorated as @injectable() (imported from @theia/core/shared/inversify)
  2. The require paths are wrong, because your JS files end up in the lib folder, while your localization files are in your src folder. I would generally recommend to put the data folder on the same level as the lib and src folders.

The following will translate your sample menu correctly:

import { injectable } from '@theia/core/shared/inversify';

export class CustomLocalizationContribution implements LocalizationContribution {
	async registerLocalizations(registry: LocalizationRegistry): Promise<void> {

		// Theia uses language codes, e.g. "de" for German
		registry.registerLocalizationFromRequire('en', require('../../../src/backend/data/i18n/nls.en.json'));
		registry.registerLocalizationFromRequire('zh-cn', require('../../../src/backend/data/i18n/nls.zh-cn.json'));
1 Like

@msujew Thank you for reply. Didn’t notice injectable annotation and load path.