def action_odf2tr(self, resource, context, form): odf_file_name, odf_file_mime_type, odf_file_data = form['odf_file'] srx_file = form['srx_file'] output_type = form['output_type'] # Not a too big file if check_size(odf_file_data, context): return # Get the good "get_units" odf_handler = get_handler_class_by_mimetype(odf_file_mime_type) try: get_units = odf_handler(string=odf_file_data).get_units except AttributeError: context.message = ERROR(u'malformed ODF file') return # a SRX file ? if srx_file is not None: srx_file_data = srx_file[2] try: srx_handler = SRXFile(string=srx_file_data) except XMLError: context.message = ERROR(u'unexpected error, please verify ' u'your input files') return else: srx_handler = None # The good handler for the output if output_type == 'PO': extension = 'po' out_handler = POFile() else: extension = 'xlf' out_handler = XLFFile() name = FileName.decode(odf_file_name)[0] out_filename = FileName.encode((name, extension, None)) # Make the output for source, source_context, line in get_units(srx_handler=srx_handler): out_handler.add_unit(odf_file_name, source, source_context, line) # Return the result context.set_content_type(out_handler.class_mimetypes[0]) context.set_content_disposition('inline', out_filename) return out_handler.to_str()
# Get the units write('.') handler = ro_database.get_handler(path) try: units = handler.get_units(srx_handler=srx_handler) units = list(units) except Exception: print print '*' print '* Error:', path print '*' raise relative_path = locale_folder_path.get_pathto(path) for source, context, line in units: po.add_unit(relative_path, source, context, line) print write('* Update PO template ') data = po.to_str() # Write the po into the locale.pot try: locale_pot = locale_folder.open('locale.pot', WRITE) except IOError: # The locale.pot file does not exist create and open locale_pot = locale_folder.make_file('locale.pot') else: with locale_pot: locale_pot.write(data)
def update_locale(srx_handler, exclude_folders, no_wrap=False): # Read configuration for languages config = get_config() src_language = config.get_value('source_language', default='en') # Get local folder package_root = config.get_value('package_root') if lfs.exists(package_root): locale_folder_path = Path('{0}/locale'.format(package_root)) else: locale_folder_path = Path('locale/') locale_folder = lfs.open(locale_folder_path) # Initialize message catalog po = POFile() lines = [] for line in open('MANIFEST').readlines(): line = line.strip() exclude_folder = False for x in exclude_folders: if line.startswith(x): exclude_folder = True break if exclude_folder is False: lines.append(line) # Process Python and HTML files write('* Extract text strings') extensions = [ '.py', '.js', '.xhtml.%s' % src_language, '.xml.%s' % src_language, '.html.%s' % src_language] for path in lines: # Filter files for extension in extensions: if path.endswith(extension): break else: continue # Get the units write('.') try: handler = ro_database.get_handler(path) except Exception: print print '*' print '* Error:', path print '*' raise try: units = handler.get_units(srx_handler=srx_handler) units = list(units) except Exception: print print '*' print '* Error:', path print '*' raise relative_path = locale_folder_path.get_pathto(path) for source, context, line in units: po.add_unit(relative_path, source, context, line) print write('* Update PO template ') data = po.to_str() # Write the po into the locale.pot try: locale_pot = locale_folder.open('locale.pot', WRITE) except IOError: # The locale.pot file does not exist create and open locale_pot = locale_folder.make_file('locale.pot') else: with locale_pot: locale_pot.write(data) # Update PO files filenames = set([ x for x in locale_folder.get_names() if x[-3:] == '.po' ]) filenames.add('%s.po' % src_language) for language in config.get_value('target_languages'): filenames.add('%s.po' % language) filenames = list(filenames) filenames.sort() print '* Update PO files:' locale_pot_path = locale_folder.get_absolute_path('locale.pot') for filename in filenames: if locale_folder.exists(filename): write(' %s ' % filename) file_path = locale_folder.get_absolute_path(filename) if no_wrap: call(['msgmerge', '--no-wrap', '-U', '-s', file_path, locale_pot_path]) else: call(['msgmerge', '-U', '-s', file_path, locale_pot_path]) else: print ' %s (new)' % filename file_path = locale_folder.get_absolute_path(filename) lfs.copy(locale_pot_path, file_path) print
def update_locale(srx_handler, exclude_folders, no_wrap=False): # Read configuration for languages config = get_config() src_language = config.get_value('source_language', default='en') # Get local folder package_root = config.get_value('package_root') if lfs.exists(package_root): locale_folder_path = Path('{0}/locale'.format(package_root)) else: locale_folder_path = Path('locale/') locale_folder = lfs.open(locale_folder_path) # Initialize message catalog po = POFile() lines = [] for line in open('MANIFEST').readlines(): line = line.strip() exclude_folder = False for x in exclude_folders: if line.startswith(x): exclude_folder = True break if exclude_folder is False: lines.append(line) # Process Python and HTML files write('* Extract text strings') extensions = [ '.py', '.js', '.xhtml.%s' % src_language, '.xml.%s' % src_language, '.html.%s' % src_language ] for path in lines: # Filter files for extension in extensions: if path.endswith(extension): break else: continue # Get the units write('.') try: handler = ro_database.get_handler(path) except Exception: print print '*' print '* Error:', path print '*' raise try: units = handler.get_units(srx_handler=srx_handler) units = list(units) except Exception: print print '*' print '* Error:', path print '*' raise relative_path = locale_folder_path.get_pathto(path) for source, context, line in units: po.add_unit(relative_path, source, context, line) print write('* Update PO template ') data = po.to_str() # Write the po into the locale.pot try: locale_pot = locale_folder.open('locale.pot', WRITE) except IOError: # The locale.pot file does not exist create and open locale_pot = locale_folder.make_file('locale.pot') else: with locale_pot: locale_pot.write(data) # Update PO files filenames = set([x for x in locale_folder.get_names() if x[-3:] == '.po']) filenames.add('%s.po' % src_language) for language in config.get_value('target_languages'): filenames.add('%s.po' % language) filenames = list(filenames) filenames.sort() print '* Update PO files:' locale_pot_path = locale_folder.get_absolute_path('locale.pot') for filename in filenames: if locale_folder.exists(filename): write(' %s ' % filename) file_path = locale_folder.get_absolute_path(filename) if no_wrap: call([ 'msgmerge', '--no-wrap', '-U', '-s', file_path, locale_pot_path ]) else: call(['msgmerge', '-U', '-s', file_path, locale_pot_path]) else: print ' %s (new)' % filename file_path = locale_folder.get_absolute_path(filename) lfs.copy(locale_pot_path, file_path) print
aux = [] for message in messages: if message not in aux: f.write('msgid "%s"\n' % message) f.write('msgstr ""\n') f.write('\n') aux.append(message) f.close() # Parse the rest of the files if filenames2: po = POFile() for filename in filenames2: handler = get_handler(filename) for source, context, line in handler.get_units(): po.add_unit(filename, source, context, line) filename = mktemp('.po') filenames.append(filename) open(filename, 'w').write(po.to_str()) # Create the POT file if filenames: filename = mktemp('.po') cmd = 'msgcat -s --output-file=%s %s' % (filename, ' '.join(filenames)) system(cmd) system('msgmerge -U locale/locale.pot %s' % filename) # Remove temporal files remove(filename) for filename in filenames: remove(filename)
# Get the units write('.') handler = ro_database.get_handler(path) try: units = handler.get_units(srx_handler=srx_handler) units = list(units) except Exception: print print '*' print '* Error:', path print '*' raise relative_path = Path('..').resolve2(path) for source, context, line in units: po.add_unit(relative_path, source, context, line) print # Update locale.pot if not lfs.exists('locale/locale.pot'): lfs.make_file('locale/locale.pot') write('* Update PO template ') data = po.to_str() file = lfs.open('locale/locale.pot', WRITE) try: file.write(data) finally: file.close() print