问题
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.
回答1:
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.
回答2:
Use in batch file
SETLOCAL EnableDelayedExpansion
@CHCP 65001 >NUL
来源:https://stackoverflow.com/questions/33723388/how-can-i-use-accented-characters-in-the-echo-command-in-batch