Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hellow! how could we achieve localization with the esm import ? #1322

Closed
FAKER-A opened this issue Feb 21, 2019 · 6 comments
Closed

hellow! how could we achieve localization with the esm import ? #1322

FAKER-A opened this issue Feb 21, 2019 · 6 comments

Comments

@FAKER-A
Copy link

FAKER-A commented Feb 21, 2019

hellow! how could we achieve localization with the esm import ?

@blutorange
Copy link
Contributor

I don't think this is supported yet, especially if you want to support multiple languages (see also #784). I made a JSF/PrimeFaces wrapper for monacoeditor. That framework does not work well with require.js / loader.js so I used the ESM version. What I ended up doing essentially boils down to:

  • Create a bundle with webpack
  • Most localized text are hardcoded, but are still passed through a small helper function in vscode/nls.js. That function is also given an i18n key, but just returns the localized text. So I use the NormalModuleWebpackReplacementPlugin to replace that file with my own version that reads the localized text from a dynamic source.
  • Now we need to get the translations for each i18n key. I pull from github.com/Microsoft/vscode-loc. It consists of many files, so I process them and write all i18n strings to one JS file.
  • The i18n keys from that repo include the source file name of the file they are used in. This is not the case in the ESM version. So I preprocess the ESM code and replace all calls to nls.localize("my.key", args) with nls.localize("source/file.js", "my.key", args).
  • Some translations are missing (see Some strings not localized #822), but fortunately, for most keys, localized texts are available with another i18n key. This can be fixed while generating the locales by mapping to these keys.
  • To make the localized strings available to the webworkers as well, we can just wrap the webworker by creating our own webworker that first loads the locale, then proceeds to load the original webworker.

@FAKER-A
Copy link
Author

FAKER-A commented Feb 25, 2019

I don't think this is supported yet, especially if you want to support multiple languages (see also #784). I made a JSF/PrimeFaces wrapper for monacoeditor. That framework does not work well with require.js / loader.js so I used the ESM version. What I ended up doing essentially boils down to:

  • Create a bundle with webpack
  • Most localized text are hardcoded, but are still passed through a small helper function in vscode/nls.js. That function is also given an i18n key, but just returns the localized text. So I use the NormalModuleWebpackReplacementPlugin to replace that file with my own version that reads the localized text from a dynamic source.
  • Now we need to get the translations for each i18n key. I pull from github.com/Microsoft/vscode-loc. It consists of many files, so I process them and write all i18n strings to one JS file.
  • The i18n keys from that repo include the source file name of the file they are used in. This is not the case in the ESM version. So I preprocess the ESM code and replace all calls to nls.localize("my.key", args) with nls.localize("source/file.js", "my.key", args).
  • Some translations are missing (see Some strings not localized #822), but fortunately, for most keys, localized texts are available with another i18n key. This can be fixed while generating the locales by mapping to these keys.
  • To make the localized strings available to the webworkers as well, we can just wrap the webworker by creating our own webworker that first loads the locale, then proceeds to load the original webworker.

Thank you for your recovery

@FAKER-A
Copy link
Author

FAKER-A commented Feb 26, 2019

I already have a solution, so I closed this issue.

@FAKER-A FAKER-A closed this as completed Feb 26, 2019
@s97712
Copy link

s97712 commented Aug 5, 2019

I already have a solution, so I closed this issue.

can you share you solution?

@blutorange
Copy link
Contributor

Afaik this is not supported officially. To elaborate on my comment above, I made a quick working example here: esm version of monaco editor with i18n support. I don't know about your use case, but perhaps this can help you get started.

@wang12124468
Copy link

You can use monaco-editor-esm-webpack-plugin to localize in esm.
Thanks to @blutorange's mind, I write a plugin to solute it.

@vscodebot vscodebot bot locked and limited conversation to collaborators Oct 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants