def list_languages(request): """ Lists the languages for the current project, the gettext catalog files that can be translated and their translation progress """ languages = [] do_django = 'django' in request.GET or get_setting('INCLUDE_DJANGOS') do_transhette = 'transhette' in request.GET or get_setting( 'INCLUDE_TRANSHETTE') has_pos = False for language in settings.LANGUAGES: pos = find_pos(language[0], include_djangos=do_django, include_transhette=do_transhette) has_pos = has_pos or len(pos) languages.append(( language[0], _(language[1]), [(os.path.realpath(l), pofile(l)) for l in pos], )) ADMIN_MEDIA_PREFIX = ADMIN_PREFIX version = transhette.get_version(True) return render_to_response('transhette/languages.html', locals(), context_instance=RequestContext(request))
def set_new_translation(request): """ Post to include a new translation for a msgid """ message = 'SOME ERRORS' if not request.POST: return None else: msgid = request.POST['msgid'] msgstr = request.POST['msgstr'] lang = get_language() pos = find_pos(lang, include_djangos=True, include_transhette=True) if pos: for file_po in pos: candidate = pofile(file_po) poentry = candidate.find(msgid) if poentry: selected_pofile = candidate poentry.msgstr = msgstr po_filename = file_po break format_errors = validate_format(selected_pofile) if not format_errors: try: selected_pofile.metadata[ 'Last-Translator'] = unicodedata.normalize( 'NFKD', u"%s %s <%s>" % (request.user.first_name, request.user.last_name, request.user.email)).encode('ascii', 'ignore') selected_pofile.metadata['X-Translated-Using'] = str( "django-transhette %s" % transhette.get_version(False)) selected_pofile.metadata[ 'PO-Revision-Date'] = datetime.datetime.now().strftime( '%Y-%m-%d %H:%M%z') except UnicodeDecodeError: pass selected_pofile.save() selected_pofile.save_as_mofile(po_filename.replace('.po', '.mo')) message = 'OK' return render_to_response('transhette/inline_demo_result.html', {'message': message}, context_instance=RequestContext(request))
def list_languages(request): """ Lists the languages for the current project, the gettext catalog files that can be translated and their translation progress """ languages = [] do_django = 'django' in request.GET or get_setting('INCLUDE_DJANGOS') do_transhette = 'transhette' in request.GET or get_setting('INCLUDE_TRANSHETTE') has_pos = False for language in settings.LANGUAGES: pos = find_pos(language[0], include_djangos=do_django, include_transhette=do_transhette) has_pos = has_pos or len(pos) languages.append( (language[0], _(language[1]), [(os.path.realpath(l), pofile(l)) for l in pos], ) ) ADMIN_MEDIA_PREFIX = ADMIN_PREFIX version = transhette.get_version(True) return render_to_response('transhette/languages.html', locals(), context_instance=RequestContext(request))
def ajax(request): def fix_nls(in_, out_): """Fixes submitted translations by filtering carriage returns and pairing newlines at the begging and end of the translated string with the original """ if 0 == len(in_) or 0 == len(out_): return out_ if "\r" in out_ and "\r" not in in_: out_=out_.replace("\r", '') if "\n" == in_[0] and "\n" != out_[0]: out_ = "\n" + out_ elif "\n" != in_[0] and "\n" == out_[0]: out_ = out_.lstrip() if "\n" == in_[-1] and "\n" != out_[-1]: out_ = out_ + "\n" elif "\n" != in_[-1] and "\n" == out_[-1]: out_ = out_.rstrip() return out_ catalog = request.GET.get('catalog', None) translation = request.GET.get('translation', None) if not translation: translation = {} for key, value in request.GET.items(): if key.startswith('translation_'): translation[key.replace('translation_', '')]=value msgid = request.GET.get('msgid', None) try: po_file = pofile(catalog) entry = po_file.find(msgid) except: po_file = None entry = None if not catalog or not translation or not msgid\ or not po_file or not entry: raise Http404 saved = False if isinstance(translation, dict): for key, item in translation.items(): entry.msgstr_plural[key] = fix_nls(entry.msgid_plural, item) else: entry.msgstr = fix_nls(entry.msgid, translation) if 'fuzzy' in entry.flags: entry.flags.remove('fuzzy') transhette_i18n_write = request.session.get('transhette_i18n_write', True) format_errors = validate_format(po_file) if transhette_i18n_write and not format_errors: try: po_file.metadata['Last-Translator'] = unicodedata.normalize('NFKD', u"%s %s <%s>" %(request.user.first_name, request.user.last_name, request.user.email)).encode('ascii', 'ignore') po_file.metadata['X-Translated-Using'] = str("django-transhette %s" % transhette.get_version(False)) po_file.metadata['PO-Revision-Date'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M%z') except UnicodeDecodeError: pass try: po_file.save() po_file.save_as_mofile(po_file.fpath.replace('.po', '.mo')) saved = True except: pass json_dict = simplejson.dumps({'saved': saved, 'translation': translation}) return HttpResponse(json_dict, mimetype='text/javascript')
def home(request): """ Displays a list of messages to be translated """ def fix_nls(in_, out_): """Fixes submitted translations by filtering carriage returns and pairing newlines at the begging and end of the translated string with the original """ if 0 == len(in_) or 0 == len(out_): return out_ if "\r" in out_ and "\r" not in in_: out_=out_.replace("\r", '') if "\n" == in_[0] and "\n" != out_[0]: out_ = "\n" + out_ elif "\n" != in_[0] and "\n" == out_[0]: out_ = out_.lstrip() if "\n" == in_[-1] and "\n" != out_[-1]: out_ = out_ + "\n" elif "\n" != in_[-1] and "\n" == out_[-1]: out_ = out_.rstrip() return out_ version = transhette.get_version(True) if 'transhette_i18n_fn' in request.session: # if another translator has updated catalog... we will reload this reload_if_catalog_updated(request) transhette_i18n_fn = request.session.get('transhette_i18n_fn') transhette_i18n_pofile = request.session.get('transhette_i18n_pofile') transhette_i18n_native_pofile = request.session.get('transhette_i18n_native_pofile') transhette_i18n_lang_code = request.session.get('transhette_i18n_lang_code') transhette_i18n_lang_bidi = (transhette_i18n_lang_code in settings.LANGUAGES_BIDI) transhette_i18n_write = request.session.get('transhette_i18n_write', True) languages = [] for language in settings.LANGUAGES: pos = find_pos(language[0]) position = None lang_frag = '/%s/' for i in xrange(len(pos)): if transhette_i18n_pofile.fpath.replace( lang_frag % transhette_i18n_lang_code, lang_frag % language[0]) == pofile(pos[i]).fpath: position = i if position is not None: languages.append((language[0], _(language[1]), position)) # Retain query arguments query_arg = '' if 'query' in request.REQUEST: query_arg = '?query=%s' % request.REQUEST.get('query') if 'page' in request.GET: if query_arg: query_arg = query_arg + '&' else: query_arg = '?' query_arg = query_arg + 'page=%d' % int(request.GET.get('page')) if 'filter' in request.GET: if request.GET.get('filter') in ['untranslated', 'translated', 'both', 'fuzzy']: filter_ = request.GET.get('filter') request.session['transhette_i18n_filter'] = filter_ return HttpResponseRedirect(reverse('transhette-home')) elif 'transhette_i18n_filter' in request.session: transhette_i18n_filter = request.session.get('transhette_i18n_filter') else: transhette_i18n_filter = 'both' if '_next' in request.POST: rx=re.compile(r'^m_([0-9]+)') rx_plural=re.compile(r'^m_([0-9]+)_([0-9]+)') file_change = False for k in request.POST.keys(): if rx_plural.match(k): id=int(rx_plural.match(k).groups()[0]) idx=int(rx_plural.match(k).groups()[1]) transhette_i18n_pofile[id].msgstr_plural[str(idx)] = fix_nls(transhette_i18n_pofile[id].msgid_plural[idx], request.POST.get(k)) file_change = True elif rx.match(k): id=int(rx.match(k).groups()[0]) transhette_i18n_pofile[id].msgstr = fix_nls(transhette_i18n_pofile[id].msgid, request.POST.get(k)) file_change = True if file_change and 'fuzzy' in transhette_i18n_pofile[id].flags: transhette_i18n_pofile[id].flags.remove('fuzzy') format_errors = validate_format(transhette_i18n_pofile) if file_change and transhette_i18n_write and not format_errors: reload_if_catalog_updated(request, polling=True) try: transhette_i18n_pofile.metadata['Last-Translator'] = unicodedata.normalize('NFKD', u"%s %s <%s>" %(request.user.first_name, request.user.last_name, request.user.email)).encode('ascii', 'ignore') transhette_i18n_pofile.metadata['X-Translated-Using'] = str("django-transhette %s" % transhette.get_version(False)) transhette_i18n_pofile.metadata['PO-Revision-Date'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M%z') except UnicodeDecodeError: pass try: mo_path = transhette_i18n_fn.replace('.po', '.mo') transhette_i18n_pofile.save() transhette_i18n_pofile.save_as_mofile(mo_path) # Try auto-reloading via the WSGI daemon mode reload mechanism if get_setting('WSGI_AUTO_RELOAD') and\ 'mod_wsgi.process_group' in request.environ and \ request.environ.get('mod_wsgi.process_group', None) and \ 'SCRIPT_FILENAME' in request.environ and \ int(request.environ.get('mod_wsgi.script_reloading', '0')): try: os.utime(request.environ.get('SCRIPT_FILENAME'), None) except OSError: pass except: request.session['transhette_i18n_write'] = False request.session['transhette_i18n_pofile'] = transhette_i18n_pofile request.session['transhette_i18n_native_pofile'] = transhette_i18n_native_pofile return HttpResponseRedirect(reverse('transhette-home') + query_arg) transhette_i18n_lang_name = _(request.session.get('transhette_i18n_lang_name')) transhette_i18n_lang_code = request.session.get('transhette_i18n_lang_code') transhette_i18n_native_lang_name = request.session.get('transhette_i18n_native_lang_name', '') transhette_i18n_native_lang_code = request.session.get('transhette_i18n_native_lang_code', '') if 'query' in request.REQUEST and request.REQUEST.get('query', '').strip(): query = request.REQUEST.get('query').strip() try: rx = re.compile(re.escape(query), re.IGNORECASE) except re.error: rx = None matched_entries = [] if rx: for e in transhette_i18n_pofile: entry_text = smart_unicode(e.msgstr) + smart_unicode(e.msgid) if get_setting('SEARCH_INTO_OCCURRENCES'): entry_text += u''.join([o[0] for o in e.occurrences]) if rx.search(entry_text): matched_entries.append(e) if transhette_i18n_native_pofile: for e in transhette_i18n_native_pofile: entry_text = smart_unicode(e.msgstr) + smart_unicode(e.msgid) if get_setting('SEARCH_INTO_OCCURRENCES'): entry_text += u''.join([o[0] for o in e.occurrences]) if rx.search(entry_text): lang_entry = transhette_i18n_pofile.find(e.msgid) if lang_entry and not lang_entry in matched_entries: matched_entries.append(lang_entry) pofile_to_paginate = matched_entries else: if transhette_i18n_filter == 'both': pofile_to_paginate = transhette_i18n_pofile elif transhette_i18n_filter == 'untranslated': pofile_to_paginate = transhette_i18n_pofile.untranslated_entries() elif transhette_i18n_filter == 'translated': pofile_to_paginate = transhette_i18n_pofile.translated_entries() elif transhette_i18n_filter == 'fuzzy': pofile_to_paginate = transhette_i18n_pofile.fuzzy_entries() if get_setting('SHOW_NATIVE_LANGUAGE') and transhette_i18n_native_pofile: to_paginate = [dict(message=message, native_message=transhette_i18n_native_pofile.find(message.msgid)) \ for message in pofile_to_paginate] else: to_paginate = [dict(message=message) for message in pofile_to_paginate] paginator = Paginator(to_paginate, get_setting('MESSAGES_PER_PAGE')) if 'page' in request.GET and int(request.GET.get('page')) <= paginator.num_pages and int(request.GET.get('page')) > 0: page = int(request.GET.get('page')) else: page = 1 if get_setting('SHOW_NATIVE_LANGUAGE') and transhette_i18n_lang_code != transhette_i18n_native_lang_code: default_column_name = True else: default_column_name = False message_list = paginator.page(page).object_list message_list = search_msg_id_in_other_pos(message_list, transhette_i18n_lang_code, transhette_i18n_pofile) needs_pagination = paginator.num_pages > 1 if needs_pagination: if paginator.num_pages >= 10: page_range = pagination_range(1, paginator.num_pages, page) else: page_range = range(1, 1 + paginator.num_pages) ADMIN_MEDIA_PREFIX = ADMIN_PREFIX ENABLE_TRANSLATION_SUGGESTIONS = get_setting('ENABLE_TRANSLATION_SUGGESTIONS') return render_to_response('transhette/pofile.html', locals(), context_instance=RequestContext(request)) else: return list_languages(request)
def set_new_translation(request): """ Post to include a new translation for a msgid """ message='SOME ERRORS' if not request.POST: return None else: msgid = request.POST['msgid'] msgstr = request.POST['msgstr'] lang = get_language() pos = find_pos(lang, include_djangos=True, include_transhette=True) if pos: for file_po in pos: candidate = pofile(file_po) poentry = candidate.find(msgid) if poentry: selected_pofile = candidate poentry.msgstr = msgstr po_filename = file_po break format_errors = validate_format(selected_pofile) if not format_errors: try: selected_pofile.metadata['Last-Translator'] = unicodedata.normalize('NFKD', u"%s %s <%s>" %(request.user.first_name, request.user.last_name, request.user.email)).encode('ascii', 'ignore') selected_pofile.metadata['X-Translated-Using'] = str("django-transhette %s" % transhette.get_version(False)) selected_pofile.metadata['PO-Revision-Date'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M%z') except UnicodeDecodeError: pass selected_pofile.save() selected_pofile.save_as_mofile(po_filename.replace('.po', '.mo')) message='OK' return render_to_response('transhette/inline_demo_result.html', {'message': message}, context_instance=RequestContext(request))
def ajax(request): def fix_nls(in_, out_): """Fixes submitted translations by filtering carriage returns and pairing newlines at the begging and end of the translated string with the original """ if 0 == len(in_) or 0 == len(out_): return out_ if "\r" in out_ and "\r" not in in_: out_ = out_.replace("\r", '') if "\n" == in_[0] and "\n" != out_[0]: out_ = "\n" + out_ elif "\n" != in_[0] and "\n" == out_[0]: out_ = out_.lstrip() if "\n" == in_[-1] and "\n" != out_[-1]: out_ = out_ + "\n" elif "\n" != in_[-1] and "\n" == out_[-1]: out_ = out_.rstrip() return out_ catalog = request.GET.get('catalog', None) translation = request.GET.get('translation', None) if not translation: translation = {} for key, value in request.GET.items(): if key.startswith('translation_'): translation[key.replace('translation_', '')] = value msgid = request.GET.get('msgid', None) try: po_file = pofile(catalog) entry = po_file.find(msgid) except: po_file = None entry = None if not catalog or not translation or not msgid\ or not po_file or not entry: raise Http404 saved = False if isinstance(translation, dict): for key, item in translation.items(): entry.msgstr_plural[key] = fix_nls(entry.msgid_plural, item) else: entry.msgstr = fix_nls(entry.msgid, translation) if 'fuzzy' in entry.flags: entry.flags.remove('fuzzy') transhette_i18n_write = request.session.get('transhette_i18n_write', True) format_errors = validate_format(po_file) if transhette_i18n_write and not format_errors: try: po_file.metadata['Last-Translator'] = unicodedata.normalize( 'NFKD', u"%s %s <%s>" % (request.user.first_name, request.user.last_name, request.user.email)).encode('ascii', 'ignore') po_file.metadata['X-Translated-Using'] = str( "django-transhette %s" % transhette.get_version(False)) po_file.metadata['PO-Revision-Date'] = datetime.datetime.now( ).strftime('%Y-%m-%d %H:%M%z') except UnicodeDecodeError: pass try: po_file.save() po_file.save_as_mofile(po_file.fpath.replace('.po', '.mo')) saved = True except: pass json_dict = simplejson.dumps({'saved': saved, 'translation': translation}) return HttpResponse(json_dict, mimetype='text/javascript')
def home(request): """ Displays a list of messages to be translated """ def fix_nls(in_, out_): """Fixes submitted translations by filtering carriage returns and pairing newlines at the begging and end of the translated string with the original """ if 0 == len(in_) or 0 == len(out_): return out_ if "\r" in out_ and "\r" not in in_: out_ = out_.replace("\r", '') if "\n" == in_[0] and "\n" != out_[0]: out_ = "\n" + out_ elif "\n" != in_[0] and "\n" == out_[0]: out_ = out_.lstrip() if "\n" == in_[-1] and "\n" != out_[-1]: out_ = out_ + "\n" elif "\n" != in_[-1] and "\n" == out_[-1]: out_ = out_.rstrip() return out_ version = transhette.get_version(True) if 'transhette_i18n_fn' in request.session: # if another translator has updated catalog... we will reload this reload_if_catalog_updated(request) transhette_i18n_fn = request.session.get('transhette_i18n_fn') transhette_i18n_pofile = request.session.get('transhette_i18n_pofile') transhette_i18n_native_pofile = request.session.get( 'transhette_i18n_native_pofile') transhette_i18n_lang_code = request.session.get( 'transhette_i18n_lang_code') transhette_i18n_lang_bidi = (transhette_i18n_lang_code in settings.LANGUAGES_BIDI) transhette_i18n_write = request.session.get('transhette_i18n_write', True) languages = [] for language in settings.LANGUAGES: pos = find_pos(language[0]) position = None lang_frag = '/%s/' for i in xrange(len(pos)): if transhette_i18n_pofile.fpath.replace( lang_frag % transhette_i18n_lang_code, lang_frag % language[0]) == pofile(pos[i]).fpath: position = i if position is not None: languages.append((language[0], _(language[1]), position)) # Retain query arguments query_arg = '' if 'query' in request.REQUEST: query_arg = '?query=%s' % request.REQUEST.get('query') if 'page' in request.GET: if query_arg: query_arg = query_arg + '&' else: query_arg = '?' query_arg = query_arg + 'page=%d' % int(request.GET.get('page')) if 'filter' in request.GET: if request.GET.get('filter') in [ 'untranslated', 'translated', 'both', 'fuzzy' ]: filter_ = request.GET.get('filter') request.session['transhette_i18n_filter'] = filter_ return HttpResponseRedirect(reverse('transhette-home')) elif 'transhette_i18n_filter' in request.session: transhette_i18n_filter = request.session.get( 'transhette_i18n_filter') else: transhette_i18n_filter = 'both' if '_next' in request.POST: rx = re.compile(r'^m_([0-9]+)') rx_plural = re.compile(r'^m_([0-9]+)_([0-9]+)') file_change = False for k in request.POST.keys(): if rx_plural.match(k): id = int(rx_plural.match(k).groups()[0]) idx = int(rx_plural.match(k).groups()[1]) transhette_i18n_pofile[id].msgstr_plural[str( idx)] = fix_nls( transhette_i18n_pofile[id].msgid_plural[idx], request.POST.get(k)) file_change = True elif rx.match(k): id = int(rx.match(k).groups()[0]) transhette_i18n_pofile[id].msgstr = fix_nls( transhette_i18n_pofile[id].msgid, request.POST.get(k)) file_change = True if file_change and 'fuzzy' in transhette_i18n_pofile[id].flags: transhette_i18n_pofile[id].flags.remove('fuzzy') format_errors = validate_format(transhette_i18n_pofile) if file_change and transhette_i18n_write and not format_errors: reload_if_catalog_updated(request, polling=True) try: transhette_i18n_pofile.metadata[ 'Last-Translator'] = unicodedata.normalize( 'NFKD', u"%s %s <%s>" % (request.user.first_name, request.user.last_name, request.user.email)).encode('ascii', 'ignore') transhette_i18n_pofile.metadata[ 'X-Translated-Using'] = str( "django-transhette %s" % transhette.get_version(False)) transhette_i18n_pofile.metadata[ 'PO-Revision-Date'] = datetime.datetime.now().strftime( '%Y-%m-%d %H:%M%z') except UnicodeDecodeError: pass try: mo_path = transhette_i18n_fn.replace('.po', '.mo') transhette_i18n_pofile.save() transhette_i18n_pofile.save_as_mofile(mo_path) # Try auto-reloading via the WSGI daemon mode reload mechanism if get_setting('WSGI_AUTO_RELOAD') and\ 'mod_wsgi.process_group' in request.environ and \ request.environ.get('mod_wsgi.process_group', None) and \ 'SCRIPT_FILENAME' in request.environ and \ int(request.environ.get('mod_wsgi.script_reloading', '0')): try: os.utime(request.environ.get('SCRIPT_FILENAME'), None) except OSError: pass except: request.session['transhette_i18n_write'] = False request.session[ 'transhette_i18n_pofile'] = transhette_i18n_pofile request.session[ 'transhette_i18n_native_pofile'] = transhette_i18n_native_pofile return HttpResponseRedirect( reverse('transhette-home') + query_arg) transhette_i18n_lang_name = _( request.session.get('transhette_i18n_lang_name')) transhette_i18n_lang_code = request.session.get( 'transhette_i18n_lang_code') transhette_i18n_native_lang_name = request.session.get( 'transhette_i18n_native_lang_name', '') transhette_i18n_native_lang_code = request.session.get( 'transhette_i18n_native_lang_code', '') if 'query' in request.REQUEST and request.REQUEST.get('query', '').strip(): query = request.REQUEST.get('query').strip() try: rx = re.compile(re.escape(query), re.IGNORECASE) except re.error: rx = None matched_entries = [] if rx: for e in transhette_i18n_pofile: entry_text = smart_unicode(e.msgstr) + smart_unicode( e.msgid) if get_setting('SEARCH_INTO_OCCURRENCES'): entry_text += u''.join([o[0] for o in e.occurrences]) if rx.search(entry_text): matched_entries.append(e) if transhette_i18n_native_pofile: for e in transhette_i18n_native_pofile: entry_text = smart_unicode(e.msgstr) + smart_unicode( e.msgid) if get_setting('SEARCH_INTO_OCCURRENCES'): entry_text += u''.join( [o[0] for o in e.occurrences]) if rx.search(entry_text): lang_entry = transhette_i18n_pofile.find(e.msgid) if lang_entry and not lang_entry in matched_entries: matched_entries.append(lang_entry) pofile_to_paginate = matched_entries else: if transhette_i18n_filter == 'both': pofile_to_paginate = transhette_i18n_pofile elif transhette_i18n_filter == 'untranslated': pofile_to_paginate = transhette_i18n_pofile.untranslated_entries( ) elif transhette_i18n_filter == 'translated': pofile_to_paginate = transhette_i18n_pofile.translated_entries( ) elif transhette_i18n_filter == 'fuzzy': pofile_to_paginate = transhette_i18n_pofile.fuzzy_entries() if get_setting( 'SHOW_NATIVE_LANGUAGE') and transhette_i18n_native_pofile: to_paginate = [dict(message=message, native_message=transhette_i18n_native_pofile.find(message.msgid)) \ for message in pofile_to_paginate] else: to_paginate = [ dict(message=message) for message in pofile_to_paginate ] paginator = Paginator(to_paginate, get_setting('MESSAGES_PER_PAGE')) if 'page' in request.GET and int( request.GET.get('page')) <= paginator.num_pages and int( request.GET.get('page')) > 0: page = int(request.GET.get('page')) else: page = 1 if get_setting( 'SHOW_NATIVE_LANGUAGE' ) and transhette_i18n_lang_code != transhette_i18n_native_lang_code: default_column_name = True else: default_column_name = False message_list = paginator.page(page).object_list message_list = search_msg_id_in_other_pos(message_list, transhette_i18n_lang_code, transhette_i18n_pofile) needs_pagination = paginator.num_pages > 1 if needs_pagination: if paginator.num_pages >= 10: page_range = pagination_range(1, paginator.num_pages, page) else: page_range = range(1, 1 + paginator.num_pages) ADMIN_MEDIA_PREFIX = ADMIN_PREFIX ENABLE_TRANSLATION_SUGGESTIONS = get_setting( 'ENABLE_TRANSLATION_SUGGESTIONS') return render_to_response('transhette/pofile.html', locals(), context_instance=RequestContext(request)) else: return list_languages(request)