Commits

Ratnadeep Debnath committed be08dfb

Better escaping/unescaping of quotes in Joomla INI handler.

  • Participants
  • Parent commits d003be7

Comments (0)

Files changed (2)

File transifex/resources/formats/joomla.py

     def _escape(self, s):
         return s.replace('\\', '\\\\')
 
+    def _examine_content(self, content):
+        self.jformat = JoomlaIniVersion.create(content)
+        return content
+
+    def _replace_translation(self, original, replacement, text):
+        return re.sub(re.escape(original),
+            self._pseudo_decorate(self._escape(self.jformat.get_compilation(replacement))), text)
+
     def _parse(self, is_source, lang_rules):
         """
         Parse an INI file and create a stringset with all entries in the file.
         """
         content = self.content
-        jformat = JoomlaIniVersion.create(self.content)
+        self.jformat = JoomlaIniVersion.create(self.content)
         self._find_linesep(content)
 
         buf = ''
                 logger.warning('Could not parse line "%s". Skipping...' % line)
                 continue
 
-            trans = jformat.get_translation(trans)
+            trans = self.jformat.get_translation(trans)
             context = ""        # We use empty context
 
             if is_source:
 class JoomlaIniOld(JoomlaIniVersion):
     """Format for Joomla 1.5."""
 
+    def _escape(self, value):
+        return value.replace('"', """)
+
+    def _unescape(self, value):
+        return value.replace(""", '"')
+
     def get_translation(self, value):
-        return value
+        return self._unescape(value)
+
+    def get_compilation(self, value):
+        return self._escape(value)
 
 
 class JoomlaIniNew(JoomlaIniVersion):
     """Format for Joomla 1.6."""
+    def _escape(self, value):
+        return value.replace('"', '"_QQ_"')
+
+    def _unescape(self, value):
+        return value.replace(""", '"').replace('"_QQ_"', '"')
 
     def get_translation(self, value):
         # Get rid of double-quote at the start and end of value
-        return value[1:-1]
+        return self._unescape(value[1:-1])
+
+    def get_compilation(self, value):
+        return self._escape(value)

File transifex/resources/tests/lib/joomla_ini/__init__.py

         self.assertEquals(self.parser.template.count('tr'), 1)
         self.assertTrue('=' in self.parser.template)
         self.assertTrue('tr' in self.parser.template)
+
+    def test_translation_escaping(self):
+        """Test for a Joomla 1.6 INI file"""
+        content = ';test\nKEY1="Translation with "_QQ_"quotes"_QQ_""'\
+                  '\nKEY2="Translation with a quote ""'
+        self.parser.bind_content(content)
+        self.parser.parse_file(is_source=True)
+        translation_strings = []
+        for s in self.parser.stringset.strings:
+            translation_strings.append(s.translation)
+        self.assertEqual(translation_strings, ['Translation with "quotes"',
+                'Translation with a quote "'])
+
+        """Test for a Joomla 1.5 INI file"""
+        content = 'KEY1=Translation with "quotes"'\
+                  '\nKEY2=Translation with a quote "'
+        self.parser.bind_content(content)
+        self.parser.parse_file(is_source=True)
+        translation_strings = []
+        for s in self.parser.stringset.strings:
+            translation_strings.append(s.translation)
+        self.assertEqual(translation_strings, ['Translation with "quotes"',
+                'Translation with a quote "'])
+
+