2003-01-06 20:57:05 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : translate.erl
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%% Purpose : Localization helper
|
|
|
|
%%% Created : 6 Jan 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2020-01-28 13:34:02 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2020 ProcessOne
|
2007-12-24 13:58:05 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-24 13:58:05 +01:00
|
|
|
%%%
|
2003-01-06 20:57:05 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(translate).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-12-24 13:58:05 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-01-06 20:57:05 +01:00
|
|
|
|
2017-02-26 13:10:59 +01:00
|
|
|
-behaviour(gen_server).
|
|
|
|
|
2017-09-24 11:42:35 +02:00
|
|
|
-export([start_link/0, reload/0, translate/2]).
|
2017-02-26 13:10:59 +01:00
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
|
|
terminate/2, code_change/3]).
|
2003-01-06 20:57:05 +01:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2017-04-15 23:29:55 +02:00
|
|
|
-include_lib("kernel/include/file.hrl").
|
|
|
|
|
|
|
|
-define(ZERO_DATETIME, {{0,0,0}, {0,0,0}}).
|
2003-01-06 20:57:05 +01:00
|
|
|
|
2019-07-10 09:30:11 +02:00
|
|
|
-type error_reason() :: file:posix() | {integer(), module(), term()} |
|
2019-07-10 23:04:32 +02:00
|
|
|
badarg | terminated | system_limit | bad_file |
|
|
|
|
bad_encoding.
|
2019-07-10 09:30:11 +02:00
|
|
|
|
2017-02-26 13:10:59 +01:00
|
|
|
-record(state, {}).
|
|
|
|
|
|
|
|
start_link() ->
|
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
|
|
|
|
|
|
|
init([]) ->
|
2017-03-17 19:35:02 +01:00
|
|
|
process_flag(trap_exit, true),
|
2019-07-10 09:30:11 +02:00
|
|
|
case load() of
|
|
|
|
ok ->
|
|
|
|
xmpp:set_tr_callback({?MODULE, translate}),
|
|
|
|
{ok, #state{}};
|
|
|
|
{error, Reason} ->
|
|
|
|
{stop, Reason}
|
|
|
|
end.
|
2017-02-26 13:10:59 +01:00
|
|
|
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_call(Request, From, State) ->
|
|
|
|
?WARNING_MSG("Unexpected call from ~p: ~p", [From, Request]),
|
|
|
|
{noreply, State}.
|
2017-02-26 13:10:59 +01:00
|
|
|
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_cast(Msg, State) ->
|
|
|
|
?WARNING_MSG("Unexpected cast: ~p", [Msg]),
|
2017-02-26 13:10:59 +01:00
|
|
|
{noreply, State}.
|
|
|
|
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_info(Info, State) ->
|
|
|
|
?WARNING_MSG("Unexpected info: ~p", [Info]),
|
2017-02-26 13:10:59 +01:00
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
terminate(_Reason, _State) ->
|
2017-03-17 19:35:02 +01:00
|
|
|
xmpp:set_tr_callback(undefined).
|
2003-01-06 20:57:05 +01:00
|
|
|
|
2017-02-26 13:10:59 +01:00
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
2019-07-10 09:30:11 +02:00
|
|
|
-spec reload() -> ok | {error, error_reason()}.
|
2017-04-15 23:29:55 +02:00
|
|
|
reload() ->
|
|
|
|
load(true).
|
|
|
|
|
2019-07-10 09:30:11 +02:00
|
|
|
-spec load() -> ok | {error, error_reason()}.
|
2017-04-15 23:29:55 +02:00
|
|
|
load() ->
|
|
|
|
load(false).
|
|
|
|
|
2019-07-10 09:30:11 +02:00
|
|
|
-spec load(boolean()) -> ok | {error, error_reason()}.
|
2017-04-15 23:29:55 +02:00
|
|
|
load(ForceCacheRebuild) ->
|
|
|
|
{MsgsDirMTime, MsgsDir} = get_msg_dir(),
|
|
|
|
{CacheMTime, CacheFile} = get_cache_file(),
|
|
|
|
{FilesMTime, MsgFiles} = get_msg_files(MsgsDir),
|
|
|
|
LastModified = lists:max([MsgsDirMTime, FilesMTime]),
|
|
|
|
if ForceCacheRebuild orelse CacheMTime < LastModified ->
|
2019-07-10 09:30:11 +02:00
|
|
|
case load(MsgFiles, MsgsDir) of
|
|
|
|
ok -> dump_to_file(CacheFile);
|
|
|
|
Err -> Err
|
|
|
|
end;
|
2017-04-15 23:29:55 +02:00
|
|
|
true ->
|
|
|
|
case ets:file2tab(CacheFile) of
|
|
|
|
{ok, _} ->
|
|
|
|
ok;
|
|
|
|
{error, {read_error, {file_error, _, enoent}}} ->
|
|
|
|
load(MsgFiles, MsgsDir);
|
|
|
|
{error, {read_error, {file_error, _, Reason}}} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Failed to read translation cache from ~ts: ~ts",
|
|
|
|
[CacheFile, format_error(Reason)]),
|
2017-04-15 23:29:55 +02:00
|
|
|
load(MsgFiles, MsgsDir);
|
|
|
|
{error, Reason} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Failed to read translation cache from ~ts: ~p",
|
|
|
|
[CacheFile, Reason]),
|
2017-04-15 23:29:55 +02:00
|
|
|
load(MsgFiles, MsgsDir)
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2019-07-10 09:30:11 +02:00
|
|
|
-spec load([file:filename()], file:filename()) -> ok | {error, error_reason()}.
|
2017-04-15 23:29:55 +02:00
|
|
|
load(Files, Dir) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
try ets:new(translations, [named_table, public]) of
|
|
|
|
_ -> ok
|
2017-04-15 23:29:55 +02:00
|
|
|
catch _:badarg -> ok
|
|
|
|
end,
|
|
|
|
case Files of
|
|
|
|
[] ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("No translation files found in ~ts, "
|
2019-07-10 09:30:11 +02:00
|
|
|
"check directory access",
|
2019-09-23 14:17:20 +02:00
|
|
|
[Dir]);
|
2017-04-15 23:29:55 +02:00
|
|
|
_ ->
|
2019-07-10 09:30:11 +02:00
|
|
|
?INFO_MSG("Building language translation cache", []),
|
|
|
|
Objs = lists:flatten(misc:pmap(fun load_file/1, Files)),
|
|
|
|
case lists:keyfind(error, 1, Objs) of
|
|
|
|
false ->
|
|
|
|
ets:delete_all_objects(translations),
|
|
|
|
ets:insert(translations, Objs),
|
|
|
|
?DEBUG("Language translation cache built successfully", []);
|
|
|
|
{error, File, Reason} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Failed to read translation file ~ts: ~ts",
|
|
|
|
[File, format_error(Reason)]),
|
2019-07-10 09:30:11 +02:00
|
|
|
{error, Reason}
|
|
|
|
end
|
2014-04-15 16:16:12 +02:00
|
|
|
end.
|
|
|
|
|
2019-07-10 09:30:11 +02:00
|
|
|
-spec load_file(file:filename()) -> [{{binary(), binary()}, binary()} |
|
|
|
|
{error, file:filename(), error_reason()}].
|
|
|
|
load_file(File) ->
|
|
|
|
Lang = lang_of_file(File),
|
2019-07-24 10:34:40 +02:00
|
|
|
try file:consult(File) of
|
2019-07-10 09:30:11 +02:00
|
|
|
{ok, Lines} ->
|
|
|
|
lists:map(
|
|
|
|
fun({In, Out}) ->
|
2019-07-24 10:34:40 +02:00
|
|
|
try {unicode:characters_to_binary(In),
|
|
|
|
unicode:characters_to_binary(Out)} of
|
2019-07-10 23:13:39 +02:00
|
|
|
{InB, OutB} when is_binary(InB), is_binary(OutB) ->
|
|
|
|
{{Lang, InB}, OutB};
|
|
|
|
_ ->
|
|
|
|
{error, File, bad_encoding}
|
2019-07-10 23:04:32 +02:00
|
|
|
catch _:badarg ->
|
|
|
|
{error, File, bad_encoding}
|
|
|
|
end;
|
2019-07-10 09:30:11 +02:00
|
|
|
(_) ->
|
|
|
|
{error, File, bad_file}
|
|
|
|
end, Lines);
|
|
|
|
{error, Reason} ->
|
|
|
|
[{error, File, Reason}]
|
2019-07-24 10:34:40 +02:00
|
|
|
catch _:{case_clause, {error, _}} ->
|
|
|
|
%% At the moment of the writing there was a bug in
|
|
|
|
%% file:consult_stream/3 - it doesn't process {error, term()}
|
|
|
|
%% result from io:read/3
|
|
|
|
[{error, File, bad_file}]
|
2003-01-06 20:57:05 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec translate(binary(), binary()) -> binary().
|
2003-01-06 20:57:05 +01:00
|
|
|
translate(Lang, Msg) ->
|
2004-09-30 23:54:39 +02:00
|
|
|
LLang = ascii_tolower(Lang),
|
|
|
|
case ets:lookup(translations, {LLang, Msg}) of
|
2013-03-14 10:33:02 +01:00
|
|
|
[{_, Trans}] -> Trans;
|
|
|
|
_ ->
|
|
|
|
ShortLang = case str:tokens(LLang, <<"-">>) of
|
|
|
|
[] -> LLang;
|
|
|
|
[SL | _] -> SL
|
|
|
|
end,
|
|
|
|
case ShortLang of
|
|
|
|
<<"en">> -> Msg;
|
|
|
|
LLang -> translate(Msg);
|
|
|
|
_ ->
|
|
|
|
case ets:lookup(translations, {ShortLang, Msg}) of
|
|
|
|
[{_, Trans}] -> Trans;
|
|
|
|
_ -> translate(Msg)
|
|
|
|
end
|
|
|
|
end
|
2004-07-10 00:34:26 +02:00
|
|
|
end.
|
|
|
|
|
2019-07-10 09:30:11 +02:00
|
|
|
-spec translate(binary()) -> binary().
|
2004-07-10 00:34:26 +02:00
|
|
|
translate(Msg) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case ejabberd_option:language() of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"en">> -> Msg;
|
|
|
|
Lang ->
|
|
|
|
LLang = ascii_tolower(Lang),
|
|
|
|
case ets:lookup(translations, {LLang, Msg}) of
|
|
|
|
[{_, Trans}] -> Trans;
|
|
|
|
_ ->
|
|
|
|
ShortLang = case str:tokens(LLang, <<"-">>) of
|
|
|
|
[] -> LLang;
|
|
|
|
[SL | _] -> SL
|
|
|
|
end,
|
|
|
|
case ShortLang of
|
|
|
|
<<"en">> -> Msg;
|
|
|
|
Lang -> Msg;
|
|
|
|
_ ->
|
|
|
|
case ets:lookup(translations, {ShortLang, Msg}) of
|
|
|
|
[{_, Trans}] -> Trans;
|
|
|
|
_ -> Msg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2003-01-06 20:57:05 +01:00
|
|
|
end.
|
|
|
|
|
2019-07-10 09:30:11 +02:00
|
|
|
-spec ascii_tolower(list() | binary()) -> binary().
|
|
|
|
ascii_tolower(B) when is_binary(B) ->
|
|
|
|
<< <<(if X >= $A, X =< $Z ->
|
|
|
|
X + 32;
|
|
|
|
true ->
|
|
|
|
X
|
|
|
|
end)>> || <<X>> <= B >>;
|
|
|
|
ascii_tolower(S) ->
|
|
|
|
ascii_tolower(unicode:characters_to_binary(S)).
|
2017-04-15 23:29:55 +02:00
|
|
|
|
|
|
|
-spec get_msg_dir() -> {calendar:datetime(), file:filename()}.
|
|
|
|
get_msg_dir() ->
|
2018-06-14 13:00:47 +02:00
|
|
|
Dir = misc:msgs_dir(),
|
2017-04-15 23:29:55 +02:00
|
|
|
case file:read_file_info(Dir) of
|
|
|
|
{ok, #file_info{mtime = MTime}} ->
|
|
|
|
{MTime, Dir};
|
|
|
|
{error, Reason} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Failed to read directory ~ts: ~ts",
|
|
|
|
[Dir, format_error(Reason)]),
|
2017-04-15 23:29:55 +02:00
|
|
|
{?ZERO_DATETIME, Dir}
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec get_msg_files(file:filename()) -> {calendar:datetime(), [file:filename()]}.
|
|
|
|
get_msg_files(MsgsDir) ->
|
|
|
|
Res = filelib:fold_files(
|
|
|
|
MsgsDir, ".+\\.msg", false,
|
|
|
|
fun(File, {MTime, Files} = Acc) ->
|
2019-07-10 09:30:11 +02:00
|
|
|
case xmpp_lang:is_valid(lang_of_file(File)) of
|
|
|
|
true ->
|
|
|
|
case file:read_file_info(File) of
|
|
|
|
{ok, #file_info{mtime = Time}} ->
|
|
|
|
{lists:max([MTime, Time]), [File|Files]};
|
|
|
|
{error, Reason} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Failed to read translation file ~ts: ~ts",
|
|
|
|
[File, format_error(Reason)]),
|
2019-07-10 09:30:11 +02:00
|
|
|
Acc
|
|
|
|
end;
|
|
|
|
false ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Ignoring translation file ~ts: file name "
|
2019-07-10 09:30:11 +02:00
|
|
|
"must be a valid language tag",
|
2019-09-23 14:17:20 +02:00
|
|
|
[File]),
|
2017-04-15 23:29:55 +02:00
|
|
|
Acc
|
|
|
|
end
|
|
|
|
end, {?ZERO_DATETIME, []}),
|
|
|
|
case Res of
|
|
|
|
{_, []} ->
|
|
|
|
case file:list_dir(MsgsDir) of
|
|
|
|
{ok, _} -> ok;
|
|
|
|
{error, Reason} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Failed to read directory ~ts: ~ts",
|
|
|
|
[MsgsDir, format_error(Reason)])
|
2017-04-15 23:29:55 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
Res.
|
|
|
|
|
|
|
|
-spec get_cache_file() -> {calendar:datetime(), file:filename()}.
|
|
|
|
get_cache_file() ->
|
|
|
|
MnesiaDir = mnesia:system_info(directory),
|
|
|
|
CacheFile = filename:join(MnesiaDir, "translations.cache"),
|
|
|
|
CacheMTime = case file:read_file_info(CacheFile) of
|
|
|
|
{ok, #file_info{mtime = Time}} -> Time;
|
|
|
|
{error, _} -> ?ZERO_DATETIME
|
|
|
|
end,
|
|
|
|
{CacheMTime, CacheFile}.
|
|
|
|
|
|
|
|
-spec dump_to_file(file:filename()) -> ok.
|
|
|
|
dump_to_file(CacheFile) ->
|
|
|
|
case ets:tab2file(translations, CacheFile) of
|
|
|
|
ok -> ok;
|
|
|
|
{error, Reason} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Failed to create translation cache in ~ts: ~p",
|
|
|
|
[CacheFile, Reason])
|
2017-04-15 23:29:55 +02:00
|
|
|
end.
|
2019-07-10 09:30:11 +02:00
|
|
|
|
|
|
|
-spec lang_of_file(file:filename()) -> binary().
|
|
|
|
lang_of_file(FileName) ->
|
|
|
|
BaseName = filename:basename(FileName),
|
|
|
|
ascii_tolower(filename:rootname(BaseName)).
|
|
|
|
|
|
|
|
-spec format_error(error_reason()) -> string().
|
|
|
|
format_error(bad_file) ->
|
|
|
|
"corrupted or invalid translation file";
|
2019-07-10 23:04:32 +02:00
|
|
|
format_error(bad_encoding) ->
|
2019-07-24 10:34:40 +02:00
|
|
|
"cannot translate from UTF-8";
|
2019-07-10 09:30:11 +02:00
|
|
|
format_error({_, _, _} = Reason) ->
|
|
|
|
"at line " ++ file:format_error(Reason);
|
|
|
|
format_error(Reason) ->
|
|
|
|
file:format_error(Reason).
|