0

So I want to set the output of the translation into a variable in batch. Is it possible to do that? I do have powershell on my computer as well. For Example, from hello to Japanese translation I want the output (hello in Japanese) to be set as a variable.

Code:

@echo off
chcp 923
echo Translate text to Japanese
set /p word="Enter word: "
:: code here to set output from google translate api to variable %output%
echo Result: %output%
pause
exit

also is the chcp necessary?

Thanks

crazydood
  • 3
  • 2
  • What do you know about [character encoding](https://en.wikipedia.org/wiki/Character_encoding) and the difference between a character encoding using just one byte per character with a [code page](https://en.wikipedia.org/wiki/Code_page) which defines which byte value represents which character and a multi-byte character encoding like [UTF-8](https://en.wikipedia.org/wiki/UTF-8) or [UTF-16](https://en.wikipedia.org/wiki/UTF-16) which are [Unicode](https://en.wikipedia.org/wiki/Unicode) encodings? – Mofi Sep 28 '20 at 05:03
  • Open a [command prompt](https://www.howtogeek.com/235101/) and run `chcp` to get output the code page respectively character encoding as used by Windows command processor by default according to the country configured for your user account on your Windows. The command line `chcp 65001 >nul` which is in real the execution of `%SystemRoot%\System32\chcp.com 65001 >nul` changes the character encoding to UTF-8 which means changing the character encoding from one byte per character to a multi-byte per character encoding. – Mofi Sep 28 '20 at 05:07
  • However, just selecting UTF-8 encoding does not mean that all characters as defined by Unicode can be displayed also in console window. The font configured for the console window must support also the characters to display in console Window. Nearly all fonts support only subsets of the characters defined by Unicode. There is used since Windows 8 the font `Consolas` by default for console windows which supports much more characters than raster font `Terminal` used in older Windows versions by default, but `Consolas` does not support all characters defined by Unicode. – Mofi Sep 28 '20 at 05:12
  • Microsoft describes latest version of font `Consolas` on page about [Consolas font family](https://docs.microsoft.com/en-us/typography/font-list/consolas). I don't see any information that this font supports Japanese characters. See also the comments by __eryksun__ below my answer on [Using another language (code page) in a batch file made for others](https://stackoverflow.com/a/48982681/3074564). I suggest to give up the idea using a batch file for this task interpreted by `cmd.exe` with output written into a console window because of that environment is not designed for Japanese text. – Mofi Sep 28 '20 at 05:18

1 Answers1

0

Try this batch+inline powershell trick. I have used this kind of inlining in my random batch scripts. If you want to see utf8 characters you may need to change a default console font, I have not done that.

Use myscript.bat and should see myscript_output.log and myscript_outputPS.log output files. This does not have any failsafe to handle missing json fields.

Passing powershell variable back to a batch context use Write-Host to output STDOUT. After that use FOR..DO @SET to read output to an environment variable. This does not handle well multiline replys but is fine for a single string values.

You may use for-do trick for catching any .exe console binary output, such as delphi/rust/anything little helper .exe console binary files.

@REM https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=ja&dt=t&q=%E2%80%9Chello%E2%80%9D
@set scriptfolder=%~dp0%
@set script=%~n0

@REM activate utf8 encoding in batch script
@chcp 65001

set retval=
Call :CallUrlPS
@echo retval=%retval%
@echo %retval% > %scriptfolder%%script%_output.log

@GOTO :END
@REM ==================================

:CallUrlPS
@SET retval=
@set cmd=^
  $data = @('',''); ^
  $objRet=Invoke-WebRequest 'https://translate.googleapis.com/translate_a/single?client=gtx^&sl=en^&tl=ja^&dt=t^&q=%%E2%%80%%9Chello%%E2%%80%%9D'; ^
  $data[0]=$objRet.Content; ^
  $data[1]=$objRet.StatusCode; ^
  $data[0]=$data[0].Trim(); ^
  Set-Content -Path '%scriptfolder%%script%_outputPS.log' -Encoding utf8 -Value $data[0]; ^
  $objJson = ConvertFrom-Json $data[0]; ^
  Write-Host $objJson[0][0][0]; ^
  ;
@SET setarg=powershell -NoLogo -Noninteractive -InputFormat none -Command "%cmd%"
@FOR /F "tokens=*" %%a IN ('%setarg%') DO @SET retval=%%a
@GOTO :EOF


:END
pause
Whome
  • 10,181
  • 6
  • 53
  • 65