Permalink
Browse files

Merge pull request #8 from bipthelin/master

Added support for app.config
  • Loading branch information...
2 parents 2db7a5f + 222cc31 commit e3893dee0e15009f25479978cefdc7dc807fd7ec Torbjörn Törnkvist committed Dec 13, 2011
Showing with 26 additions and 6 deletions.
  1. +25 −5 src/gettext_server.erl
  2. +1 −1 src/gettext_sup.erl
View
@@ -120,10 +120,11 @@ start(CallBackMod, Name) ->
%% ignore |
%% {stop, Reason}
%%--------------------------------------------------------------------
-init([CallBackMod0, Name]) ->
+init([CallBackModConfig, Name]) ->
+ {CallBackMod0, Config} = CallBackModConfig,
CallBackMod = get_callback_mod(CallBackMod0),
- GettextDir = get_gettext_dir(CallBackMod),
- DefLang = get_default_lang(CallBackMod),
+ GettextDir = get_gettext_dir(CallBackMod, Config),
+ DefLang = get_default_lang(CallBackMod, Config),
TableNameStr = atom_to_list(Name) ++ "_db",
TableName = list_to_atom(TableNameStr),
Cache = create_db(TableName, GettextDir),
@@ -184,7 +185,8 @@ get_default_lang(CallBackMod) ->
%%--------------------------------------------------------------------
handle_call({key2str, Key, Lang}, _From, State) ->
TableName = State#state.table_name,
- Reply = lookup(TableName, Lang, Key),
+ DefaultLang = State#state.def_lang,
+ Reply = lookup(TableName, Lang, DefaultLang, Key),
{reply, Reply, State};
%%
handle_call({lang2cset, Lang}, _From, State) ->
@@ -272,6 +274,18 @@ code_change(_OldVsn, State, _Extra) ->
%%% Internal functions
%%--------------------------------------------------------------------
+get_default_lang(CallBackMod, Config) ->
+ case proplists:get_value(default_lang, Config) of
+ undefined -> get_default_lang(CallBackMod);
+ ConfLang -> ConfLang
+ end.
+
+get_gettext_dir(CallBackMod, Config) ->
+ case proplists:get_value(gettext_dir, Config) of
+ undefined -> get_gettext_dir(CallBackMod);
+ ConfDir -> ConfDir
+ end.
+
db_filename(TableName, GettextDir) ->
filename:join(GettextDir, atom_to_list(TableName) ++ ".dets").
@@ -487,8 +501,14 @@ insert(TableName, LC, L) ->
lists:foreach(F, L).
lookup(TableName, Lang, Key) ->
+ lookup(TableName, Lang, Lang, Key).
+
+lookup(TableName, Lang, DefaultLang, Key) ->
try ets:lookup(get(ets_table), ?KEY(Lang, Key)) of
- [] -> Key;
+ [] -> case string:equal(Lang, DefaultLang) of
+ true -> Key;
+ false -> lookup(TableName, DefaultLang, Key)
+ end;
[?ENTRY(_,_,Str)|_] -> Str
catch
_:_ ->
View
@@ -52,7 +52,7 @@ start_link(Options) ->
%% {error, Reason}
%%----------------------------------------------------------------------
init([]) ->
- init([gettext:callback_mod()]); % just a default that should always work
+ init([{gettext:callback_mod(),application:get_all_env()}]); % just a default that should always work
init([CallBackMod]) ->
GettextServer = {gettext_server,{gettext_server,start_link,[CallBackMod]},
permanent,5000,worker,[gettext_server]},

0 comments on commit e3893de

Please sign in to comment.