2003-01-06 20:57:05 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : translate.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose :
|
|
|
|
%%% Created : 6 Jan 2003 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(translate).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
|
|
|
|
-export([start/0,
|
|
|
|
load_dir/1,
|
|
|
|
load_file/2,
|
|
|
|
translate/2]).
|
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
|
|
|
start() ->
|
|
|
|
ets:new(translations, [named_table, public]),
|
2004-04-15 21:55:38 +02:00
|
|
|
Dir =
|
|
|
|
case os:getenv("EJABBERD_MSGS_PATH") of
|
|
|
|
false ->
|
2004-05-17 22:36:41 +02:00
|
|
|
case code:priv_dir(ejabberd) of
|
2004-04-15 21:55:38 +02:00
|
|
|
{error, _} ->
|
|
|
|
?MSGS_DIR;
|
|
|
|
Path ->
|
|
|
|
filename:join([Path, "msgs"])
|
|
|
|
end;
|
|
|
|
Path ->
|
|
|
|
Path
|
|
|
|
end,
|
2003-12-28 21:59:21 +01:00
|
|
|
load_dir(Dir),
|
2003-01-06 20:57:05 +01:00
|
|
|
ok.
|
|
|
|
|
|
|
|
load_dir(Dir) ->
|
2003-07-20 22:35:35 +02:00
|
|
|
case file:list_dir(Dir) of
|
|
|
|
{ok, Files} ->
|
|
|
|
MsgFiles = lists:filter(
|
|
|
|
fun(FN) ->
|
|
|
|
case string:len(FN) > 4 of
|
|
|
|
true ->
|
2004-09-30 23:54:39 +02:00
|
|
|
string:substr(
|
|
|
|
FN,
|
|
|
|
string:len(FN) - 3) == ".msg";
|
2003-07-20 22:35:35 +02:00
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end, Files),
|
|
|
|
lists:foreach(
|
|
|
|
fun(FN) ->
|
2004-09-30 23:54:39 +02:00
|
|
|
L = ascii_tolower(
|
|
|
|
string:substr(FN, 1, string:len(FN) - 4)),
|
|
|
|
load_file(L, Dir ++ "/" ++ FN)
|
2003-07-20 22:35:35 +02:00
|
|
|
end, MsgFiles),
|
|
|
|
ok;
|
|
|
|
{error, Reason} ->
|
|
|
|
?ERROR_MSG("~p", [Reason])
|
|
|
|
end.
|
2003-01-06 20:57:05 +01:00
|
|
|
|
|
|
|
load_file(Lang, File) ->
|
2003-01-16 21:24:53 +01:00
|
|
|
case file:consult(File) of
|
|
|
|
{ok, Terms} ->
|
|
|
|
lists:foreach(fun({Orig, Trans}) ->
|
2004-07-10 00:34:26 +02:00
|
|
|
Trans1 = case Trans of
|
|
|
|
"" ->
|
|
|
|
Orig;
|
|
|
|
_ ->
|
|
|
|
Trans
|
|
|
|
end,
|
|
|
|
ets:insert(translations,
|
|
|
|
{{Lang, Orig}, Trans1})
|
2003-01-16 21:24:53 +01:00
|
|
|
end, Terms);
|
|
|
|
{error, Reason} ->
|
|
|
|
exit(file:format_error(Reason))
|
2003-01-06 20:57:05 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
translate(Lang, Msg) ->
|
2004-09-30 23:54:39 +02:00
|
|
|
LLang = ascii_tolower(Lang),
|
|
|
|
case ets:lookup(translations, {LLang, Msg}) of
|
2003-01-06 20:57:05 +01:00
|
|
|
[{_, Trans}] ->
|
|
|
|
Trans;
|
|
|
|
_ ->
|
2004-09-30 23:54:39 +02:00
|
|
|
ShortLang = case string:tokens(LLang, "-") of
|
|
|
|
[] ->
|
|
|
|
LLang;
|
|
|
|
[SL | _] ->
|
|
|
|
SL
|
|
|
|
end,
|
2004-07-10 00:34:26 +02:00
|
|
|
case ShortLang of
|
|
|
|
"en" ->
|
|
|
|
Msg;
|
2004-09-30 23:54:39 +02:00
|
|
|
LLang ->
|
2004-07-10 00:34:26 +02:00
|
|
|
translate(Msg);
|
|
|
|
_ ->
|
|
|
|
case ets:lookup(translations, {ShortLang, Msg}) of
|
|
|
|
[{_, Trans}] ->
|
|
|
|
Trans;
|
|
|
|
_ ->
|
|
|
|
translate(Msg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
translate(Msg) ->
|
|
|
|
case ?MYLANG of
|
|
|
|
undefined ->
|
|
|
|
Msg;
|
|
|
|
"en" ->
|
|
|
|
Msg;
|
|
|
|
Lang ->
|
2004-09-30 23:54:39 +02:00
|
|
|
LLang = ascii_tolower(Lang),
|
|
|
|
case ets:lookup(translations, {LLang, Msg}) of
|
2004-07-10 00:34:26 +02:00
|
|
|
[{_, Trans}] ->
|
|
|
|
Trans;
|
|
|
|
_ ->
|
2004-09-30 23:54:39 +02:00
|
|
|
ShortLang = case string:tokens(LLang, "-") of
|
|
|
|
[] ->
|
|
|
|
LLang;
|
|
|
|
[SL | _] ->
|
|
|
|
SL
|
|
|
|
end,
|
2004-07-10 00:34:26 +02:00
|
|
|
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.
|
|
|
|
|
2004-09-30 23:54:39 +02:00
|
|
|
ascii_tolower([C | Cs]) when C >= $A, C =< $Z ->
|
|
|
|
[C + ($a - $A) | ascii_tolower(Cs)];
|
|
|
|
ascii_tolower([C | Cs]) ->
|
|
|
|
[C | ascii_tolower(Cs)];
|
|
|
|
ascii_tolower([]) ->
|
|
|
|
[].
|
|
|
|
|