Changeset 3734

Show
Ignore:
Timestamp:
11/19/07 18:25:24 (1 year ago)
Author:
chrisz
Message:

Ticket #1397.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/turbogears/command/i18n.py

    r3700 r3734  
    3636    data = open(src, 'rb').read() 
    3737    open(dest, 'wb').write(data) 
     38 
     39_str_literal = r"""(?:'((?:[^']|\\')*)'|"((?:[^"]|\\")*)")""" 
     40_py_i18n_re = re.compile(r"\b_\s*\(\s*[uU]?[rR]?%s\s*\)" % _str_literal) 
     41_js_i18n_re = re.compile(r"\b_\s*\(\s*%s\s*\)" % _str_literal) 
    3842 
    3943class InternationalizationTool: 
     
    212216            mf.close() 
    213217 
    214  
    215218    def clean_generated_files(self): 
    216219        potfile = self.get_potfile_path() 
     
    239242            print "File %s exists, use --force to override" % targetfile 
    240243 
    241  
    242244    def _copy_moduletranslation(self, sourcefile, targetdir, language): 
    243245        modulefilename = os.path.basename(sourcefile) 
     
    247249        else: 
    248250            print "%s translation for language '%s' does not exist (file searched '%s').\nPlease consider to contribute a translation." % (modulefilename, language, sourcefile) 
    249  
    250251 
    251252    def add_languages(self, codes): 
     
    270271 
    271272            self._copy_file_withcheck(potfile, catalog_file) 
    272  
    273  
    274  
    275273 
    276274    def scan_source_files(self): 
     
    290288                elif ext == '.kid': 
    291289                    kid_files.append(srcfile) 
    292                 # DBR start 
    293290                elif ext == '.js': 
    294291                    js_files.append(srcfile) 
    295                 # DBR end 
    296292                else: 
    297293                    pass # do nothing 
     
    326322        tags_to_ignore = ['script', 'style'] 
    327323        keys = [] 
    328         kid_expr_re = re.compile(r"_\(('(?P<texta>[^']*)'|\"(?P<textb>[^\"]*)\")\)") 
    329324 
    330325        def process_text(is_attribute, k, tag): 
    331                 key = None 
    332                 s = kid_expr_re.search(k) 
    333                 if s: 
    334                     key = s.groupdict()['texta'] or s.groupdict()['textb'] 
    335                 # DBR start 
    336                 # we don't have a kid expression in there 
    337                 # so it is "just" a text entry - which we want to 
    338                 # be translated!! 
    339                 elif not is_attribute: 
    340                     import kid.codewriter as cw 
    341                     parts = cw.interpolate(k) 
    342                     if isinstance(parts, list) and len(parts) > 1: 
    343                         print "Warning: Mixed content in tag <%s>: %s" % (tag, k) 
    344                     elif isinstance(parts, basestring): 
    345                         key = k 
    346                 # DBR end 
    347                 if key and (key not in keys) and (tag not in tags_to_ignore): 
    348                     messages.append((tag, fname, key)) 
    349                     keys.append(key) 
     326            key = None 
     327            s = _py_i18n_re.search(k) 
     328            if s: 
     329                key = s.group(1) or s.group(2) 
     330            elif not is_attribute: 
     331                # we don't have a kid expression in there, so it is 
     332                # "just" a text entry - which we want to be translated! 
     333                import kid.codewriter as cw 
     334                parts = cw.interpolate(k) 
     335                if isinstance(parts, list) and len(parts) > 1: 
     336                    print "Warning: Mixed content in tag <%s>: %s" % (tag, k) 
     337                elif isinstance(parts, basestring): 
     338                    key = k 
     339            if key and (key not in keys) and (tag not in tags_to_ignore): 
     340                messages.append((tag, fname, key)) 
     341                keys.append(key) 
    350342 
    351343        for fname in files: 
     
    397389 
    398390    def get_strings_in_js(self, fname): 
    399         gettext_expr_re = re.compile(r"_\(('(?P<texta>[^']*)'|\"(?P<textb>[^\"]*)\")\)") 
    400391        messages = [] 
    401392        keys = [] 
    402393        for i, line in enumerate(open(fname)): 
    403             s = gettext_expr_re.search(line) 
     394            s = _js_i18n_re.search(line) 
    404395            while s: 
    405                 key = s.groupdict()['texta'] or s.groupdict()['textb'] 
     396                key = s.group(1) or s.group(2) 
    406397                pos = s.end() 
    407398                if key and (key not in keys): 
    408399                    messages.append((i + 1, fname, key)) 
    409400                    keys.append(key) 
    410                 s = gettext_expr_re.search(line, pos) 
     401                s = _js_i18n_re.search(line, pos) 
    411402        return keys, messages 
    412403 
     
    414405        messages = [] 
    415406        keys = [] 
    416         gettext_expr_re = re.compile(r"_\(('(?P<texta>[^']*)'|\"(?P<textb>[^\"]*)\")\)") 
    417407        for fname in files: 
    418408            print 'Working on', fname 
     
    429419        fd.close() 
    430420 
    431  
    432  
    433  
    434421    def get_potfile_path(self): 
    435422        return os.path.join(self.locale_dir, '%s.pot' % self.domain) 
     423 
    436424    def get_locale_catalog(self, code): 
    437425        return os.path.join(self.locale_dir, code, 'LC_MESSAGES', '%s.po' % self.domain) 
     426 
    438427    def list_message_catalogs(self): 
    439428        files = [] 
     
    444433                    files.append(fname) 
    445434        return files 
     435 
    446436    def fix_tzinfo(self, potfile): 
    447437        """