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
Use in batch file
SETLOCAL EnableDelayedExpansion
@CHCP 65001 >NUL
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.