master
nixo 2020-09-02 13:46:53 +02:00
parent 6f787c0852
commit f5148da326
1 changed files with 33 additions and 21 deletions

View File

@ -2,13 +2,18 @@ module I18N
using Dates using Dates
const i18n_cache = Any[]
language_map = include("codes.jl") language_map = include("codes.jl")
# include("types.jl")
include("pot.jl")
include("dart.jl")
translationpath(langcode, domain = "base") = translationpath(langcode, domain = "base") =
joinpath(realpath("."), "locales/$(langcode)/LC_MESSAGES/$(domain).pot") joinpath(realpath("."), "locales/$(langcode)/LC_MESSAGES/$(domain).pot")
macro __str(phrase) macro __str(phrase)
push!(Main.i18n_cache, (file = __source__.file, push!(I18N.i18n_cache, (file = __source__.file,
line = __source__.line, line = __source__.line,
strid = phrase)) strid = phrase))
return esc(quote return esc(quote
@ -21,7 +26,6 @@ macro translations(languages...)
return esc(quote return esc(quote
i18n = Dict{String,Dict{String,String}}() i18n = Dict{String,Dict{String,String}}()
# (lang, file.jl, line) => translation # (lang, file.jl, line) => translation
i18n_cache = Any[]
# lang => [(name, mail, year)] # lang => [(name, mail, year)]
i18n_translators = Dict{String,Array{Tuple{String,String,String}}}() i18n_translators = Dict{String,Array{Tuple{String,String,String}}}()
for language in $languages for language in $languages
@ -63,29 +67,37 @@ lang_expand(lang) = language_map[lang]
current_year() = Dates.today() |> year current_year() = Dates.today() |> year
macro write_po_files(name = nothing, version = nothing, macro write_po_files(name = nothing, version = nothing,
authors = nothing, year = nothing) authors = nothing, year = nothing,
split_domains = false)
return esc(quote return esc(quote
unique!(i18n_cache) unique!(I18N.i18n_cache)
# Sort by line & by file # Sort by ine & by file
sort!(i18n_cache, by = x -> x[2]) sort!(I18N.i18n_cache, by = x -> x[2])
sort!(i18n_cache, by = first) sort!(I18N.i18n_cache, by = first)
lpath = joinpath(realpath("."), "locales")
if ispath(lpath)
@error "Folder $(lpath) exists, delete before continuing"
end
# Write to file # Write to file
for language in keys(i18n) for language in keys(i18n)
translationfile = I18N.translationpath(string(language)) for tr in I18N.i18n_cache
mkpath(dirname(translationfile)) domain = $split_domains ? basename(dirname(realpath((tr.file)))) : "base"
open(translationfile, "w") do io translationfile = I18N.translationpath(string(language), domain)
mkpath(dirname(translationfile))
exists = isfile(translationfile)
open(translationfile, "a+") do io
translators = get(i18n_translators, language, []) translators = get(i18n_translators, language, [])
write(io, I18N.po_header(lang_expand(language), if !exists
something($name, uppercasefirst(basename(realpath(".")))), write(io, I18N.po_header(lang_expand(language),
something($version, "0.1.0-dev"), something($name, uppercasefirst(basename(realpath(".")))),
something($year, I18N.current_year()), something($version, "0.1.0-dev"),
join(something($authors, something($year, I18N.current_year()),
first.(translators)), join(something($authors,
", "), translators)) first.(translators)),
for tr in i18n_cache ", "), translators))
translation = get(get(i18n, language, Dict()), tr.strid, "") end
write(io, translation = get(get(i18n, language, Dict()), tr.strid, "")
I18N.format(tr.file, tr.line, tr.strid, translation)) write(io, I18N.format(tr.file, tr.line, tr.strid, translation))
end end
end end
end end