简体   繁体   English

如何在摩纳哥编辑器上更改显示语言

[英]How to change display language on monaco editor

don`t know how to change display language from javascript to such as swift(or something else) 不知道如何将显示语言从javascript更改为swift(或其他)

I installed using "npm install monaco-editor" command 我使用“ npm install monaco-editor”命令安装

    <script src="node_modules/monaco-editor/min/vs/loader.js"></script>
    <script>
    var editor;
    require.config({ paths: { 'vs': 'node_modules/monaco-editor/min/vs' 
    }});
    require(['vs/editor/editor.main'], function() {

        editor = 
    monaco.editor.create(document.getElementById('container'), {
            value: [
                'function x() {',
                '\tconsole.log("Hello world!");',
                '}'
            ].join('\n'),
            language: 'javascript',
            theme: "vs-dark"
        });
    });


    function showMessage() {
        var text = editor.getValue();
        monaco.editor.setModelLanguage(editor.getModel(), "swift")
        editor.updateOptions({
            language: "objective-c"
        });
        alert(text);
    }
</script>

I expect the after running showMessage function, display codes changes to objective-c from javascript. 我希望在运行showMessage函数之后,显示代码从javascript更改为Objective-c。 and actually error shows nothing. 而实际上错误什么也没显示。 does anyone know how to do it ?? 有谁知道怎么做?

I'm not quite sure what you're trying to do, but I made this little function: 我不太确定您要做什么,但是我做了这个小功能:

const changeLang = lang => {
    monaco.editor.setModelLanguage(editor.getModel(), lang);
    console.log(`model language was changed to ${editor.getModel().getLanguageIdentifier().language}`);
}

It changes the language and prints the new model language every time for me. 它会每次更改语言并打印新的模型语言。

Your line editor.updateOptions({language: "objective-c"}) will not do anything. 您的行editor.updateOptions({language: "objective-c"})不会执行任何操作。 If you look at the docs , you'll see that ITextModelUpdateOptions only takes indentSize , insertSpaces , tabSize , or trimAutoWhitespace . 如果您查看文档 ,将会看到ITextModelUpdateOptions仅采用indentSizeinsertSpacestabSizetrimAutoWhitespace

It looks like you might have been looking at the first couple answers of this github issue to make that line. 看来您可能一直在看这个github问题的前几个答案来完成这一行。 That very first answer given is not correct. 给出的第一个答案是不正确的。

You do text = editor.getValue() at the start of your function and alert(text) at the end, but text never changes. 您可以在函数的开始处执行text = editor.getValue()并在函数的末尾执行alert(text) ,但是text永远不会改变。 The model language will only affect the appearance, not the content. 模型语言只会影响外观,而不会影响内容。 If you were only trying to get the static content, then the way you wrote it is fine. 如果您只是尝试获取静态内容,那么编写它的方式就很好。 But if you were expecting it to change, it won't. 但是,如果您期望它会改变,那就不会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM