forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
withlocales.jl
27 lines (26 loc) · 806 Bytes
/
withlocales.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# This file is a part of Julia. License is MIT: https://julialang.org/license
function withlocales(f, newlocales)
# save current locales
locales = Dict{Int,String}()
for cat in 0:9999
cstr = ccall(:setlocale, Cstring, (Cint, Cstring), cat, C_NULL)
if cstr != C_NULL
locales[cat] = unsafe_string(cstr)
end
end
try
# change to each of given locales
for lc in newlocales
set = true
for (cat, _) in locales
set &= ccall(:setlocale, Cstring, (Cint, Cstring), cat, lc) != C_NULL
end
set && f(lc)
end
finally
# recover locales
for (cat, lc) in locales
cstr = ccall(:setlocale, Cstring, (Cint, Cstring), cat, lc)
end
end
end