How can I use accented characters in the echo command in Batch?

后端 未结 2 1159
陌清茗
陌清茗 2021-01-22 19:17

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

相关标签:
2条回答
  • 2021-01-22 19:35

    Use in batch file

    SETLOCAL EnableDelayedExpansion

    @CHCP 65001 >NUL

    0 讨论(0)
  • 2021-01-22 19:53
    • 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.

    0 讨论(0)
提交回复
热议问题