Browse Source

language -> @language: allow reference to local variables

master
nixo 2 years ago
parent
commit
0e44198a8c
  1. 6
      src/I18N.jl
  2. 8
      test/hello.jl
  3. 6
      test/numbers.jl
  4. 17
      test/test.jl

6
src/I18N.jl

@ -12,7 +12,7 @@ macro __str(phrase)
line = __source__.line,
strid = phrase))
return esc(quote
get(get(i18n, I18N.language(), Dict()),
get(get(i18n, @language, Dict()),
$phrase, $phrase)
end)
end
@ -102,7 +102,9 @@ macro translator(lang, name, email, copyright)
end)
end
@inline language() = "en"
macro language()
"en"
end
export @__str, @translations, @translate,
@translator, @write_po_files, lang_expand

8
test/hello.jl

@ -1,3 +1,5 @@
_"hello"
_"hi"
_"hola"
function return_hellos(req)
(_"hello",
_"hi",
_"hola")
end

6
test/numbers.jl

@ -1,3 +1,3 @@
_"first"
_"second"
_"third"
function print_numbers(req)
println.([_"first", _"second", _"third"])
end

17
test/test.jl

@ -1,15 +1,15 @@
push!(LOAD_PATH, realpath(".."))
using I18N
@translations it en
# Define it before expanding any _"string"
# Custom function used to get the program language.
# Must return a 2-char language code
req = Dict(:params => Dict(:lang => "it"))
@inline function I18N.language()
req[:params][:lang]
macro language()
return esc(:(req[:params][:lang]))
end
@translations it en
@translate "it" "first" "primo"
@translate "it" "third" "terzo"
@ -21,3 +21,10 @@ include("hello.jl")
@translator "it" "Nicolò Balzarotti" "nicolo@nixo.xyz" "2020"
@write_po_files "Test" "0.0.0-dev" ["Nicolò Balzarotti <nicolo@nixo.xyz>"] "2020"
let req = Dict(:params => Dict(:lang => "it"))
_"third"
end
print_numbers(Dict(:params => Dict(:lang => "it")))
return_hellos(Dict(:params => Dict(:lang => "it")))

Loading…
Cancel
Save