How can I get system locale in Windows 7?
I mean something like: cs_CZ.UTF-8
I tried writing "locale" in the command line but that doesn't work in Windows. Any suggestions?
How can I get system locale in Windows 7?
I mean something like: cs_CZ.UTF-8
I tried writing "locale" in the command line but that doesn't work in Windows. Any suggestions?
There's not a specific command (or at least, not one that I'm aware of) to get this information, but you can find it between those provided by systeminfo.exe
.
If you need the actual locale to conditionally do other things in a batch file you can create a batch file (save a text file as .bat extension) with the following. As a starting point this will print to the command prompt, for example, "en-us" (no quotes) You can also use the variable !VERBOSE_SYSTEM_LOCALE! for the human readable locale e.g. English(UnitedStates)
@echo off
setlocal EnableDelayedExpansion
FOR /F "delims==" %%G IN ('systeminfo.exe') Do (
set input=%%G
for /l %%a in (1,1,100) do if "!input:~-1!"==" " set input=!input:~0,-1!
IF "!input:~0,13!"=="System Locale" (
set answer=!input:~15!
set answer=!answer: =!
set VERBOSE_SYSTEM_LOCALE=!answer:*;=!
call set SYSTEM_LOCALE_WITH_SEMICOLON=%%answer:!VERBOSE_SYSTEM_LOCALE!=%%
set SYSTEM_LOCALE=!SYSTEM_LOCALE_WITH_SEMICOLON:~0,-1!
echo !SYSTEM_LOCALE!
)
)
In fact, your proposals fail, because they rely on searching a string ("System Locale") which changes depending on the current locale! On my french Win10Pro, the string is "Option régionale du système" (with accented letter, which is very difficult to handle properly in a CMD file).
I've done some testing, and it seems, on MY system, that the lines about locale in systeminfo output are the only ones to contain a semi-colon character (";"):
24/09/2017 17:18:43,23 | C:\CMD ()
> systeminfo | findstr ;
Option régionale du système: fr;Français (France)
Paramètres régionaux d'entrée: fr;Français (France)
Based on that (which may NOT be true on other systems with different languages), I coded this get_locale.cmd script :
@echo off
setlocal EnableDelayedExpansion
FOR /F "delims==" %%A IN ('systeminfo.exe ^| findstr ";"') do (
FOR /F "usebackq tokens=2-3 delims=:;" %%B in (`echo %%A`) do (
set VERBOSE_SYSTEM_LOCALE=%%C
REM Removing useless leading spaces ...
FOR /F "usebackq tokens=1 delims= " %%D in (`echo %%B`) do (
set SYSTEM_LOCALE=%%D
)
set SYSTEM_LOCALE_WITH_SEMICOLON=!SYSTEM_LOCALE!;
set | findstr /I locale
REM No need to handle second line, quit after first one
goto :EOF
)
)
The result of my script on MY system is:
24/09/2017 17:18:45,31 | C:\CMD ()
> get_locale.cmd
SYSTEM_LOCALE=fr
SYSTEM_LOCALE_WITH_SEMICOLON=fr;
VERBOSE_SYSTEM_LOCALE=Français (France)
Be warned, however, that the three environment variables exist only inside the script, they do NOT persist in CMD environment after the script ends (no export command as in *nix).
me again.
I discovered a slight enhancement to my script. Using SETX one can store environment variables into USER environment for future CMD Windows to use (as stated in SETX /? second remark).
Then the get_locale.cmd script becomes:
@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /F "delims==" %%A IN ('systeminfo.exe ^| findstr ";"') DO (
FOR /F "usebackq tokens=2-3 delims=:;" %%B IN (`echo %%A`) DO (
SET VERBOSE_SYSTEM_LOCALE=%%C
setx VERBOSE_SYSTEM_LOCALE "%%C" 1>NUL
REM Removing useless leading spaces ...
FOR /F "usebackq tokens=1 delims= " %%D IN (`echo %%B`) DO (
SET SYSTEM_LOCALE=%%D
setx SYSTEM_LOCALE %%D 1>NUL
)
SET SYSTEM_LOCALE_WITH_SEMICOLON=!SYSTEM_LOCALE!;
setx SYSTEM_LOCALE_WITH_SEMICOLON !SYSTEM_LOCALE!; 1>NUL
SET | findstr /I locale
REM No need to handle second line, quit after first one
GOTO :EOF
)
)
For the VERBOSE_SYSTEM_LOCALE variable, since the value is possibly several words long, it is required to wrap %%C into quotes so that SETX receives only one parameter as expected.
Now, in any new CMD window, these values will be available:
16/12/2017 2:00:46 | C:\Users\Seagram ()
>set | findstr /i locale
SYSTEM_LOCALE=fr
SYSTEM_LOCALE_WITH_SEMICOLON=fr;
VERBOSE_SYSTEM_LOCALE=Français (France)
PS: It would be nice if other contributors from other countries with other languages could confirm that looking for semi-colon ';' works on their Windows systems. Thanks in advance!