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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2010-01-12 17:11:32 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2010 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
|
|
|
%%%
|
2007-12-24 13:58:05 +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., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2003-01-06 20:57:05 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(translate).
|
2007-12-24 13:58:05 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-01-06 20:57:05 +01:00
|
|
|
|
|
|
|
-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) ->
|
2009-04-27 22:55:35 +02:00
|
|
|
LP = ascii_tolower(
|
|
|
|
string:substr(FN, 1, string:len(FN) - 4)),
|
|
|
|
L = case string:tokens(LP, ".") of
|
|
|
|
[Language] -> Language;
|
|
|
|
[Language, _Project] -> Language
|
|
|
|
end,
|
2004-09-30 23:54:39 +02:00
|
|
|
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);
|
2008-10-25 00:16:17 +02:00
|
|
|
%% Code copied from ejabberd_config.erl
|
|
|
|
{error, {_LineNumber, erl_parse, _ParseMessage} = Reason} ->
|
|
|
|
ExitText = lists:flatten(File ++ " approximately in the line "
|
|
|
|
++ file:format_error(Reason)),
|
|
|
|
?ERROR_MSG("Problem loading translation file ~n~s", [ExitText]),
|
|
|
|
exit(ExitText);
|
2003-01-16 21:24:53 +01:00
|
|
|
{error, Reason} ->
|
2008-10-25 00:16:17 +02:00
|
|
|
ExitText = lists:flatten(File ++ ": " ++ file:format_error(Reason)),
|
|
|
|
?ERROR_MSG("Problem loading translation file ~n~s", [ExitText]),
|
|
|
|
exit(ExitText)
|
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([]) ->
|
|
|
|
[].
|
|
|
|
|