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]),
|
|
|
|
load_dir(?MSGS_DIR),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
load_dir(Dir) ->
|
|
|
|
{ok, Files} = file:list_dir(Dir),
|
|
|
|
MsgFiles = lists:filter(
|
|
|
|
fun(FN) ->
|
2003-01-06 21:05:48 +01:00
|
|
|
case string:len(FN) > 4 of
|
|
|
|
true ->
|
|
|
|
string:substr(FN,
|
|
|
|
string:len(FN) - 3) == ".msg";
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
2003-01-06 20:57:05 +01:00
|
|
|
end, Files),
|
|
|
|
lists:foreach(
|
|
|
|
fun(FN) ->
|
|
|
|
load_file(string:substr(FN, 1, string:len(FN) - 4),
|
|
|
|
Dir ++ "/" ++ FN)
|
|
|
|
end, MsgFiles),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
load_file(Lang, File) ->
|
2003-01-16 21:24:53 +01:00
|
|
|
case file:consult(File) of
|
|
|
|
{ok, Terms} ->
|
|
|
|
lists:foreach(fun({Orig, Trans}) ->
|
|
|
|
ets:insert(translations,
|
|
|
|
{{Lang, Orig}, Trans})
|
|
|
|
end, Terms);
|
|
|
|
{error, Reason} ->
|
|
|
|
exit(file:format_error(Reason))
|
2003-01-06 20:57:05 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
translate(Lang, Msg) ->
|
|
|
|
case ets:lookup(translations, {Lang, Msg}) of
|
|
|
|
[{_, Trans}] ->
|
|
|
|
Trans;
|
|
|
|
_ ->
|
|
|
|
Msg
|
|
|
|
end.
|
|
|
|
|