使用Ruby在Windows中读取区域位置设置(国家代码)? [英] Reading the regional location setting (country code) in Windows using Ruby?

查看:156
本文介绍了使用Ruby在Windows中读取区域位置设置(国家代码)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Ruby访问控制面板:地区和语言:位置:当前位置 设置.我只对国家/地区代码感兴趣.

I am trying to access the Control Panel: Region and Language: Location: Current location setting using Ruby. I am only interested in the country code.

我得到的最接近的是系统区域设置中的国家/地区代码,但这与我所追求的不尽相同.

The closest I have got is the country code from the System Locale but that is not quite what I was after.

`systeminfo | findstr /B /C:"System Locale"`.to_s.upcase.strip[30..31]

我希望外面有人知道.谢谢.

I hope that someone out there might know. Thanks.

推荐答案

使用Win32 API:

Using the Win32 API:

require 'Win32API'

# Set up some Win32 constants
GEOCLASS_NATION  = 16
GEO_ISO2         = 4
GEO_FRIENDLYNAME = 8

# Set up some API calls
GetUserGeoID = Win32API.new('kernel32', 'GetUserGeoID', ['L'], 'L')
GetGeoInfo   = Win32API.new('kernel32', 'GetGeoInfoA', ['L', 'L', 'P', 'L', 'L'], 'L')

# Get user's GEOID
geoid = GetUserGeoID.call(GEOCLASS_NATION)
=> 77

# Get ISO name
buffer = " " * 100
GetGeoInfo.call(geoid, GEO_ISO2, buffer, buffer.length, 0)
geo_iso = buffer.strip
=> "FI"

# Get friendly name
buffer = " " * 100
GetGeoInfo.call(geoid, GEO_FRIENDLYNAME, buffer, buffer.length, 0)
geo_name = buffer.strip
=> "Finland"

GetUserGeoID的文档:
http://msdn.microsoft.com/en-us/library/dd318138. aspx

Documentation for GetUserGeoID:
http://msdn.microsoft.com/en-us/library/dd318138.aspx

GetGeoInfo的文档:
https://docs.microsoft.com/zh-cn/windows/desktop/api/winnls/nf-winnls-getgeoinfoa

Documentation for GetGeoInfo:
https://docs.microsoft.com/en-us/windows/desktop/api/winnls/nf-winnls-getgeoinfoa

要将GEOID转换为位置名称,您还可以使用此表:
http://msdn.microsoft.com/en-us/library/dd374073. aspx

To convert a GEOID to a location name you can also use this table:
http://msdn.microsoft.com/en-us/library/dd374073.aspx

这篇关于使用Ruby在Windows中读取区域位置设置(国家代码)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆