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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2016-01-13 12:29:14 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2016 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
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([start/0, load_dir/1, load_file/2,
|
2003-01-06 20:57:05 +01:00
|
|
|
translate/2]).
|
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2003-01-06 20:57:05 +01:00
|
|
|
|
|
|
|
start() ->
|
|
|
|
ets:new(translations, [named_table, public]),
|
2013-03-14 10:33:02 +01:00
|
|
|
Dir = case os:getenv("EJABBERD_MSGS_PATH") of
|
2004-04-15 21:55:38 +02:00
|
|
|
false ->
|
2004-05-17 22:36:41 +02:00
|
|
|
case code:priv_dir(ejabberd) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{error, _} -> ?MSGS_DIR;
|
|
|
|
Path -> filename:join([Path, "msgs"])
|
2004-04-15 21:55:38 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
Path -> Path
|
|
|
|
end,
|
|
|
|
load_dir(iolist_to_binary(Dir)),
|
2003-01-06 20:57:05 +01:00
|
|
|
ok.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec load_dir(binary()) -> ok.
|
|
|
|
|
2003-01-06 20:57:05 +01:00
|
|
|
load_dir(Dir) ->
|
2003-07-20 22:35:35 +02:00
|
|
|
case file:list_dir(Dir) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, Files} ->
|
|
|
|
MsgFiles = lists:filter(fun (FN) ->
|
|
|
|
case length(FN) > 4 of
|
|
|
|
true ->
|
|
|
|
string:substr(FN, length(FN) - 3)
|
|
|
|
== ".msg";
|
|
|
|
_ -> false
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
Files),
|
|
|
|
lists:foreach(fun (FNS) ->
|
|
|
|
FN = list_to_binary(FNS),
|
|
|
|
LP = ascii_tolower(str:substr(FN, 1,
|
|
|
|
byte_size(FN) - 4)),
|
|
|
|
L = case str:tokens(LP, <<".">>) of
|
|
|
|
[Language] -> Language;
|
|
|
|
[Language, _Project] -> Language
|
|
|
|
end,
|
|
|
|
load_file(L, <<Dir/binary, "/", FN/binary>>)
|
|
|
|
end,
|
|
|
|
MsgFiles),
|
|
|
|
ok;
|
|
|
|
{error, Reason} -> ?ERROR_MSG("~p", [Reason])
|
2003-07-20 22:35:35 +02:00
|
|
|
end.
|
2003-01-06 20:57:05 +01:00
|
|
|
|
|
|
|
load_file(Lang, File) ->
|
2014-04-15 16:16:12 +02:00
|
|
|
case file:open(File, [read]) of
|
|
|
|
{ok, Fd} ->
|
2014-04-15 16:58:06 +02:00
|
|
|
io:setopts(Fd, [{encoding,latin1}]),
|
2014-04-15 16:16:12 +02:00
|
|
|
load_file_loop(Fd, 1, File, Lang),
|
|
|
|
file:close(Fd);
|
2015-03-27 15:55:57 +01:00
|
|
|
{error, Error} ->
|
2014-04-15 16:16:12 +02:00
|
|
|
ExitText = iolist_to_binary([File, ": ",
|
|
|
|
file:format_error(Error)]),
|
|
|
|
?ERROR_MSG("Problem loading translation file ~n~s",
|
|
|
|
[ExitText]),
|
|
|
|
exit(ExitText)
|
|
|
|
end.
|
|
|
|
|
|
|
|
load_file_loop(Fd, Line, File, Lang) ->
|
|
|
|
case io:read(Fd, '', Line) of
|
|
|
|
{ok,{Orig, Trans}, NextLine} ->
|
|
|
|
Trans1 = case Trans of
|
|
|
|
<<"">> -> Orig;
|
|
|
|
_ -> Trans
|
|
|
|
end,
|
|
|
|
ets:insert(translations,
|
|
|
|
{{Lang, iolist_to_binary(Orig)},
|
|
|
|
iolist_to_binary(Trans1)}),
|
|
|
|
|
|
|
|
load_file_loop(Fd, NextLine, File, Lang);
|
|
|
|
{ok,_, _NextLine} ->
|
|
|
|
ExitText = iolist_to_binary([File,
|
|
|
|
" approximately in the line ",
|
|
|
|
Line]),
|
|
|
|
?ERROR_MSG("Problem loading translation file ~n~s",
|
|
|
|
[ExitText]),
|
|
|
|
exit(ExitText);
|
|
|
|
{error,
|
|
|
|
{_LineNumber, erl_parse, _ParseMessage} = Reason} ->
|
|
|
|
ExitText = iolist_to_binary([File,
|
|
|
|
" approximately in the line ",
|
|
|
|
file:format_error(Reason)]),
|
|
|
|
?ERROR_MSG("Problem loading translation file ~n~s",
|
|
|
|
[ExitText]),
|
|
|
|
exit(ExitText);
|
|
|
|
{error, Reason} ->
|
|
|
|
ExitText = iolist_to_binary([File, ": ",
|
|
|
|
file:format_error(Reason)]),
|
|
|
|
?ERROR_MSG("Problem loading translation file ~n~s",
|
|
|
|
[ExitText]),
|
|
|
|
exit(ExitText);
|
|
|
|
{eof,_Line} ->
|
|
|
|
ok
|
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.
|
|
|
|
|
|
|
|
translate(Msg) ->
|
|
|
|
case ?MYLANG 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.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
ascii_tolower(B) ->
|
|
|
|
iolist_to_binary(ascii_tolower_s(binary_to_list(B))).
|
2004-09-30 23:54:39 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
ascii_tolower_s([C | Cs]) when C >= $A, C =< $Z ->
|
|
|
|
[C + ($a - $A) | ascii_tolower_s(Cs)];
|
|
|
|
ascii_tolower_s([C | Cs]) -> [C | ascii_tolower_s(Cs)];
|
|
|
|
ascii_tolower_s([]) -> [].
|