怎么用脚本获取系统区域格式

wen 实用脚本 4

本文目录导读:

怎么用脚本获取系统区域格式

  1. Windows (PowerShell)
  2. Linux/macOS (Bash)
  3. 跨平台脚本 (Python)
  4. JavaScript (Node.js)
  5. VBScript (Windows)
  6. PowerShell 多平台 (兼容)
  7. 使用建议

Windows (PowerShell)

# 获取当前区域格式(如 zh-CN, en-US)
Get-Culture | Select-Object Name
# 获取更详细的信息
Get-Culture | Format-List
# 获取显示语言
Get-WinSystemLocale | Select-Object Name
# 获取用户区域设置
Get-Culture | Select-Object DisplayName, LCID, Name

批处理 (CMD)

:: 获取区域格式
reg query "HKEY_CURRENT_USER\Control Panel\International" /v sShortDate
:: 使用 systeminfo 获取区域信息
systeminfo | findstr /C:"区域"

Linux/macOS (Bash)

# 获取当前区域格式
echo $LANG
locale | grep LANG=
# 获取所有区域相关信息
locale
# 获取 LC_ALL 或 LC_CTYPE
echo $LC_ALL
echo $LC_CTYPE
# 更详细的信息
locale | grep -E "^(LANG|LC_|LANGUAGE)"

跨平台脚本 (Python)

import locale
import sys
import platform
# 获取系统默认区域
def get_system_locale():
    locale.setlocale(locale.LC_ALL, '')
    current_locale = locale.getlocale()
    # 在 Windows 上获取更准确的区域格式
    if platform.system() == 'Windows':
        import ctypes
        user32 = ctypes.windll.user32
        kernel32 = ctypes.windll.kernel32
        # 获取用户区域设置
        w_language = ctypes.create_unicode_buffer(50)
        kernel32.GetUserDefaultLocaleName(w_language, 50)
        return {
            'locale': w_language.value,
            'language': locale.getlocale(locale.LC_MESSAGES)[0],
            'encoding': locale.getpreferredencoding()
        }
    return {
        'locale': current_locale[0],
        'encoding': locale.getpreferredencoding()
    }
# 执行
result = get_system_locale()
print(f"区域格式: {result.get('locale', 'N/A')}")
print(f"编码: {result.get('encoding', 'N/A')}")

JavaScript (Node.js)

// 获取系统区域格式
const os = require('os');
const { execSync } = require('child_process');
function getSystemLocale() {
    // Windows
    if (process.platform === 'win32') {
        try {
            const result = execSync('powershell -Command "Get-Culture | Select-Object -ExpandProperty Name"', 
                { encoding: 'utf-8' }).trim();
            return result;
        } catch(e) {
            return 'Unknown';
        }
    } 
    // Linux/macOS
    else if (process.platform === 'linux' || process.platform === 'darwin') {
        const lang = process.env.LANG || 
                     execSync('locale | grep LANG= | cut -d= -f2', 
                     { encoding: 'utf-8' }).trim();
        return lang;
    }
    return 'Unknown';
}
console.log('系统区域格式:', getSystemLocale());

VBScript (Windows)

' 获取系统区域格式
Set objShell = CreateObject("WScript.Shell")
strLocale = objShell.RegRead("HKEY_CURRENT_USER\Control Panel\International\LocaleName")
' 或者使用更简单的方法
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem")
For Each objItem in colItems
    WScript.Echo "区域格式: " & objItem.Locale
Next
WScript.Echo "LocaleName: " & strLocale

PowerShell 多平台 (兼容)

# 跨平台获取区域格式
if ($IsWindows) {
    $culture = Get-Culture
    $localeName = $culture.Name
    $uiCulture = Get-UICulture | Select-Object -ExpandProperty Name
} else {
    # Linux/macOS
    $localeName = $env:LANG
    $uiCulture = $env:LANG
}
Write-Host "区域格式: $localeName"
Write-Host "UI语言: $uiCulture"
Write-Host "当前日期格式: $(Get-Date -Format 'yyyy-MM-dd')"

使用建议

  • Windows: 推荐使用 PowerShell 的 Get-Culture
  • Linux/macOS: 检查环境变量 $LANG
  • 跨平台: 使用 Python 的 locale 模块
  • Node.js: 结合 process.env 和系统命令

选择适合你环境和需求的方法即可。

抱歉,评论功能暂时关闭!