41 lines
1.7 KiB
Erlang
41 lines
1.7 KiB
Erlang
%%% A rework of the JumpText idea. To be adapted into actual JumpText for v2.0.
|
|
%%% To add translations to a module you add a file named ?MODULE.trans to priv/i18n/.
|
|
%%% The file contains an Erlang terms file of the form:
|
|
%%% `{LangCode :: atom(), Translations :: #{Phrase :: string() := Translation :: string()}}'
|
|
%%% The entire file's contents are returned by `read_translations/1'.
|
|
%%% The function `j/2' extracts the dictionary requested and returns a translation
|
|
%%% function that closes over the desired phrase dictionary.
|
|
%%% The customary way to insert translations into a program is to assign the translation
|
|
%%% closure the name `J' and keep it in the local process state. When the user selects
|
|
%%% another language, create a new `J' with the desired language.
|
|
%%%
|
|
%%% `oneshot_j/2' is a convenience function for those who don't intend to keep the
|
|
%%% library of translations in memory (calling `oneshot_j/2' will always perform a disk
|
|
%%% read, where `j/2' could be called any number of times without a disk read if the
|
|
%%% translation library is retained).
|
|
|
|
-module(gmc_jt).
|
|
-vsn("0.1.5").
|
|
-export([read_translations/1, j/2, oneshot_j/2]).
|
|
|
|
|
|
read_translations(Module) ->
|
|
File = atom_to_list(Module) ++ ".trans",
|
|
Path = filename:join([zx:get_home(), "priv", "i18n", File]),
|
|
case file:consult(Path) of
|
|
{ok, Translations} -> Translations;
|
|
{error, enoent} -> []
|
|
end.
|
|
|
|
|
|
j(Lang, Translations) ->
|
|
case proplists:get_value(Lang, Translations, none) of
|
|
none -> fun(Phrase) -> Phrase end;
|
|
Dict -> fun(Phrase) -> maps:get(Phrase, Dict, Phrase) end
|
|
end.
|
|
|
|
|
|
oneshot_j(Module, Lang) ->
|
|
Translations = read_translations(Module),
|
|
j(Lang, Translations).
|