Browse files

I18n support for controllers and libraries.

Erlang modules are now compiled with debug_info and abstract code included in beams
is scanned for boss_translator:trans/3 calls.
  • Loading branch information...
1 parent 755ed3c commit 055b88c0838457094e1d893bb1e2742126018cba Grzegorz Stanislawski committed Dec 2, 2011
Showing with 59 additions and 2 deletions.
  1. +1 −1 src/boss/boss_compiler.erl
  2. +50 −1 src/boss/boss_lang.erl
  3. +8 −0 src/boss/boss_translator.erl
View
2 src/boss/boss_compiler.erl
@@ -40,7 +40,7 @@ compile(File, Options) ->
end.
compile_forms(Forms, File, Options) ->
- case compile:forms(Forms, Options) of
+ case compile:forms(Forms, [debug_info | Options]) of
{ok, Module1, Bin} ->
code:purge(Module1),
case code:load_binary(Module1, File, Bin) of
View
51 src/boss/boss_lang.erl
@@ -74,7 +74,7 @@ lang_write_multiline_to_file(IODevice, [Token|Rest]) ->
lang_write_multiline_to_file(IODevice, Rest).
extract_strings(App) ->
- lists:usort(extract_model_strings(App) ++ extract_view_strings(App)).
+ lists:usort(extract_model_strings(App) ++ extract_view_strings(App) ++ extract_controller_strings(App)).
extract_strings(App, Lang) ->
AllStrings = extract_strings(App),
@@ -92,6 +92,55 @@ extract_strings(App, Lang) ->
end, lists:usort(AllStrings)),
{UntranslatedStrings, PoStrings}.
+extract_controller_strings(_App) ->
+ Modules=boss_files:module_list([boss_files:lib_path(),
+ boss_files:web_controller_path(),
+ boss_files:mail_controller_path()]),
+ lists:foldl(fun
+ (Module, Acc) ->
+ io:format("~p~n",[Module]),
+ case beam_lib:chunks(filename:join([boss_files:ebin_dir(),Module]),
+ [abstract_code]) of
+ {error,beam_lib,_} -> Acc;
+ {ok,{_,[{abstract_code,{raw_abstract_v1,AbsCode}}]}} -> scan_abs_code(AbsCode,Acc)
+ end
+ end, [], Modules).
+
+scan_abs_code([],Acc) -> Acc;
+scan_abs_code({call,_,{atom,_,_Func},Params},Acc) -> scan_abs_code(Params,Acc);
+scan_abs_code({call,_,{remote,_,{atom,_,boss_translator},{atom,_,trans}},
+ [{atom,_,_App},{string,_,String},_]},Acc) ->
+ [String|Acc];
+scan_abs_code({call,_,{remote,_,{atom,_,_Module},{atom,_,_Func}},Params},Acc) -> scan_abs_code(Params,Acc);
+scan_abs_code({call,_,{remote,_,{var,_,_Obj},{atom,_,_Func}},Params},Acc) -> scan_abs_code(Params,Acc);
+scan_abs_code({call,_,{remote,_,_,{atom,_,_Func}},Params},Acc) -> scan_abs_code(Params,Acc);
+scan_abs_code({'fun',_,{function,_,_}},Acc) -> Acc;
+scan_abs_code({'fun',_,{clauses,Clauses}},Acc) -> scan_abs_code(Clauses,Acc);
+scan_abs_code({'case',_,_expr,Clauses},Acc) -> scan_abs_code(Clauses,Acc);
+scan_abs_code({'if',_,Clauses},Acc) -> scan_abs_code(Clauses,Acc);
+scan_abs_code({'receive',_,Clauses,_timeout,TimeoutClauses},Acc) ->
+ scan_abs_code(TimeoutClauses,scan_abs_code(Clauses,Acc));
+scan_abs_code({eof,_},Acc) -> Acc;
+scan_abs_code({nil,_},Acc) -> Acc;
+scan_abs_code({atom,_,_},Acc) -> Acc;
+scan_abs_code({string,_,_},Acc) -> Acc;
+scan_abs_code({var,_,_},Acc) -> Acc;
+scan_abs_code({integer,_,_},Acc) -> Acc;
+scan_abs_code({record,_,{var,_,_},_,Elements},Acc) -> scan_abs_code(Elements,Acc);
+scan_abs_code({record_field,_,{atom,_,_},Elements},Acc) -> scan_abs_code(Elements,Acc);
+scan_abs_code({record_field,_,{var,_,_},_,Elements},Acc) -> scan_abs_code(Elements,Acc);
+scan_abs_code({tuple,_,Elements},Acc) -> scan_abs_code(Elements,Acc);
+scan_abs_code({cons,_,Param1,Param2},Acc) -> scan_abs_code(Param2,scan_abs_code(Param1,Acc));
+scan_abs_code({op,_,_opname,Param1,Param2},Acc) -> scan_abs_code(Param2,scan_abs_code(Param1,Acc));
+scan_abs_code({attribute,_,_,_},Acc) -> Acc;
+scan_abs_code({match,_,_,_},Acc) -> Acc;
+scan_abs_code({function,_,_,_,Clauses},Acc) -> scan_abs_code(Clauses,Acc);
+scan_abs_code({clause,_,_,_,Code},Acc) -> scan_abs_code(Code,Acc);
+scan_abs_code(Arg,Acc) when is_tuple(Arg) -> Acc;
+scan_abs_code([Expr|Rest],Acc) ->
+ NewAcc=scan_abs_code(Expr,Acc),
+ scan_abs_code(Rest,NewAcc).
+
extract_po_strings(App, Lang) ->
LangFile = boss_files:lang_path(App, Lang),
Tokens = po_scanner:scan(LangFile),
View
8 src/boss/boss_translator.erl
@@ -12,6 +12,8 @@
reload_all/1
]).
+-export([trans/3]).
+
start() ->
start([]).
@@ -43,3 +45,9 @@ fun_for(Pid, Locale) ->
true -> fun(String) -> ?MODULE:lookup(Pid, String, Locale) end;
false -> none
end.
+
+trans(App,T,Lang) ->
+ case boss_translator:lookup(boss_web:translator_pid(App), T, Lang) of
+ undefined -> T;
+ S-> S
+ end.

0 comments on commit 055b88c

Please sign in to comment.