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