def get_context_data(self, **kwargs): context = super(TranslationFileListView, self).get_context_data(**kwargs) third_party_apps = self.po_filter in ('all', 'third-party') django_apps = self.po_filter in ('all', 'django') project_apps = self.po_filter in ('all', 'project') languages = [] has_pos = False for language in rosetta_settings.ROSETTA_LANGUAGES: if not can_translate_language(self.request.user, language[0]): continue po_paths = find_pos( language[0], project_apps=project_apps, django_apps=django_apps, third_party_apps=third_party_apps, ) po_files = [(get_app_name(l), os.path.realpath(l), pofile(l)) for l in po_paths] po_files.sort(key=lambda app: app[0]) languages.append((language[0], _(language[1]), po_files)) has_pos = has_pos or bool(po_paths) context['version'] = get_rosetta_version() context['languages1'] = languages context['has_pos'] = has_pos context['po_filter'] = self.po_filter return context
def get_context_data(self, **kwargs): context = super(TranslationFileListView, self).get_context_data(**kwargs) third_party_apps = self.po_filter in ('all', 'third-party') django_apps = self.po_filter in ('all', 'django') project_apps = self.po_filter in ('all', 'project') languages = [] has_pos = False for language in settings.LANGUAGES: if not can_translate_language(self.request.user, language[0]): continue po_paths = find_pos(language[0], project_apps=project_apps, django_apps=django_apps, third_party_apps=third_party_apps, ) po_files = [(get_app_name(l), os.path.realpath(l), pofile(l)) for l in po_paths] po_files.sort(key=lambda app: app[0]) languages.append((language[0], _(language[1]), po_files)) has_pos = has_pos or bool(po_paths) try: ADMIN_MEDIA_PREFIX = settings.ADMIN_MEDIA_PREFIX except AttributeError: ADMIN_MEDIA_PREFIX = settings.STATIC_URL + 'admin/' context['version'] = get_rosetta_version(True) context['ADMIN_MEDIA_PREFIX'] = ADMIN_MEDIA_PREFIX context['languages'] = languages context['has_pos'] = has_pos context['po_filter'] = self.po_filter return context
def get_context_data(self, **kwargs): context = super(TranslationFormView, self).get_context_data(**kwargs) entries = self.get_entries() paginator = Paginator(entries, rosetta_settings.MESSAGES_PER_PAGE) # Handle REF_LANG setting; mark up our entries with the reg lang's # corresponding translations LANGUAGES = list(rosetta_settings.ROSETTA_LANGUAGES) if rosetta_settings.ENABLE_REFLANG: if self.ref_lang_po_file: for o in paginator.object_list: ref_entry = self.ref_lang_po_file.find(o.msgid) if ref_entry and ref_entry.msgstr: o.ref_txt = ref_entry.msgstr else: o.ref_txt = o.msgid else: for o in paginator.object_list: o.ref_txt = o.msgid # XXX: having "MSGID" at the end of the dropdown is really odd, no? # Why not instead do this? # LANGUAGES = [('', '----')] + list(settings.LANGUAGES) LANGUAGES.append(('msgid', 'MSGID')) # Determine page number & how pagination links should be displayed try: page = int(self._request_request('page', 1)) except ValueError: page = 1 # fall back to page 1 else: if not (0 < page <= paginator.num_pages): page = 1 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) rosetta_messages = paginator.page(page).object_list # Handle MAIN_LANGUAGE setting, if applicable; mark up each entry # in the pagination window with the "main language"'s string. main_language_id = rosetta_settings.MAIN_LANGUAGE main_language = None if main_language_id and main_language_id != self.language_id: # Translate from id to language name for language in rosetta_settings.ROSETTA_LANGUAGES: if language[0] == main_language_id: main_language = _(language[1]) break if main_language: main_lang_po_path = self.po_file_path.replace( '/%s/' % self.language_id, '/%s/' % main_language_id) # XXX: brittle; what if this path doesn't exist? Isn't a .po file? main_lang_po = pofile(main_lang_po_path) for message in rosetta_messages: message.main_lang = main_lang_po.find(message.msgid).msgstr # Collect some constants for the template rosetta_i18n_lang_name = six.text_type( dict(rosetta_settings.ROSETTA_LANGUAGES).get(self.language_id)) # "bidi" as in "bi-directional" rosetta_i18n_lang_bidi = self.language_id.split( '-')[0] in settings.LANGUAGES_BIDI query_string_args = {} if self.msg_filter: query_string_args['msg_filter'] = self.msg_filter if self.query: query_string_args['query'] = self.query if self.ref_lang: query_string_args['ref_lang'] = self.ref_lang # Base for pagination links; the page num itself is added in template pagination_query_string_base = urlencode_safe(query_string_args) # Base for msg filter links; it doesn't make sense to persist page # numbers in these links. We just pass in ref_lang, if it's set. filter_query_string_base = urlencode_safe( {k: v for k, v in query_string_args.items() if k == 'ref_lang'}) context.update({ 'version': get_rosetta_version(), 'LANGUAGES': LANGUAGES, 'rosetta_settings': rosetta_settings, 'rosetta_i18n_lang_name': rosetta_i18n_lang_name, 'rosetta_i18n_lang_code': self.language_id, 'rosetta_i18n_lang_code_normalized': self.language_id.replace('_', '-'), 'rosetta_i18n_lang_bidi': rosetta_i18n_lang_bidi, 'rosetta_i18n_filter': self.msg_filter, 'rosetta_i18n_write': self.po_file_is_writable, 'rosetta_messages': rosetta_messages, 'page_range': needs_pagination and page_range, 'needs_pagination': needs_pagination, 'main_language': main_language, 'rosetta_i18n_app': get_app_name(self.po_file_path), 'page': page, 'query': self.query, 'pagination_query_string_base': pagination_query_string_base, 'filter_query_string_base': filter_query_string_base, 'paginator': paginator, 'rosetta_i18n_pofile': self.po_file, 'ref_lang': self.ref_lang, }) return context
def post(self, request, *args, **kwargs): """The only circumstances when we POST is to submit the main form, both updating translations (if any changed) and advancing to the next page of messages. There is no notion of validation of this content; as implemented, unknown fields are ignored and a generic failure message is shown. Submitted changes are saved out to the specified .po file on the filesystem if that file is writable, otherwise the cached version of the file is updated (so it can be downloaded). Then the user is redirected to the next page of messages (if there is one; otherwise they're redirected back to the current page). """ # The message text inputs are captured as hashes of their initial # contents, preceded by "m_". Messages with plurals end with their # variation number. single_text_input_regex = re.compile(r'^m_([0-9a-f]+)$') plural_text_input_regex = re.compile(r'^m_([0-9a-f]+)_([0-9]+)$') file_change = False for field_name, new_msgstr in request.POST.items(): md5hash = None if plural_text_input_regex.match(field_name): md5hash, plural_id = plural_text_input_regex.match( field_name).groups() md5hash = str(md5hash) # polib parses .po files into unicode strings, but # doesn't bother to convert plural indexes to int, # so we need unicode here. plural_id = six.text_type(plural_id) # Above no longer true as of Polib 1.0.4 if plural_id and plural_id.isdigit(): plural_id = int(plural_id) elif single_text_input_regex.match(field_name): md5hash = str( single_text_input_regex.match(field_name).groups()[0]) plural_id = None if md5hash is not None: # Empty string should be processed! entry = self.po_file.find(md5hash, 'md5hash') # If someone did a makemessage, some entries might # have been removed, so we need to check. if entry: old_msgstr = entry.msgstr if plural_id is not None: # 0 is ok! entry.msgstr_plural[plural_id] = self.fix_nls( entry.msgid_plural, new_msgstr) else: entry.msgstr = self.fix_nls(entry.msgid, new_msgstr) is_fuzzy = bool( self.request.POST.get('f_%s' % md5hash, False)) old_fuzzy = 'fuzzy' in entry.flags if old_fuzzy and not is_fuzzy: entry.flags.remove('fuzzy') elif not old_fuzzy and is_fuzzy: entry.flags.append('fuzzy') file_change = True if old_msgstr != new_msgstr or old_fuzzy != is_fuzzy: entry_changed.send( sender=entry, user=request.user, old_msgstr=old_msgstr, old_fuzzy=old_fuzzy, pofile=self.po_file_path, language_code=self.language_id, ) else: messages.error( self.request, _("Some items in your last translation block couldn't " "be saved: this usually happens when the catalog file " "changes on disk after you last loaded it."), ) if file_change and self.po_file_is_writable: try: self.po_file.metadata[ 'Last-Translator'] = unicodedata.normalize( 'NFKD', u"%s %s <%s>" % ( getattr(self.request.user, 'first_name', 'Anonymous'), getattr(self.request.user, 'last_name', 'User'), getattr(self.request.user, 'email', '*****@*****.**'), ), ).encode('ascii', 'ignore') self.po_file.metadata[ 'X-Translated-Using'] = u"django-rosetta %s" % ( get_rosetta_version()) self.po_file.metadata[ 'PO-Revision-Date'] = timestamp_with_timezone() except UnicodeDecodeError: pass try: self.po_file.save() po_filepath, ext = os.path.splitext(self.po_file_path) if rosetta_settings.AUTO_COMPILE: self.po_file.save_as_mofile(po_filepath + '.mo') post_save.send(sender=None, language_code=self.language_id, request=self.request) # Try auto-reloading via the WSGI daemon mode reload mechanism should_try_wsgi_reload = ( rosetta_settings.WSGI_AUTO_RELOAD and 'mod_wsgi.process_group' in self.request.environ and self.request.environ.get('mod_wsgi.process_group', None) and 'SCRIPT_FILENAME' in self.request.environ and int( self.request.environ.get('mod_wsgi.script_reloading', 0))) if should_try_wsgi_reload: try: os.utime(self.request.environ.get('SCRIPT_FILENAME'), None) except OSError: pass # Try auto-reloading via uwsgi daemon reload mechanism if rosetta_settings.UWSGI_AUTO_RELOAD: try: import uwsgi uwsgi.reload() # pretty easy right? except: pass # we may not be running under uwsgi :P # XXX: It would be nice to add a success message here! except Exception as e: messages.error(self.request, e) if file_change and not self.po_file_is_writable: storage = get_storage(self.request) storage.set(self.po_file_cache_key, self.po_file) # Reconstitute url to redirect to. Start with determining whether the # page number can be incremented. paginator = Paginator(self.get_entries(), rosetta_settings.MESSAGES_PER_PAGE) try: page = int(self._request_request('page', 1)) except ValueError: page = 1 # fall back to page 1 else: if not (0 < page <= paginator.num_pages): page = 1 if page < paginator.num_pages: page += 1 query_string_args = { 'msg_filter': self.msg_filter, 'query': self.query, 'ref_lang': self.ref_lang, 'page': page, } # Winnow down the query string args to non-blank ones query_string_args = {k: v for k, v in query_string_args.items() if v} return HttpResponseRedirect("{url}?{qs}".format( url=reverse('rosetta-form', kwargs=self.kwargs), qs=urlencode_safe(query_string_args), ))