def test_different_tagname(self): res = I18nPreprocessor().process( '<div>{{# hello }}{{name}}{{/ hello }}</div>') self.assertEqual(res, '<div>{{name}}</div>') res = I18nPreprocessor().process('<div>{{# _ }}{{name}}{{/ _ }}</div>') self.assertEqual(res, '<div>{{# _ }}{{name}}{{/ _ }}</div>')
def templatize(src, origin=None): # Get all the paths that we know about paths = [os.path.abspath(path) for name, path in find('(.*)')] # Hijack the process if the file we're talking about is in one of the # finder paths. if origin and os.path.abspath(origin) in paths: with open(origin) as template_file: # Load the template content. content = template_file.read() # Find all the translatable strings. processor = I18nPreprocessor() pattern = re.compile(processor.trans_re, flags=re.DOTALL) strings = set(re.findall(pattern, content)) def escape(s): s = s.replace('\\', '\\\\') s = s.replace('"', r'\"') return s # Build a string that looks like a Python file that's ready to be # translated. translatable = '\n'.join( ['_("""{0}""")'.format(escape(string)) for string in strings]) return translatable # If the file isn't in one of our paths, then delegate to the original # method. else: return base_templatize(src, origin)
def test_string_with_newlines_translation(self): translation.activate('fr') res = I18nPreprocessor().process( '<div>{{# _ }}Hello, \n{{name}}\n!{{/ _ }} {{# _ }}Second string{{/ _ }}</div>' ) self.assertEqual(res, '<div>XXX Hello, \n{{name}}\n! XXX Second string</div>')
def test_simple_string_translation(self): translation.activate('fr') res = I18nPreprocessor().process( '<div>{{# _ }}Hello, {{name}}!{{/ _ }}</div>' ) self.assertEqual(res, '<div>XXX Hello, {{name}}!</div>')
def templatize(src, origin=None): # Get all the paths that we know about paths = [os.path.abspath(path) for name, path in find('(.*)')] # Hijack the process if the file we're talking about is in one of the # finder paths. if origin and os.path.abspath(origin) in paths: with open(origin) as template_file: # Load the template content. content = template_file.read() # Find all the translatable strings. processor = I18nPreprocessor() short_pattern = re.compile(processor.short_trans_re) long_pattern = re.compile(processor.long_trans_re, re.DOTALL) strings = set() for match in chain(re.finditer(short_pattern, content), re.finditer(long_pattern, content)): strings.add(match.group('msg')) def escape(s): s = s.replace('\\', '\\\\') s = s.replace('"', r'\"') return s # Build a string that looks like a Python file that's ready to be # translated. translatable = '\n'.join( sorted('_("""{0}""")'.format(escape(string)) for string in strings)) # override jstemplate makemessages.py with force_text return force_text(translatable) # If the file isn't in one of our paths, then delegate to the original # method. else: return base_templatize(src, origin)
def test_short_style_ignore_mismatched_quotes(self): res = I18nPreprocessor().process('<div>{{_ "\'}}</div>') self.assertEqual(res, '<div>{{_ "\'}}</div>')
def test_short_style_no_string(self): res = I18nPreprocessor().process('<div>{{_ ""}}</div>') self.assertEqual(res, '<div>{{""}}</div>')
def test_short_style_simple_string_translation(self): translation.activate('fr') res = I18nPreprocessor().process('<div>{{ _ "Hello, world!" }}</div>') self.assertEqual(res, '<div>{{ "XXX Hello, world!" }}</div>')
def test_no_string(self): res = I18nPreprocessor().process('<div>{{# _ }}{{/ _ }}</div>') self.assertEqual(res, '<div></div>')