Browse Source

keep noop trans tags in parsed AST tree (fixes #162)

Andreas Stenius 11 years ago
parent
commit
3358f3474a

+ 2 - 0
src/erlydtl_beam_compiler.erl

@@ -876,6 +876,8 @@ phrase_locale(Locale, Context) -> {Locale, Context}.
 translated_ast(Text, TreeWalker) ->
     translated_ast(Text, undefined, TreeWalker).
 
+translated_ast({noop, Value}, _, TreeWalker) ->
+    value_ast(Value, true, true, TreeWalker);
 translated_ast(Text, {string_literal, _, Context}, TreeWalker) ->
     translated_ast(Text, unescape_string_literal(Context), TreeWalker);
 translated_ast({string_literal, _, String}, Context, TreeWalker) ->

+ 6 - 3
src/erlydtl_parser.yrl

@@ -123,6 +123,7 @@ Nonterminals
     TransTag
     TransArgs
     TransText
+    TransValue
 
     TemplatetagTag
     Templatetag
@@ -423,13 +424,15 @@ Templatetag -> closecomment_keyword : '$1'.
 
 TransTag -> open_tag trans_keyword TransArgs close_tag : '$3'.
 TransTag -> open_tag trans_keyword TransArgs as_keyword identifier close_tag : {scope_as, '$5', ['$3']}.
-TransTag -> open_tag trans_keyword TransArgs noop_keyword close_tag : element(2, '$3').
 
 TransArgs -> TransText : {trans, '$1'}.
 TransArgs -> TransText context_keyword string_literal: {trans, '$1', '$3'}.
 
-TransText -> string_literal : '$1'.
-TransText -> Variable : '$1'.
+TransText -> TransValue : '$1'.
+TransText -> TransValue noop_keyword : {noop, '$1'}.
+
+TransValue -> string_literal : '$1'.
+TransValue -> Variable : '$1'.
 
 WidthRatioTag -> open_tag widthratio_keyword Value Value number_literal close_tag : {widthratio, '$3', '$4', '$5'}.
 

+ 2 - 2
src/erlydtl_scanner.erl

@@ -36,7 +36,7 @@
 %%%-------------------------------------------------------------------
 -module(erlydtl_scanner).
 
-%% This file was generated 2014-04-10 14:54:46 UTC by slex 0.2.1.
+%% This file was generated 2014-04-15 19:15:09 UTC by slex 0.2.1.
 %% http://github.com/erlydtl/slex
 -slex_source(["src/erlydtl_scanner.slex"]).
 
@@ -89,9 +89,9 @@ is_keyword(any, "with") -> true;
 is_keyword(any, "from") -> true;
 is_keyword(any, "count") -> true;
 is_keyword(any, "context") -> true;
+is_keyword(any, "noop") -> true;
 is_keyword(close, "only") -> true;
 is_keyword(close, "parsed") -> true;
-is_keyword(close, "noop") -> true;
 is_keyword(close, "reversed") -> true;
 is_keyword(close, "openblock") -> true;
 is_keyword(close, "closeblock") -> true;

+ 1 - 1
src/erlydtl_scanner.slex

@@ -311,10 +311,10 @@ form \
   is_keyword(any, "from") -> true; \
   is_keyword(any, "count") -> true; \
   is_keyword(any, "context") -> true; \
+  is_keyword(any, "noop") -> true; \
   \
   is_keyword(close, "only") -> true; \
   is_keyword(close, "parsed") -> true; \
-  is_keyword(close, "noop") -> true; \
   is_keyword(close, "reversed") -> true; \
   is_keyword(close, "openblock") -> true; \
   is_keyword(close, "closeblock") -> true; \

+ 8 - 2
src/i18n/sources_parser.erl

@@ -149,7 +149,8 @@ process_ast(Fname, Token, State) ->
 
 %%Block are recursivelly processed, trans are accumulated and other tags are ignored
 process_token(Fname, {block,{identifier,{_Line,_Col},_Identifier},Children}, St) -> process_ast(Fname, Children, St);
-process_token(Fname, {trans,{string_literal,{Line,Col},String}}, #state{acc=Acc, translators_comment=Comment}=St) ->
+process_token(Fname, {trans,Text}, #state{acc=Acc, translators_comment=Comment}=St) ->
+    {{Line, Col}, String} = trans(Text),
     Phrase = #phrase{msgid=unescape(String),
                      comment=Comment,
                      file=Fname,
@@ -157,8 +158,9 @@ process_token(Fname, {trans,{string_literal,{Line,Col},String}}, #state{acc=Acc,
                      col=Col},
     St#state{acc=[Phrase | Acc], translators_comment=undefined};
 process_token(Fname,
-              {trans,{string_literal,{Line,Col}, String}, {string_literal, _, Context}},
+              {trans,Text,{string_literal, _, Context}},
               #state{acc=Acc, translators_comment=Comment}=St) ->
+    {{Line, Col}, String} = trans(Text),
     Phrase = #phrase{msgid=unescape(String),
                      context=unescape(Context),
                      comment=Comment,
@@ -190,6 +192,10 @@ process_token(Fname, {_Instr, _Cond, Children, Children2}, St) ->
     process_ast(Fname, Children2, StModified);
 process_token(_,_AST,St) -> St.
 
+trans({noop, Value}) ->
+    trans(Value);
+trans({string_literal,Pos,String}) -> {Pos, String}.
+
 unescape(String) -> string:sub_string(String, 2, string:len(String) -1).
 
 unparse(undefined) -> undefined;

+ 3 - 0
test/erlydtl_test_defs.erl

@@ -1350,6 +1350,9 @@ all_test_defs() ->
                                   end
                           end}],
         <<"test ok">>},
+       {"trans context noop",
+        <<"{% trans 'message' noop context 'foo' %}">>, [], [],
+        <<"message">>},
        {"blocktrans context (run-time)",
         <<"{% blocktrans context 'bar' %}translate this{% endblocktrans %}">>,
         [], [{locale, "foo"}, {translation_fun,

+ 5 - 2
test/sources_parser_tests.erl

@@ -66,9 +66,12 @@ ext_test_defs() ->
      {"trans with context",
       <<"{% trans 'msg' context 'ctxt' %}">>,
       {[msgid, context], [["msg", "ctxt"]]}},
-     {"trans noop is empty",
+     {"trans noop",
       <<"{% trans 'msg' noop %}">>,
-      {[msgid], []}}
+      {[msgid], [["msg"]]}},
+     {"trans noop with context",
+      <<"{% trans 'msg' noop context 'ctxt' %}">>,
+      {[msgid, context], [["msg", "ctxt"]]}}
     ].
 
 unparser_test_() ->