How can I use accented characters in the echo comm

2019-08-10 04:24发布

Hello people of Stack Overflow. Me and my friend are making a game in the Batch/.bat programming language. Up until this point, we have had no problems that we couldnt fix. When I started translating it into the Spanish version, however, we ran into problems. When I tried to have the program "echo" words in Spanish with accents (e.g. Á, É) it wouldnt work. I had set up a test page which had the program echo "áéíóú" but it came out with something else, I cannot copy and paste it right now. I ran into a similar problem when I tested the Russian/Cyrillic keyboard. I had set the character set to 437 when testing the accents and 1251 when testing the Russian. When I saved it, the special characters even changed in the Bat file code. Is there anything special I need to do to make the special characters appear? Again, I am only trying to use them on the echo command.

2条回答
乱世女痞
2楼-- · 2019-08-10 04:45
  • Save the batch file in UTF-8 without BOM signature and use UTF-8 codepage:

    chcp 65001 >nul
    echo Päivää Привет Hello
    
  • Or make a file with messages for each language and save it in unicode encoding (UTF-16LE):

    en.hello=Hello
    .......other messages
    fi.hello=Päivää
    .......other messages
    

    Then load the translations:

    set LANGUAGE=fi
    ...............
    for /f "tokens=1* delims=." %%a in ('type "%~dp0messages.txt') do (
        if "%%a"=="%LANGUAGE%" set "msg.%%b"
    )
    ...............
    echo %msg.hello%
    

    Now the messages should be displayed correctly by default since the system console codepage usually corresponds to the language set in regional settings.

    Päivää

    You can keep these messages in readable form inside the batch file saved as UTF-8 without BOM signature and use an embedded VBScript/powershell code to extract to messages.txt in UTF16-LE encoding:

    ::msg.en.hello=Hello
    ::msg.fi.hello=Päivää
    powershell -c "Get-Content '%~f0' -encoding UTF8 | Select-String -Pattern '^::msg\.' | ForEach-Object {$_ -replace '::msg.',''} | Set-Content '%temp%\messages.txt' -encoding Unicode"
    

    And then load it as shown above.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-08-10 04:54

Use in batch file

SETLOCAL EnableDelayedExpansion

@CHCP 65001 >NUL

查看更多
登录 后发表回答