aboutsummaryrefslogtreecommitdiff
path: root/src/translate.erl
blob: a6cc8c146457b731a0b017c623e6723ce724c9c0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
%%%----------------------------------------------------------------------
%%% 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]),
    Dir = 
	case os:getenv("EJABBERD_MSGS_PATH") of
	    false ->
		case code:priv_dir(ejabberd) of
		    {error, _} ->
			?MSGS_DIR;
		    Path ->
			filename:join([Path, "msgs"])
		end;
	    Path ->
		Path
	end,
    load_dir(Dir),
    ok.

load_dir(Dir) ->
    case file:list_dir(Dir) of
	{ok, Files} ->
	    MsgFiles = lists:filter(
			 fun(FN) ->
				 case string:len(FN) > 4 of
				     true ->
					 string:substr(
					   FN,
					   string:len(FN) - 3) == ".msg";
				     _ ->
					 false
				 end
			 end, Files),
	    lists:foreach(
	      fun(FN) ->
		      L = ascii_tolower(
			    string:substr(FN, 1, string:len(FN) - 4)),
		      load_file(L, Dir ++ "/" ++ FN)
	      end, MsgFiles),
	    ok;
	{error, Reason} ->
	    ?ERROR_MSG("~p", [Reason])
    end.

load_file(Lang, File) ->
    case file:consult(File) of
	{ok, Terms} ->
	    lists:foreach(fun({Orig, Trans}) ->
			      Trans1 = case Trans of
					   "" ->
					       Orig;
					   _ ->
					       Trans
				       end,
			      ets:insert(translations,
					     {{Lang, Orig}, Trans1})
			  end, Terms);
	{error, Reason} ->
	    exit(file:format_error(Reason))
    end.

translate(Lang, Msg) ->
    LLang = ascii_tolower(Lang),
    case ets:lookup(translations, {LLang, Msg}) of
	[{_, Trans}] ->
	    Trans;
	_ ->
	    ShortLang = case string: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
    end.

translate(Msg) ->
    case ?MYLANG of
	undefined ->
	    Msg;
	"en" ->
	    Msg;
	Lang ->
	    LLang = ascii_tolower(Lang),
	    case ets:lookup(translations, {LLang, Msg}) of
		[{_, Trans}] ->
		    Trans;
		_ ->
		    ShortLang = case string: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
    end.

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([]) ->
    [].