From b3b83622d51b6200c697dd85a7c39a25c88a66d8 Mon Sep 17 00:00:00 2001
From: Yusei Tahara <yusei@nexedi.com>
Date: Wed, 20 Feb 2008 14:31:44 +0000
Subject: [PATCH] Support message id extraction from page template.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@19436 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 .../ExtensionTemplateItem/Glossary.py         | 57 +++++++++++++++++++
 .../Base_findStaticTranslationText.xml        | 37 ++++++++++++
 ...slatedMessageListFromEntireSystemAsPot.xml | 20 ++++---
 bt5/erp5_forge/bt/revision                    |  2 +-
 .../bt/template_catalog_local_role_key_list   |  0
 .../bt/template_catalog_role_key_list         |  0
 6 files changed, 108 insertions(+), 8 deletions(-)
 create mode 100644 bt5/erp5_forge/SkinTemplateItem/portal_skins/erp5_toolbox/Base_findStaticTranslationText.xml
 create mode 100644 bt5/erp5_forge/bt/template_catalog_local_role_key_list
 create mode 100644 bt5/erp5_forge/bt/template_catalog_role_key_list

diff --git a/bt5/erp5_forge/ExtensionTemplateItem/Glossary.py b/bt5/erp5_forge/ExtensionTemplateItem/Glossary.py
index 6715f2868c..4a8c66c8eb 100644
--- a/bt5/erp5_forge/ExtensionTemplateItem/Glossary.py
+++ b/bt5/erp5_forge/ExtensionTemplateItem/Glossary.py
@@ -27,3 +27,60 @@ def getActionTitleListFromAllActionProvider(portal):
   for action in provider.listActions():
       result[action.title] = None
   return result.keys()
+
+
+from StringIO import StringIO
+from TAL.HTMLTALParser import HTMLTALParser
+def findStaticTranslationText(page_template):
+  def iterate(node, target_name, function):
+    if type(node) is list:
+      for i in node:
+        iterate(i, target_name, function)
+    elif type(node) is tuple and node:
+      if node[0]==target_name:
+        function(node)
+      else:
+        for i in node[1:]:
+          iterate(i, target_name, function)
+
+  text_dict = {}
+  def addText(node):
+    if len(node)!=2:
+      node = (node[0], node[1:])
+    program = [node]
+    macros = {}
+    engine = MyDummyEngine(macros)
+    output = StringIO()
+    interpreter = MyDummyTALInterpreter(program, macros, engine, output)
+    interpreter()
+    if interpreter._i18n_message_id_dict is not None:
+      text_dict.update(interpreter._i18n_message_id_dict)
+
+  parser = HTMLTALParser()
+  parser.parseString(page_template._text)
+  iterate(parser.gen.program, 'insertTranslation', addText)
+  return text_dict.keys()
+
+#
+# Utility class for findStaticTranslationText
+#
+from TAL.TALInterpreter import TALInterpreter
+from TAL.DummyEngine import DummyEngine
+
+class MyDummyEngine(DummyEngine):
+  
+  def evaluate(self, expression):
+    return None
+
+class MyDummyTALInterpreter(TALInterpreter):
+
+  _i18n_message_id_dict = None
+  _currentTag = None
+
+  def translate(self, msgid, default, i18ndict, obj):
+    try:
+      self._i18n_message_id_dict[msgid] = None
+    except TypeError:
+      self._i18n_message_id_dict = {msgid:None}
+
+    return TALInterpreter.translate(self, msgid, default, i18ndict, obj)
diff --git a/bt5/erp5_forge/SkinTemplateItem/portal_skins/erp5_toolbox/Base_findStaticTranslationText.xml b/bt5/erp5_forge/SkinTemplateItem/portal_skins/erp5_toolbox/Base_findStaticTranslationText.xml
new file mode 100644
index 0000000000..6e2c431395
--- /dev/null
+++ b/bt5/erp5_forge/SkinTemplateItem/portal_skins/erp5_toolbox/Base_findStaticTranslationText.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0"?>
+<ZopeData>
+  <record id="1" aka="AAAAAAAAAAE=">
+    <pickle>
+      <tuple>
+        <global name="ExternalMethod" module="Products.ExternalMethod.ExternalMethod"/>
+        <tuple/>
+      </tuple>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <item>
+            <key> <string>__ac_local_roles__</string> </key>
+            <value>
+              <none/>
+            </value>
+        </item>
+        <item>
+            <key> <string>_function</string> </key>
+            <value> <string>findStaticTranslationText</string> </value>
+        </item>
+        <item>
+            <key> <string>_module</string> </key>
+            <value> <string>Glossary</string> </value>
+        </item>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>Base_findStaticTranslationText</string> </value>
+        </item>
+        <item>
+            <key> <string>title</string> </key>
+            <value> <string></string> </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
diff --git a/bt5/erp5_forge/SkinTemplateItem/portal_skins/erp5_toolbox/ERP5Site_getToBeTranslatedMessageListFromEntireSystemAsPot.xml b/bt5/erp5_forge/SkinTemplateItem/portal_skins/erp5_toolbox/ERP5Site_getToBeTranslatedMessageListFromEntireSystemAsPot.xml
index 35df645fa2..6952c76b57 100644
--- a/bt5/erp5_forge/SkinTemplateItem/portal_skins/erp5_toolbox/ERP5Site_getToBeTranslatedMessageListFromEntireSystemAsPot.xml
+++ b/bt5/erp5_forge/SkinTemplateItem/portal_skins/erp5_toolbox/ERP5Site_getToBeTranslatedMessageListFromEntireSystemAsPot.xml
@@ -83,7 +83,7 @@ portal_url = context.portal_url\n
 python_script_list = []\n
 form_list = []\n
 listbox_list = []\n
-zpt_list = []\n
+page_template_list = []\n
 def iterate(obj):\n
   for i in obj.objectValues():\n
     if i.meta_type==\'Script (Python)\':\n
@@ -92,8 +92,8 @@ def iterate(obj):\n
       form_list.append(i)\n
     elif i.meta_type==\'ListBox\' or i.id==\'listbox\':\n
       listbox_list.append(i)\n
-    elif i.meta_type==\'Page Template\':\n
-      zpt_list.append(i)\n
+    elif i.meta_type==\'Page Template\' and i.content_type.startswith(\'text/html\'):\n
+      page_template_list.append(i)\n
     if i.isPrincipiaFolderish:\n
       iterate(i)\n
 iterate(context.portal_skins)\n
@@ -107,12 +107,13 @@ FUNC_NAME_LIST = (\'N_\',\n
                   \'translateString\',\n
                   )\n
 \n
+Base_getFunctionFirstArgumentValue = context.Base_getFunctionFirstArgumentValue\n
 for i in python_script_list:\n
   source = i.body()\n
   for func_name in FUNC_NAME_LIST:\n
     call_func_name = \'%s(\' % func_name\n
     if call_func_name in source:\n
-      for m in context.Base_getFunctionFirstArgumentValue(func_name, source):\n
+      for m in Base_getFunctionFirstArgumentValue(func_name, source):\n
         add_message(m, portal_url.getRelativeContentURL(i))\n
 \n
 \n
@@ -134,9 +135,12 @@ for i in listbox_list:\n
     add_message(label, portal_url.getRelativeContentURL(i))\n
 \n
 #\n
-# ZPT\n
+# Page Template\n
 #\n
-# TODO : To parse html and get static translated text.\n
+Base_findStaticTranslationText = context.Base_findStaticTranslationText\n
+for i in page_template_list:\n
+  for m in Base_findStaticTranslationText(i):\n
+    add_message(m, portal_url.getRelativeContentURL(i))\n
 \n
 #\n
 # Workflow\n
@@ -254,9 +258,10 @@ return printed\n
                             <string>python_script_list</string>
                             <string>form_list</string>
                             <string>listbox_list</string>
-                            <string>zpt_list</string>
+                            <string>page_template_list</string>
                             <string>iterate</string>
                             <string>FUNC_NAME_LIST</string>
+                            <string>Base_getFunctionFirstArgumentValue</string>
                             <string>_getiter_</string>
                             <string>i</string>
                             <string>source</string>
@@ -265,6 +270,7 @@ return printed\n
                             <string>m</string>
                             <string>value</string>
                             <string>label</string>
+                            <string>Base_findStaticTranslationText</string>
                             <string>s</string>
                             <string>t</string>
                             <string>action_title</string>
diff --git a/bt5/erp5_forge/bt/revision b/bt5/erp5_forge/bt/revision
index 4c009fb2fb..6e16ebf9e9 100644
--- a/bt5/erp5_forge/bt/revision
+++ b/bt5/erp5_forge/bt/revision
@@ -1 +1 @@
-206
\ No newline at end of file
+208
\ No newline at end of file
diff --git a/bt5/erp5_forge/bt/template_catalog_local_role_key_list b/bt5/erp5_forge/bt/template_catalog_local_role_key_list
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/bt5/erp5_forge/bt/template_catalog_role_key_list b/bt5/erp5_forge/bt/template_catalog_role_key_list
new file mode 100644
index 0000000000..e69de29bb2
-- 
2.30.9