Browse Source

pass translation function into 'date' filter.

oxpa 9 years ago
parent
commit
7be5db238a
3 changed files with 20 additions and 10 deletions
  1. 5 5
      src/erlydtl_beam_compiler.erl
  2. 11 4
      src/erlydtl_filters.erl
  3. 4 1
      src/filter_lib/erlydtl_dateformat.erl

+ 5 - 5
src/erlydtl_beam_compiler.erl

@@ -1123,10 +1123,10 @@ filter_ast1({{identifier, Pos, Name}, Args}, ValueAst, TreeWalker) ->
 filter_ast2(Name, Args, #dtl_context{ filters = Filters }) ->
     case proplists:get_value(Name, Filters) of
         {Mod, Fun}=Filter ->
-            case erlang:function_exported(Mod, Fun, length(Args)) of
-                true -> {ok, ?Q("'@Mod@':'@Fun@'(_@Args)")};
-                false ->
-                    {filter_args, Name, Filter, length(Args)}
+            case {erlang:function_exported(Mod, Fun, length(Args)), Name} of
+                {true, 'date'} -> {ok, ?Q("'@Mod@':'@Fun@'(_@Args, _TranslationFun)")};
+                {true, _} -> {ok, ?Q("'@Mod@':'@Fun@'(_@Args)")};
+                {false, _} -> {filter_args, Name, Filter, length(Args)}
             end;
         undefined ->
             {unknown_filter, Name, length(Args)}
@@ -1483,7 +1483,7 @@ now_ast(FormatString, TreeWalker) ->
     %% i.e. \"foo\" becomes "foo"
     UnescapeOuter = string:strip(FormatString, both, 34),
     {{StringAst, Info}, TreeWalker1} = string_ast(UnescapeOuter, TreeWalker),
-    {{?Q("erlydtl_dateformat:format(_@StringAst)"), Info}, TreeWalker1}.
+    {{?Q("erlydtl_dateformat:format(_@StringAst, _TranslationFun)"), Info}, TreeWalker1}.
 
 spaceless_ast(Contents, TreeWalker) ->
     {{Ast, Info}, TreeWalker1} = body_ast(Contents, TreeWalker),

+ 11 - 4
src/erlydtl_filters.erl

@@ -56,6 +56,7 @@
         cut/2,
         date/1,
         date/2,
+        date/3,
         default/2,
         default_if_none/2,
         dictsort/2,
@@ -236,14 +237,20 @@ cut(Input, [Char]) when is_list(Input) ->
  
 %% @doc Formats a date according to the default format.
 date(Input) ->
-    date(Input, "F j, Y").
+    date(Input, fun(A) -> A end).
+
+%% @doc Formats a date according to the default format. 
+%% @doc Translating tokens with function F
+date(Input, F) when is_function(F)->
+    date(Input, "F j, Y", F).
 
 %% @doc Formats a date according to the given format.
-date(Input, FormatStr)
+date(Input, FormatStr, F)
   when is_tuple(Input)
-       andalso (size(Input) == 2 orelse size(Input) == 3) ->
+       andalso (size(Input) == 2 orelse size(Input) == 3)
+       andalso is_function(F) ->
     erlydtl_dateformat:format(Input, FormatStr);
-date(Input, _FormatStr) ->
+date(Input, _FormatStr, _F) ->
     io:format("Unexpected date parameter: ~p~n", [Input]),
     "".
 

+ 4 - 1
src/filter_lib/erlydtl_dateformat.erl

@@ -1,5 +1,5 @@
 -module(erlydtl_dateformat).
--export([format/1, format/2]).
+-export([format/1, format/2, format/3]).
 
 -define(TAG_SUPPORTED(C),
         C =:= $a orelse
@@ -63,6 +63,9 @@ format({{_,_,_} = Date,{_,_,_} = Time}, FormatString) ->
 %%
 format({_,_,_} = Date, FormatString) ->
     replace_tags(Date, {0,0,0}, FormatString);
+format(DateTime, FormatString, TransFun) ->
+    io:format("Translations are not yet supported", []),
+    FormatString.
 format(DateTime, FormatString) ->
     io:format("Unrecognised date paramater : ~p~n", [DateTime]),
     FormatString.