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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2011-02-14 13:47:22 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2011 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-19 15:47:33 +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]).
|
|
|
|
|
2009-01-09 20:18:46 +01:00
|
|
|
-export([tokens/2, ascii_tolower/1]).
|
|
|
|
|
2003-01-06 20:57:05 +01:00
|
|
|
-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 = string:substr(FN, 1, string:len(FN) - 4),
|
|
|
|
L1 = case string:tokens(LP, ".") of
|
|
|
|
[Language] -> Language;
|
|
|
|
[Language, _Project] -> Language
|
|
|
|
end,
|
|
|
|
L2 = ascii_tolower(list_to_binary(L1)),
|
|
|
|
load_file(L2, 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-12-01 16:53:30 +01: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-12-01 16:53:30 +01: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;
|
|
|
|
_ ->
|
2009-01-09 20:18:46 +01:00
|
|
|
ShortLang = case tokens(LLang, $-) of
|
2004-09-30 23:54:39 +02:00
|
|
|
[] ->
|
|
|
|
LLang;
|
|
|
|
[SL | _] ->
|
|
|
|
SL
|
|
|
|
end,
|
2004-07-10 00:34:26 +02:00
|
|
|
case ShortLang of
|
2009-01-09 20:18:46 +01:00
|
|
|
<<"en">> ->
|
2004-07-10 00:34:26 +02:00
|
|
|
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) ->
|
2009-01-19 14:44:43 +01:00
|
|
|
%%TODO: ?MYLANG macro returns lang as a list(). Lang should be a binary.
|
2004-07-10 00:34:26 +02:00
|
|
|
case ?MYLANG of
|
|
|
|
undefined ->
|
|
|
|
Msg;
|
2009-01-19 14:44:43 +01:00
|
|
|
"en" ->
|
2004-07-10 00:34:26 +02:00
|
|
|
Msg;
|
|
|
|
Lang ->
|
2009-01-19 14:44:43 +01:00
|
|
|
BLang = list_to_binary(Lang),
|
|
|
|
LLang = ascii_tolower(BLang),
|
2004-09-30 23:54:39 +02:00
|
|
|
case ets:lookup(translations, {LLang, Msg}) of
|
2004-07-10 00:34:26 +02:00
|
|
|
[{_, Trans}] ->
|
|
|
|
Trans;
|
|
|
|
_ ->
|
2009-01-09 20:18:46 +01:00
|
|
|
ShortLang = case tokens(LLang, $-) of
|
2004-09-30 23:54:39 +02:00
|
|
|
[] ->
|
|
|
|
LLang;
|
|
|
|
[SL | _] ->
|
|
|
|
SL
|
|
|
|
end,
|
2004-07-10 00:34:26 +02:00
|
|
|
case ShortLang of
|
2009-01-09 20:18:46 +01:00
|
|
|
<<"en">> ->
|
2004-07-10 00:34:26 +02:00
|
|
|
Msg;
|
2009-01-19 14:44:43 +01:00
|
|
|
BLang ->
|
2004-07-10 00:34:26 +02:00
|
|
|
Msg;
|
|
|
|
_ ->
|
|
|
|
case ets:lookup(translations, {ShortLang, Msg}) of
|
|
|
|
[{_, Trans}] ->
|
|
|
|
Trans;
|
|
|
|
_ ->
|
|
|
|
Msg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2003-01-06 20:57:05 +01:00
|
|
|
end.
|
|
|
|
|
2009-01-09 20:18:46 +01:00
|
|
|
|
2008-08-14 15:32:31 +02:00
|
|
|
ascii_tolower(undefined) ->
|
2009-01-09 20:18:46 +01:00
|
|
|
<<>>;
|
|
|
|
ascii_tolower(Bin) ->
|
|
|
|
<< <<(char_tolower(X))>> || <<X>> <= Bin >>.
|
|
|
|
|
|
|
|
char_tolower(C) when C >= $A, C =< $Z ->
|
|
|
|
C + ($a -$A);
|
|
|
|
char_tolower(C) ->
|
|
|
|
C.
|
|
|
|
|
|
|
|
tokens(<<>>,_Sep) ->
|
|
|
|
[];
|
|
|
|
tokens(Bin, Sep) ->
|
|
|
|
tokens(Bin, Sep, <<>>, []).
|
|
|
|
|
|
|
|
tokens(<<>>, _Sep, T, Tokens) ->
|
|
|
|
lists:reverse([T|Tokens]);
|
|
|
|
tokens(<<Sep, R/binary>>, Sep, T, Tokens) ->
|
|
|
|
tokens(R, Sep, <<>>, [T | Tokens]);
|
|
|
|
tokens(<<C, R/binary>>, Sep, T, Tokens) ->
|
|
|
|
tokens(R, Sep, <<T/binary, C>>, Tokens).
|