def main(): updFiles = glob.glob(r"WikidPad*.po") updParams = [] # for uf in updFiles: # updParams += [ur"-u", uf] pySrcParams = [ r"Consts.py", r"ExceptionLogger.py", r"WikidPadStarter.py", r"extensions", r"lib\pwiki", r"lib\pwiki\rtlibRepl", r"lib\pwiki\timeView", r"lib\pwiki\wikidata", r"lib\pwiki\wikidata\compact_sqlite", r"lib\pwiki\wikidata\original_gadfly", r"lib\pwiki\wikidata\original_sqlite" ] params = [r"-o", r"WikidPad.pot", r"--xrc=WikidPad.xrc"] + updParams + \ pySrcParams pygettext.main(params)
def createTemplate(self, pyfile, potfile): "Create a POT File from a Python Script with gettext Strings" #TODO Exception Handling for Syntax Problems cmd = 'pygettext.exe --no-location -d {} "{}"'.format( potfile.replace(".pot", ""), pyfile) #os.system(cmd) try: pygettext.main("--no-location", "-d", potfile.replace(".pot", ""), pyfile) except Exception as err: showerror(title=_("Failed to Create Template"), message="{}".format(err))
def collect_string_for_files(self,files): if not files:return params =['','-v'] for file in files: params.append(file) pygettext.sys.argv = params pygettext.main() pot = os.path.join(self.currentProject,'messages.pot') if not os.path.exists(pot):return locales = self.locales_directory() filename = os.path.join(locales,'messages.pot') try: os.rename(pot,filename) except OSError, e: print e
def main(): updFiles = glob.glob(ur"WikidPad*.po") updParams = [] # for uf in updFiles: # updParams += [ur"-u", uf] pySrcParams = [ ur"Consts.py", ur"ExceptionLogger.py", ur"WikidPadStarter.py", ur"extensions", ur"lib\pwiki", ur"lib\pwiki\rtlibRepl", ur"lib\pwiki\timeView", ur"lib\pwiki\wikidata", ur"lib\pwiki\wikidata\compact_sqlite", ur"lib\pwiki\wikidata\original_gadfly", ur"lib\pwiki\wikidata\original_sqlite" ] params = [ur"-o", ur"WikidPad.pot", ur"--xrc=WikidPad.xrc"] + updParams + \ pySrcParams pygettext.main(params)
def collect_string_for_files(self, files): if not files: return params = ['', '-v'] for file in files: params.append(file) pygettext.sys.argv = params pygettext.main() pot = os.path.join(self.currentProject, 'messages.pot') if not os.path.exists(pot): return locales = self.locales_directory() filename = os.path.join(locales, 'messages.pot') try: os.rename(pot, filename) except OSError, e: print e
def Dump(language,outPath,*files): """Dumps translatable string from *files to a new txt file in outPath, named based on language. If an already existing translation file exists for that language, the new one will be updated with any matching strings from the previous one.""" #--Determine files to dump if not files: # No files specified. Assume this file is located in root/src/bolt # and we'll dump every .py file in root recursively files = [] mopy = GPath(__file__).realpath.head.head.head files = [root.join(file).s for root,dirs,fnames in mopy.walk() for file in fnames if file.cext == '.py'] print('files:') for file in files: print(file) #--Output files outTxt = '%sNEW.txt' % language fullTxt = outPath.join(outTxt) tmpTxt = outPath.join('%sNEW.tmp' % language) oldTxt = outPath.join('%s.txt' % language) #--First dump a fresh translation file args = ['p','-a','-o',fullTxt.s] args.extend(files) if hasattr(sys,'frozen'): # Frozen app, the tool scripts aren't accessible # Instead, they're included in the package as an # importable module import pygettext old_argv = sys.argv[:] sys.argv = args pygettext.main() sys.argv = old_argv else: p = GPath(sys.prefix).join('Tools','i18n','pygettext.py') args[0] = p.s subprocess.call(args,shell=True) return outTxt
def dump_translator(out_path, lang): """Dumps all translatable strings in python source files to a new text file. As this requires the source files, it will not work in standalone mode, unless the source files are also installed. :param out_path: The directory containing localization files - typically bass.dirs[u'l10n']. :param lang: The language to dump a text file for. :return: The path to the file that the dump was written to.""" new_txt = os.path.join(out_path, u'%sNEW.txt' % lang) tmp_txt = os.path.join(out_path, u'%sNEW.tmp' % lang) old_txt = os.path.join(out_path, u'%s.txt' % lang) gt_args = [u'p', u'-a', u'-o', new_txt] gt_args.extend(_find_all_bash_modules()) # Need to do this differently on standalone if bass.is_standalone: # Delayed import, since it's only present on standalone import pygettext old_argv = sys.argv[:] sys.argv = gt_args pygettext.main() sys.argv = old_argv else: # pygettext is only in Tools, so call it explicitly gt_args[0] = sys.executable gt_args.insert( 1, os.path.join(sys.prefix, u'Tools', u'i18n', u'pygettext.py')) subprocess.call(gt_args, shell=True) # Fill in any already translated stuff...? try: re_msg_ids_start = re.compile(u'#:') re_encoding = re.compile( u'' r'"Content-Type:\s*text/plain;\s*charset=(.*?)\\n"$', re.I) re_non_escaped_quote = re.compile(u'' r'([^\\])"') def sub_quote(regex_match): return regex_match.group(1) + r'\"' target_enc = None # Do all this in binary mode and carefully handle encodings with open(tmp_txt, u'wb') as out: # Copy old translation file header, and get encoding for strings with open(old_txt, u'rb') as ins: for old_line in ins: if not target_enc: # Chop off the terminating newline encoding_match = re_encoding.match( old_line.rstrip(b'\r\n')) if encoding_match: # Encoding names are all ASCII, so this is safe target_enc = unicode(encoding_match.group(1), u'ascii') if re_msg_ids_start.match(old_line): break # Break once we hit the first translatable string out.write(old_line) # Read through the new translation file, fill in any already # translated strings with open(new_txt, u'rb') as ins: skipped_header = False for new_line in ins: # First, skip the header - we already copied it above if not skipped_header: msg_ids_match = re_msg_ids_start.match(new_line) if msg_ids_match: skipped_header = True out.write(new_line) continue elif new_line.startswith(b'msgid "'): # Decode the line and retrieve only the msgid contents stripped_line = unicode(new_line, target_enc) stripped_line = stripped_line.strip(u'\r\n')[7:-1] # Replace escape sequences - Quote, Tab, Backslash stripped_line = stripped_line.replace(u'\\"', u'"') stripped_line = stripped_line.replace(u'\\t', u'\t') stripped_line = stripped_line.replace(u'\\\\', u'\\') # Try translating, check if that changes the string ##: This is a neat, pragmatic implementation - but of # course limits us to only ever dumping translations # for the current language translated_line = _(stripped_line) # We're going to need the msgid either way out.write(new_line) if translated_line != stripped_line: # This has a translation, so write that one out out.write(b'msgstr "') # Escape any characters used in escape sequences # and encode the resulting 'final' translation final_ln = translated_line.replace(u'\\', u'\\\\') final_ln = final_ln.replace(u'\t', u'\\t') final_ln = re_non_escaped_quote.sub( sub_quote, final_ln) final_ln = final_ln.encode(target_enc) out.write(final_ln) out.write(b'"\n') else: # Not translated, write out an empty msgstr out.write(b'msgstr ""\n') elif new_line.startswith(b'msgstr "'): # Skip all msgstr lines from new_txt (handled above) continue else: out.write(new_line) except (IOError, UnicodeError, OSError): bolt.deprint(u'Error while dumping translation file:', traceback=True) try: os.remove(tmp_txt) except OSError: pass else: # Replace the empty translation file generated by pygettext with the # temp one we just created try: os.remove(new_txt) os.rename(tmp_txt, new_txt) except OSError: if os.path.exists(new_txt): try: os.remove(tmp_txt) except OSError: pass return new_txt