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

How to translate the editor UI to a different language? #629

Open
atilara opened this issue Jun 21, 2024 · 4 comments
Open

How to translate the editor UI to a different language? #629

atilara opened this issue Jun 21, 2024 · 4 comments

Comments

@atilara
Copy link

atilara commented Jun 21, 2024

Describe the solution you'd like
I want to use the editor localized in a language not officially supported in the translation files, in this case, brazillian portuguese.

Describe alternatives you've considered
VS Code already has a portuguese translation, as you can find here. Already tried to use it with a library called monaco-editor-nls, but it didn't work when I tried like this:

'use client';
import Box from '@mui/material/Box';
import { loader } from '@monaco-editor/react';
import { useEffect } from 'react';
import pt_BR from '@/vscode-language-pack-pt-BR/translations/main.i18n.json';
import { setLocaleData } from 'monaco-editor-nls';

export default function Poc() {
  useEffect(() => {
    setLocaleData(pt_BR.contents);
    loader.init().then((monaco) => {
      monaco.editor.create(
        document.getElementById('monaco-editor') as HTMLElement,
        {
          value: '// another comment',
        }
      );
    });
  }, []);

  return (
    <Box
      display="flex"
      flexDirection={'column'}
      alignItems="center"
      justifyContent="center"
    >
      <div
        id="monaco-editor"
        style={{
          width: '70%',
          height: '500px',
        }}
      />
    </Box>
  );
}

Do you have any ideas of what I can do?

@suren-atoyan
Copy link
Owner

Hi @atilara

Please check this

@atilara
Copy link
Author

atilara commented Jun 23, 2024

Hey, thanks for answering. Already did. It works fine for the supported languages, the thing is: there is not a portuguese translation present on the files. That is why I ask if it's possible to add translation for a unsupported language.

@suren-atoyan
Copy link
Owner

Ah, sorry I misunderstood. To be able to use packages like monaco-editor-nls and @/vscode-language-pack-pt-BR, first you need to import monaco as an npm package and not from the CDN. Please check this section of the documentation.

@atilara
Copy link
Author

atilara commented Jun 25, 2024

Hey, thanks for answering again. I still didn't get it right. Tried the following implementation:

'use client';
import Box from '@mui/material/Box';
import { useEffect } from 'react';
import { setLocaleData } from 'monaco-editor-nls';
// import pt_BR from 'monaco-editor-nls/locale/pt-br.json';
import pt_BR from '@/vscode-language-pack-pt-BR/translations/main.i18n.json';

export default function Poc() {
  useEffect(() => {
    setLocaleData(pt_BR.contents);
    const monaco = require('monaco-editor/esm/vs/editor/editor.api');
    monaco.editor.create(
      document.getElementById('monaco-editor') as HTMLElement,
      {
        value: 'function x() {\n\tconsole.log("Hello world!");\n}',
        language: 'javascript',
        theme: 'vs-dark',
      }
    );
  }, []);

  return (
    <Box
      display="flex"
      flexDirection={'column'}
      alignItems="center"
      justifyContent="center"
    >
      <div
        id="monaco-editor"
        style={{
          width: '70%',
          height: '500px',
        }}
      />
    </Box>
  );
}

The monaco-editor-nls recommends importing monaco after running the setLocaleData(). Already tried importing from monaco-editor, using: const monaco = require('monaco-editor') and it doesn't work, the translation is not applied. Importing from monaco-editor/esm/vs/editor/editor.api doesn't return any errors but keyboard shortcuts like f1 and ctrl + f doens't work so I can't even see if the translation is applied or not.

Do you have any idea of what I can do? Maybe with a different library or implementation form? Is there any way to access i18n running on monaco-editor and apply the translation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants