Skip to content

Instantly share code, notes, and snippets.

@zr-tex8r
Created April 30, 2022 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zr-tex8r/eb20d1a3b15e28641701950c5e0bcd8c to your computer and use it in GitHub Desktop.
Save zr-tex8r/eb20d1a3b15e28641701950c5e0bcd8c to your computer and use it in GitHub Desktop.
なんかアレなやつ
local ffi = require("ffi")
ffi.cdef[[
int GetLocaleInfoEx(
const wchar_t *lpLocaleName,
unsigned long LCType,
wchar_t *lpLCData,
int cchData
);
]]
function to_wstr(str)
local out = ffi.new("wchar_t[?]", #str+1)
for i = 1, #str do out[i-1] = str:byte(i) end
return out
end
function from_wstr(wstr, len)
local cs = {}
for i = 1, len do cs[i] = wstr[i-1] end
return string.char(table.unpack(cs))
end
function current_lang()
local res = ffi.new("wchar_t[?]", 16)
local rlen = ffi.C.GetLocaleInfoEx(nil, 3, res, 16)
if rlen == 0 then return end
local clcode = from_wstr(res, rlen)
print("current language = "..clcode)
local qry = ffi.new("wchar_t[?]", 3)
for c1 = 97, 122 do for c2 = 97, 122 do
qry[0], qry[1] = c1, c2
rlen = ffi.C.GetLocaleInfoEx(qry, 3, res, 16)
local code = from_wstr(res, rlen)
if clcode == code then
print("two-letter code = "..string.char(c1, c2))
end
end end
end
current_lang()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment