Exemplo n.º 1
0
def get_locale_catalog(locale):
    """
    Fetch the complete message catalog for a certain locale.  Messages or spread across different po files.
    To check if a message is translated or not you'll need this catalog, iterating over the po files is way to slow.
    This method consolidates all the messages a places them in a dictionary.

    @param locale: locale of the translation, e.g.: nl_NL, nl_BE, fr_BE,
    @type locale: str
    @return: POFile with an additional dictionary to quickly lookup a message
    """
    if not locale:
        raise ValueError('Invalid locale: %s' % locale)

    request = getattr(THREAD_LOCAL_STORAGE, REQUEST, None)

    if not request:
        raise SystemError(
            'Could not fetch the request object from THREAD_LOCALE_STORAGE')

    cache_key_locale = get_cache_key(locale)
    if hasattr(request, cache_key_locale):
        return getattr(request, cache_key_locale)

    storage = get_storage(request)

    if storage.has(cache_key_locale):
        catalog = storage.get(cache_key_locale)
        setattr(request, cache_key_locale, catalog)
        return catalog

    files = find_pos(locale, third_party_apps=THIRD_PARTY_APPS)
    if len(files) == 0:
        raise ValueError('Could not find any po files for locale: %s' % locale)

    # This catalog is needed to check whether a message is translated or not, we don't wont the interfere
    # with rosetta's logic ...
    #catalog = copy.deepcopy(pofile(files[0]))
    logger.info('Creating cached catalog for: %s' % locale)
    catalog = pofile(files[0])

    # Join the other po files to the original
    for i in range(1, len(files)):
        #deep = copy.deepcopy(pofile(files[i]))
        deep = pofile(files[i])
        for entry in deep:
            entry.pfile = files[i]
            catalog.append(entry)

    catalog.dict = dict((e.msgid, e) for e in catalog)

    # Store the catalog on the request
    setattr(request, cache_key_locale, catalog)
    # Store the catalog in the cache
    storage.set(cache_key_locale, catalog)

    #get_catalogs()[locale] = catalog
    #print "Catalog: ", repr(catalog)
    #print "Dict: ", repr(catalog.dict)
    return catalog
Exemplo n.º 2
0
def get_locale_catalog(locale):
    """
    Fetch the complete message catalog for a certain locale.  Messages or spread across different po files.
    To check if a message is translated or not you'll need this catalog, iterating over the po files is way to slow.
    This method consolidates all the messages a places them in a dictionary.

    @param locale: locale of the translation, e.g.: nl_NL, nl_BE, fr_BE,
    @type locale: str
    @return: POFile with an additional dictionary to quickly lookup a message
    """
    if not locale:
        raise ValueError('Invalid locale: %s' % locale)

    request = getattr(THREAD_LOCAL_STORAGE, REQUEST, None)

    if not request:
        raise SystemError('Could not fetch the request object from THREAD_LOCALE_STORAGE')

    cache_key_locale = get_cache_key(locale)
    if hasattr(request, cache_key_locale):
        return getattr(request, cache_key_locale)

    storage = get_storage(request)

    if storage.has(cache_key_locale):
        catalog = storage.get(cache_key_locale)
        setattr(request, cache_key_locale, catalog)
        return catalog

    files = find_pos(locale, third_party_apps=THIRD_PARTY_APPS)
    if len(files) == 0:
        raise ValueError('Could not find any po files for locale: %s' % locale)

    # This catalog is needed to check whether a message is translated or not, we don't wont the interfere
    # with rosetta's logic ...
    #catalog = copy.deepcopy(pofile(files[0]))
    logger.info('Creating cached catalog for: %s' % locale)
    catalog = pofile(files[0])

    # Join the other po files to the original
    for i in range(1, len(files)):
        #deep = copy.deepcopy(pofile(files[i]))
        deep = pofile(files[i])
        for entry in deep:
            entry.pfile = files[i]
            catalog.append(entry)

    catalog.dict = dict((e.msgid, e) for e in catalog)

    # Store the catalog on the request
    setattr(request, cache_key_locale, catalog)
    # Store the catalog in the cache
    storage.set(cache_key_locale, catalog)

    #get_catalogs()[locale] = catalog
    #print "Catalog: ", repr(catalog)
    #print "Dict: ", repr(catalog.dict)
    return catalog
Exemplo n.º 3
0
def lang_sel(request,langid,idx):
    """
    Selects a file to be translated
    """
    if langid not in [l[0] for l in settings.LANGUAGES]:
        raise Http404
    else:
        
        do_django = 'django' in request.GET
        do_rosetta = 'rosetta' in request.GET
        
        file_ = find_pos(langid,include_djangos=do_django,include_rosetta=do_rosetta)[int(idx)]
        
        request.session['rosetta_i18n_lang_code'] = langid
        request.session['rosetta_i18n_lang_name'] = unicode([l[1] for l in settings.LANGUAGES if l[0] == langid][0])
        request.session['rosetta_i18n_fn'] = file_
        po = pofile(file_)
        for i in range(len(po)):
            po[i].id = i
            
        request.session['rosetta_i18n_pofile'] = po
        try:
            os.utime(file_,None)
            request.session['rosetta_i18n_write'] = True
        except OSError:
            request.session['rosetta_i18n_write'] = False
            
        return HttpResponseRedirect(reverse('rosetta-home'))
Exemplo n.º 4
0
def lang_sel(request, langid, idx):
    """
    Selects a file to be translated
    """
    if langid not in [l[0] for l in settings.LANGUAGES]:
        raise Http404
    else:

        rosetta_i18n_catalog_filter = request.session.get('rosetta_i18n_catalog_filter', 'project')

        third_party_apps = rosetta_i18n_catalog_filter in ('all', 'third-party')
        django_apps = rosetta_i18n_catalog_filter in ('all', 'django')
        project_apps = rosetta_i18n_catalog_filter in ('all', 'project')
        file_ = find_pos(langid, project_apps=project_apps, django_apps=django_apps, third_party_apps=third_party_apps)[int(idx)]

        request.session['rosetta_i18n_lang_code'] = langid
        request.session['rosetta_i18n_lang_name'] = unicode([l[1] for l in settings.LANGUAGES if l[0] == langid][0])
        request.session['rosetta_i18n_fn'] = file_
        po = pofile(file_)
        for entry in po:
            entry.md5hash = hashlib.md5(entry.msgid.encode("utf8") + entry.msgstr.encode("utf8")).hexdigest()

        request.session['rosetta_i18n_pofile'] = po
        try:
            os.utime(file_, None)
            request.session['rosetta_i18n_write'] = True
        except OSError:
            request.session['rosetta_i18n_write'] = False

        return HttpResponseRedirect(reverse('rosetta-home'))
Exemplo n.º 5
0
def list_languages(request):
    """
    Lists the languages for the current project, the gettext catalog files
    that can be translated and their translation progress
    """
    languages = []

    if 'filter' in request.GET:
        if request.GET.get('filter') in ('project', 'third-party', 'django', 'all'):
            filter_ = request.GET.get('filter')
            request.session['rosetta_i18n_catalog_filter'] = filter_
            return HttpResponseRedirect(reverse('rosetta-pick-file'))

    rosetta_i18n_catalog_filter = request.session.get('rosetta_i18n_catalog_filter', 'project')

    third_party_apps = rosetta_i18n_catalog_filter in ('all', 'third-party')
    django_apps = rosetta_i18n_catalog_filter in ('all', 'django')
    project_apps = rosetta_i18n_catalog_filter in ('all', 'project')

    has_pos = False
    for language in settings.LANGUAGES:
        pos = find_pos(language[0], project_apps=project_apps, django_apps=django_apps, third_party_apps=third_party_apps)
        has_pos = has_pos or len(pos)
        languages.append(
            (language[0],
            _(language[1]),
            [(get_app_name(l), os.path.realpath(l), pofile(l)) for l in  pos],
            )
        )
    ADMIN_MEDIA_PREFIX = settings.ADMIN_MEDIA_PREFIX
    version = rosetta.get_version(True)
    return render_to_response('rosetta/languages.html', locals(), context_instance=RequestContext(request))
Exemplo n.º 6
0
def get_po_from_filepath(path):
    """Get the .po file at the given path and raise an error if it
    doesn't exist.
    """
    if not os.path.exists(path):
        raise IOError('File not found at path %s' % path)
    return polib.pofile(path)
Exemplo n.º 7
0
def list_languages(request):
    """
    Lists the languages for the current project, the gettext catalog files
    that can be translated and their translation progress
    """
    languages = []
    
    if 'filter' in request.GET:
        if request.GET.get('filter') in ('project', 'third-party', 'django', 'all'):
            filter_ = request.GET.get('filter')
            request.session['rosetta_i18n_catalog_filter'] = filter_
            return HttpResponseRedirect(reverse('rosetta-pick-file'))
    
    rosetta_i18n_catalog_filter = request.session.get('rosetta_i18n_catalog_filter', 'project')
    
    third_party_apps = rosetta_i18n_catalog_filter in ('all', 'third-party')
    django_apps = rosetta_i18n_catalog_filter in ('all', 'django')
    project_apps = rosetta_i18n_catalog_filter in ('all', 'project')
    
    has_pos = False
    for language in settings.LANGUAGES:
        pos = find_pos(language[0], project_apps=project_apps,django_apps=django_apps,third_party_apps=third_party_apps)
        has_pos = has_pos or len(pos)
        languages.append(
            (language[0], 
            _(language[1]),
            [(get_app_name(l), os.path.realpath(l), pofile(l)) for l in  pos],
            )
        )
    ADMIN_MEDIA_PREFIX = settings.ADMIN_MEDIA_PREFIX
    version = rosetta.get_version(True)
    return render_to_response('rosetta/languages.html', locals(), context_instance=RequestContext(request))    
Exemplo n.º 8
0
def lang_sel(request,langid,idx):
    """
    Selects a file to be translated
    """
    if langid not in [l[0] for l in settings.LANGUAGES]:
        raise Http404
    else:
        
        do_django = 'django' in request.GET
        do_rosetta = 'rosetta' in request.GET
        
        file_ = find_pos(langid,include_djangos=do_django,include_rosetta=do_rosetta)[int(idx)]
        
        request.session['rosetta_i18n_lang_code'] = langid
        request.session['rosetta_i18n_lang_name'] = unicode([l[1] for l in settings.LANGUAGES if l[0] == langid][0])
        request.session['rosetta_i18n_fn'] = file_
        po = pofile(file_)
        for entry in po:
            entry.md5hash = hashlib.md5(entry.msgid.encode("utf8")+entry.msgstr.encode("utf8")).hexdigest()

            
        request.session['rosetta_i18n_pofile'] = po
        try:
            os.utime(file_,None)
            request.session['rosetta_i18n_write'] = True
        except OSError:
            request.session['rosetta_i18n_write'] = False
            
        return HttpResponseRedirect(reverse('rosetta-home'))
Exemplo n.º 9
0
def lang_sel(request,langid,idx):
    """
    Selects a file to be translated
    """
    if langid not in [l[0] for l in settings.LANGUAGES]:
        raise Http404
    else:
        
        rosetta_i18n_catalog_filter = request.session.get('rosetta_i18n_catalog_filter', 'project')
        
        third_party_apps = rosetta_i18n_catalog_filter in ('all', 'third-party')
        django_apps = rosetta_i18n_catalog_filter in ('all', 'django')
        project_apps = rosetta_i18n_catalog_filter in ('all', 'project')
        
        file_ = find_pos(langid, project_apps=project_apps,django_apps=django_apps,third_party_apps=third_party_apps)[int(idx)]
        
        request.session['rosetta_i18n_lang_code'] = langid
        request.session['rosetta_i18n_lang_name'] = unicode([l[1] for l in settings.LANGUAGES if l[0] == langid][0])
        request.session['rosetta_i18n_fn'] = file_
        po = pofile(file_)
        for entry in po:
            entry.md5hash = hashlib.md5(entry.msgid.encode("utf8")+entry.msgstr.encode("utf8")).hexdigest()

            
        request.session['rosetta_i18n_pofile'] = po
        try:
            os.utime(file_,None)
            request.session['rosetta_i18n_write'] = True
        except OSError:
            request.session['rosetta_i18n_write'] = False
            
        return HttpResponseRedirect(reverse('rosetta-home'))
Exemplo n.º 10
0
def lang_sel(request, langid, idx):
    """
    Selects a file to be translated
    """
    storage = get_storage(request)
    if langid not in [l[0] for l in settings.LANGUAGES]:
        raise Http404
    else:

        rosetta_i18n_catalog_filter = storage.get('rosetta_i18n_catalog_filter', 'project')

        third_party_apps = rosetta_i18n_catalog_filter in ('all', 'third-party')
        django_apps = rosetta_i18n_catalog_filter in ('all', 'django')
        project_apps = rosetta_i18n_catalog_filter in ('all', 'project')
        file_ = find_pos(langid, project_apps=project_apps, django_apps=django_apps, third_party_apps=third_party_apps)[int(idx)]

        storage.set('rosetta_i18n_lang_code', langid)
        storage.set('rosetta_i18n_lang_name', unicode([l[1] for l in settings.LANGUAGES if l[0] == langid][0]))
        storage.set('rosetta_i18n_fn',  file_)
        po = pofile(file_)
        for entry in po:
            entry.md5hash = hashlib.md5(
                entry.msgid.encode("utf8") +
                entry.msgstr.encode("utf8") +
                (entry.msgctxt and entry.msgctxt.encode("utf8") or "")
            ).hexdigest()

        storage.set('rosetta_i18n_pofile', po)
        try:
            os.utime(file_, None)
            storage.set('rosetta_i18n_write', True)
        except OSError:
            storage.set('rosetta_i18n_write', False)

        return HttpResponseRedirect(reverse('rosetta-home'))
Exemplo n.º 11
0
def lang_sel(request,langid,idx):
    """
    Selects a file to be translated
    """
    if langid not in [l[0] for l in settings.LANGUAGES]:
        raise Http404
    else:
        
        do_django = 'django' in request.GET
        do_rosetta = 'rosetta' in request.GET
        
        file_ = find_pos(langid,include_djangos=do_django,include_rosetta=do_rosetta)[int(idx)]
        
        request.session['rosetta_i18n_lang_code'] = langid
        request.session['rosetta_i18n_lang_name'] = unicode([l[1] for l in settings.LANGUAGES if l[0] == langid][0])
        request.session['rosetta_i18n_fn'] = file_
        po = pofile(file_)
        for entry in po:
            entry.md5hash = hashlib.md5(entry.msgid.encode("utf8")+entry.msgstr.encode("utf8")).hexdigest()

            
        request.session['rosetta_i18n_pofile'] = po
        try:
            os.utime(file_,None)
            request.session['rosetta_i18n_write'] = True
        except OSError:
            request.session['rosetta_i18n_write'] = False
            
        return HttpResponseRedirect(reverse('rosetta-home'))
Exemplo n.º 12
0
    def do_language(self, lang):
        old_file = os.path.join(self.old_tree, lang, 'LC_MESSAGES/django.po')
        new_file = os.path.join(self.new_tree, lang, 'LC_MESSAGES/django.po')

        old_props = []
        po = polib.pofile(old_file)
        #
        # Find old props
        #
        for entry in po:
            # do something with entry...
            if self.entry_is_property(entry):
                old_props.append(entry)

        #
        # Add old props to new file
        #
        po = polib.pofile(new_file)
        for old_entry in old_props:
            if old_entry.obsolete:
                old_entry.obsolete = 0
            if 'fuzzy' in old_entry.flags:
                old_entry.flags.remove('fuzzy')
            entry = po.find(old_entry.msgid)
            if entry:
                po.remove(entry)
                po.append(old_entry)
            else:
                # only found in old file
                po.append(old_entry)
                
        #
        # Set defaults for empty props
        #
        for entry in po:
            if entry.msgid.find('embedCreate_t') > -1:
                a = 3
            if self.entry_is_property(entry):
                if entry.obsolete:
                    entry.obsolete = 0
                if 'fuzzy' in entry.flags:
                    entry.flags.remove('fuzzy')
                if not self.is_valid_content(entry):
                    entry.msgstr = entry.msgid.split('[')[1].split(']')[0]
                
        po.save()
Exemplo n.º 13
0
def list_languages():
    languages = []
    has_pos = False
    for language in settings.LANGUAGES:
        pos = find_pos(language[0])        
        has_pos = has_pos or len(pos)
        languages.append(
            (language[0], 
            language[1],
            [(get_app_name(l), os.path.realpath(l), pofile(l)) for l in  pos],
            )
        )
    return languages
Exemplo n.º 14
0
    def setup_mofile_with_entry(self, poentry, language='en'):
        locale_location = join(settings.LOCALE_DIR, 'locale', language, 'LC_MESSAGES')
        pofile = polib.pofile(join(locale_location, 'django.po'))

        pofile.append(poentry)
        pofile.save()
        pofile.save_as_mofile(join(locale_location, 'django.mo'))
        
        jit_locale = gettext_module.translation('django', join(settings.LOCALE_DIR, 'locale'), [trans_real.to_locale(language)], trans_real.DjangoTranslation)
        jit_locale.set_language(language)
        
        locale = trans_real.translation(language)
        locale.merge(jit_locale)
Exemplo n.º 15
0
def list_languages(request, do_session_warn=False):
    """
    Lists the languages for the current project, the gettext catalog files
    that can be translated and their translation progress
    """
    storage = get_storage(request)
    languages = []

    if 'filter' in request.GET:
        if request.GET.get('filter') in ('project', 'third-party', 'django',
                                         'all'):
            filter_ = request.GET.get('filter')
            storage.set('rosetta_i18n_catalog_filter', filter_)
            return HttpResponseRedirect(reverse('rosetta-pick-file'))

    rosetta_i18n_catalog_filter = storage.get('rosetta_i18n_catalog_filter',
                                              'project')

    third_party_apps = rosetta_i18n_catalog_filter in ('all', 'third-party')
    django_apps = rosetta_i18n_catalog_filter in ('all', 'django')
    project_apps = rosetta_i18n_catalog_filter in ('all', 'project')

    has_pos = False
    for language in settings.LANGUAGES:
        pos = find_pos(language[0],
                       project_apps=project_apps,
                       django_apps=django_apps,
                       third_party_apps=third_party_apps)
        has_pos = has_pos or len(pos)
        languages.append((
            language[0],
            _(language[1]),
            sorted([(get_app_name(l), os.path.realpath(l), pofile(l))
                    for l in pos],
                   key=lambda app: app[0]),
        ))
    try:
        ADMIN_MEDIA_PREFIX = settings.ADMIN_MEDIA_PREFIX
    except AttributeError:
        ADMIN_MEDIA_PREFIX = settings.STATIC_URL + 'admin/'
    do_session_warn = do_session_warn and 'SessionRosettaStorage' in rosetta_settings.STORAGE_CLASS and 'signed_cookies' in settings.SESSION_ENGINE

    return render_to_response(
        'rosetta/languages.html',
        dict(version=rosetta.get_version(True),
             ADMIN_MEDIA_PREFIX=ADMIN_MEDIA_PREFIX,
             do_session_warn=do_session_warn,
             languages=languages,
             has_pos=has_pos,
             rosetta_i18n_catalog_filter=rosetta_i18n_catalog_filter),
        context_instance=RequestContext(request))
Exemplo n.º 16
0
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
    do_rosetta = "rosetta" in request.GET
    has_pos = False
    for language in settings.LANGUAGES:
        pos = find_pos(language[0], include_djangos=do_django, include_rosetta=do_rosetta)
        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 = settings.ADMIN_MEDIA_PREFIX
    version = rosetta.get_version(True)
    return render_to_response("rosetta/languages.html", locals())
Exemplo n.º 17
0
    def setup_mofile_with_entry(self, poentry, language='en'):
        locale_location = join(settings.LOCALE_DIR, 'locale', language,
                               'LC_MESSAGES')
        pofile = polib.pofile(join(locale_location, 'django.po'))

        pofile.append(poentry)
        pofile.save()
        pofile.save_as_mofile(join(locale_location, 'django.mo'))

        jit_locale = gettext_module.translation(
            'django', join(settings.LOCALE_DIR, 'locale'),
            [trans_real.to_locale(language)], trans_real.DjangoTranslation)
        jit_locale.set_language(language)

        locale = trans_real.translation(language)
        locale.merge(jit_locale)
Exemplo n.º 18
0
def list_languages(request, do_session_warn=False):
    """
    Lists the languages for the current project, the gettext catalog files
    that can be translated and their translation progress
    """
    storage = get_storage(request)
    languages = []

    if 'filter' in request.GET:
        if request.GET.get('filter') in ('project', 'third-party', 'django', 'all'):
            filter_ = request.GET.get('filter')
            storage.set('rosetta_i18n_catalog_filter', filter_)
            return HttpResponseRedirect(reverse('rosetta-pick-file'))

    rosetta_i18n_catalog_filter = storage.get('rosetta_i18n_catalog_filter', 'project')

    third_party_apps = rosetta_i18n_catalog_filter in ('all', 'third-party')
    django_apps = rosetta_i18n_catalog_filter in ('all', 'django')
    project_apps = rosetta_i18n_catalog_filter in ('all', 'project')

    has_pos = False
    for language in settings.LANGUAGES:
        pos = find_pos(language[0], project_apps=project_apps, django_apps=django_apps, third_party_apps=third_party_apps)
        has_pos = has_pos or len(pos)
        languages.append(
            (language[0],
            _(language[1]),
            sorted([(get_app_name(l), os.path.realpath(l), pofile(l)) for l in pos], key=lambda app: app[0]),
            )
        )
    try:
        ADMIN_MEDIA_PREFIX = settings.ADMIN_MEDIA_PREFIX
    except AttributeError:
        ADMIN_MEDIA_PREFIX = settings.STATIC_URL + 'admin/'
    do_session_warn = do_session_warn and 'SessionRosettaStorage' in rosetta_settings.STORAGE_CLASS and 'signed_cookies' in settings.SESSION_ENGINE

    return render_to_response('rosetta/languages.html', dict(
        version=rosetta.get_version(True),
        ADMIN_MEDIA_PREFIX=ADMIN_MEDIA_PREFIX,
        do_session_warn=do_session_warn,
        languages=languages,
        has_pos=has_pos,
        rosetta_i18n_catalog_filter=rosetta_i18n_catalog_filter
    ), context_instance=RequestContext(request))
Exemplo n.º 19
0
def lang_sel(request, langid, idx):
    """
    Selects a file to be translated
    """
    storage = get_storage(request)
    if langid not in [l[0] for l in settings.LANGUAGES]:
        raise Http404
    else:

        rosetta_i18n_catalog_filter = storage.get(
            'rosetta_i18n_catalog_filter', 'project')

        third_party_apps = rosetta_i18n_catalog_filter in ('all',
                                                           'third-party')
        django_apps = rosetta_i18n_catalog_filter in ('all', 'django')
        project_apps = rosetta_i18n_catalog_filter in ('all', 'project')
        file_ = sorted(find_pos(langid,
                                project_apps=project_apps,
                                django_apps=django_apps,
                                third_party_apps=third_party_apps),
                       key=get_app_name)[int(idx)]

        storage.set('rosetta_i18n_lang_code', langid)
        storage.set(
            'rosetta_i18n_lang_name',
            six.text_type([l[1] for l in settings.LANGUAGES
                           if l[0] == langid][0]))
        storage.set('rosetta_i18n_fn', file_)
        po = pofile(file_)
        for entry in po:
            entry.md5hash = hashlib.new(
                'md5', (six.text_type(entry.msgid) +
                        six.text_type(entry.msgstr) + six.text_type(
                            entry.msgctxt or "")).encode('utf8')).hexdigest()

        storage.set('rosetta_i18n_pofile', po)
        try:
            os.utime(file_, None)
            storage.set('rosetta_i18n_write', True)
        except OSError:
            storage.set('rosetta_i18n_write', False)

        return HttpResponseRedirect(reverse('rosetta-home'))
Exemplo n.º 20
0
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
    do_rosetta = 'rosetta' in request.GET
    has_pos = False
    for language in settings.LANGUAGES:
        pos = find_pos(language[0],include_djangos=do_django,include_rosetta=do_rosetta)        
        has_pos = has_pos or len(pos)
        languages.append(
            (language[0], 
            _(language[1]),
            [(get_app_name(l), os.path.realpath(l), pofile(l)) for l in  pos],
            )
        )
    ADMIN_MEDIA_PREFIX = settings.ADMIN_MEDIA_PREFIX
    version = rosetta.get_version(True)
    return render_to_response('rosetta/languages.html', locals(), context_instance=RequestContext(request))    
Exemplo n.º 21
0
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_

    storage = get_storage(request)
    version = rosetta.get_version(True)
    if storage.has('rosetta_i18n_fn'):
        rosetta_i18n_fn = storage.get('rosetta_i18n_fn')
        rosetta_i18n_app = get_app_name(rosetta_i18n_fn)
        rosetta_i18n_lang_code = storage.get('rosetta_i18n_lang_code')
        rosetta_i18n_lang_bidi = rosetta_i18n_lang_code.split('-')[0] in settings.LANGUAGES_BIDI
        rosetta_i18n_write = storage.get('rosetta_i18n_write', True)
        if rosetta_i18n_write:
            rosetta_i18n_pofile = pofile(rosetta_i18n_fn, wrapwidth=rosetta_settings.POFILE_WRAP_WIDTH)
            for entry in rosetta_i18n_pofile:
                entry.md5hash = hashlib.md5(
                    entry.msgid.encode("utf8") +
                    entry.msgstr.encode("utf8") +
                    (entry.msgctxt and entry.msgctxt.encode("utf8") or "")
                ).hexdigest()

        else:
            rosetta_i18n_pofile = storage.get('rosetta_i18n_pofile')

        if 'filter' in request.GET:
            if request.GET.get('filter') in ('untranslated', 'translated', 'fuzzy', 'all'):
                filter_ = request.GET.get('filter')
                storage.set('rosetta_i18n_filter', filter_)
                return HttpResponseRedirect(reverse('rosetta-home'))

        rosetta_i18n_filter = storage.get('rosetta_i18n_filter', 'all')

        if '_next' in request.POST:
            rx = re.compile(r'^m_([0-9a-f]+)')
            rx_plural = re.compile(r'^m_([0-9a-f]+)_([0-9]+)')
            file_change = False
            for key, value in request.POST.items():
                md5hash = None
                plural_id = None

                if rx_plural.match(key):
                    md5hash = str(rx_plural.match(key).groups()[0])
                    # polib parses .po files into unicode strings, but
                    # doesn't bother to convert plural indexes to int,
                    # so we need unicode here.
                    plural_id = unicode(rx_plural.match(key).groups()[1])

                elif rx.match(key):
                    md5hash = str(rx.match(key).groups()[0])

                if md5hash is not None:
                    entry = rosetta_i18n_pofile.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:
                            #plural_string = fix_nls(entry.msgstr_plural[plural_id], value)
                            plural_string = fix_nls(entry.msgid_plural, value)
                            entry.msgstr_plural[plural_id] = plural_string
                        else:
                            entry.msgstr = fix_nls(entry.msgid, value)

                        is_fuzzy = bool(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 != value or old_fuzzy != is_fuzzy:
                            entry_changed.send(sender=entry,
                                               user=request.user,
                                               old_msgstr=old_msgstr,
                                               old_fuzzy=old_fuzzy,
                                               pofile=rosetta_i18n_fn,
                                               language_code=rosetta_i18n_lang_code,
                                               )

                    else:
                        storage.set('rosetta_last_save_error', True)

            if file_change and rosetta_i18n_write:
                try:
                    # Provide defaults in case authorization is not required.
                    request.user.first_name = getattr(request.user, 'first_name', 'Anonymous')
                    request.user.last_name = getattr(request.user, 'last_name', 'User')
                    request.user.email = getattr(request.user, 'email', '*****@*****.**')

                    rosetta_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')
                    rosetta_i18n_pofile.metadata['X-Translated-Using'] = u"django-rosetta %s" % rosetta.get_version(False)
                    rosetta_i18n_pofile.metadata['PO-Revision-Date'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M%z')
                except UnicodeDecodeError:
                    pass

                try:
                    rosetta_i18n_pofile.save()
                    po_filepath, ext = os.path.splitext(rosetta_i18n_fn)
                    save_as_mo_filepath = po_filepath + '.mo'
                    rosetta_i18n_pofile.save_as_mofile(save_as_mo_filepath)

                    post_save.send(sender=None, language_code=rosetta_i18n_lang_code, request=request)
                    # Try auto-reloading via the WSGI daemon mode reload mechanism
                    if  rosetta_settings.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
                    # Try auto-reloading via uwsgi daemon reload mechanism
                    if rosetta_settings.UWSGI_AUTO_RELOAD:
                        try:
                            import uwsgi
                            # pretty easy right?
                            uwsgi.reload()
                        except:
                            # we may not be running under uwsgi :P
                            pass

                except:
                    storage.set('rosetta_i18n_write', False)
                storage.set('rosetta_i18n_pofile', rosetta_i18n_pofile)

                # Retain query arguments
                query_arg = '?_next=1'
                if 'query' in request.GET or 'query' in request.POST:
                    query_arg += '&query=%s' % request.REQUEST.get('query')
                if 'page' in request.GET:
                    query_arg += '&page=%d&_next=1' % int(request.GET.get('page'))
                return HttpResponseRedirect(reverse('rosetta-home') + iri_to_uri(query_arg))
        rosetta_i18n_lang_name = _(storage.get('rosetta_i18n_lang_name'))
        rosetta_i18n_lang_code = storage.get('rosetta_i18n_lang_code')

        if 'query' in request.REQUEST and request.REQUEST.get('query', '').strip():
            query = request.REQUEST.get('query').strip()
            rx = re.compile(re.escape(query), re.IGNORECASE)
            paginator = Paginator([e for e in rosetta_i18n_pofile if not e.obsolete and rx.search(smart_unicode(e.msgstr) + smart_unicode(e.msgid) + u''.join([o[0] for o in e.occurrences]))], rosetta_settings.MESSAGES_PER_PAGE)
        else:
            if rosetta_i18n_filter == 'untranslated':
                paginator = Paginator(rosetta_i18n_pofile.untranslated_entries(), rosetta_settings.MESSAGES_PER_PAGE)
            elif rosetta_i18n_filter == 'translated':
                paginator = Paginator(rosetta_i18n_pofile.translated_entries(), rosetta_settings.MESSAGES_PER_PAGE)
            elif rosetta_i18n_filter == 'fuzzy':
                paginator = Paginator([e for e in rosetta_i18n_pofile.fuzzy_entries() if not e.obsolete], rosetta_settings.MESSAGES_PER_PAGE)
            else:
                paginator = Paginator([e for e in rosetta_i18n_pofile if not e.obsolete], rosetta_settings.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 '_next' in request.GET or '_next' in request.POST:
            page += 1
            if page > paginator.num_pages:
                page = 1
            query_arg = '?page=%d' % page
            return HttpResponseRedirect(reverse('rosetta-home') + iri_to_uri(query_arg))

        rosetta_messages = paginator.page(page).object_list

        if rosetta_settings.MAIN_LANGUAGE and rosetta_settings.MAIN_LANGUAGE != rosetta_i18n_lang_code:

            main_language = None
            for language in settings.LANGUAGES:
                if language[0] == rosetta_settings.MAIN_LANGUAGE:
                    main_language = _(language[1])
                    break

            fl = ("/%s/" % rosetta_settings.MAIN_LANGUAGE).join(rosetta_i18n_fn.split("/%s/" % rosetta_i18n_lang_code))
            po = pofile(fl)

            main_messages = []
            for message in rosetta_messages:
                message.main_lang = po.find(message.msgid).msgstr

        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)
        try:
            ADMIN_MEDIA_PREFIX = settings.ADMIN_MEDIA_PREFIX
            ADMIN_IMAGE_DIR = ADMIN_MEDIA_PREFIX + 'img/admin/'
        except AttributeError:
            ADMIN_MEDIA_PREFIX = settings.STATIC_URL + 'admin/'
            ADMIN_IMAGE_DIR = ADMIN_MEDIA_PREFIX + 'img/'
        ENABLE_TRANSLATION_SUGGESTIONS = rosetta_settings.BING_APP_ID and rosetta_settings.ENABLE_TRANSLATION_SUGGESTIONS
        BING_APP_ID = rosetta_settings.BING_APP_ID
        MESSAGES_SOURCE_LANGUAGE_NAME = rosetta_settings.MESSAGES_SOURCE_LANGUAGE_NAME
        MESSAGES_SOURCE_LANGUAGE_CODE = rosetta_settings.MESSAGES_SOURCE_LANGUAGE_CODE
        if storage.has('rosetta_last_save_error'):
            storage.delete('rosetta_last_save_error')
            rosetta_last_save_error = True

        for message in rosetta_messages:
            if not message.msgid_plural:
                continue

            tmp = SortedDict()
            keylist = sorted(map(int, message.msgstr_plural.keys()))
            for k in keylist:
                tmp[k] = message.msgstr_plural[str(k)]

            message.msgstr_plural = tmp

        tmpl = 'rosetta/pofile.html'
        if 'singlepage' in rosetta_i18n_fn:
            tmpl = 'rosetta/pofile_singlepage.html'

        return render_to_response(tmpl, locals(), context_instance=RequestContext(request))
    else:
        return list_languages(request, do_session_warn=True)
Exemplo n.º 22
0
def save_message(msgid, msgtxt, locale):
    """
    Saves a translated message (msgtxt) to all the po files that have the msgid

    @param msgid: msgid as it appears in the po files
    @param msgtxt: message text
    @param locale: locale of the translation, e.g.: nl_NL, nl_BE, fr_BE,
    @param request: http request
    @return: list with all the changed po files
    """
    # Validate the translated message, it must have the same amount of variable definitions
    if not validate_variables(msgid, msgtxt):
        raise ValueError('Invalid translation, unmatched variables')

    # file_ = find_pos(langid, project_apps=project_apps, django_apps=django_apps,
    #stor = storage.get_storage(request)
    files = []
    catalog = get_locale_catalog(locale)
    pofiles = find_pos(locale, third_party_apps=THIRD_PARTY_APPS)

    #print "Post 1 = ", str(source), ", ", str(target_locale), ", ", str(target_msg)
    #print "Post 1.1 = ", str(settings.SOURCE_LANGUAGE_CODE), ", ", str(request.LANGUAGE_CODE)
    #print "Post = ", repr(stor), ", ", repr(pos)

    translated = catalog.dict.get(msgid, None)

    # Update the message in the catalog
    if translated:
        translated.msgstr = msgtxt
        make_entry_valid(translated)
        catalog.dict[msgid] = translated
        request = getattr(THREAD_LOCAL_STORAGE, REQUEST, None)
        cache_key_locale = get_cache_key(locale)
        storage = get_storage(request)
        storage.set(cache_key_locale, catalog)

    # Save the translation in all the po files that have msgid
    logger.info('Saving msgid %s ' % msgid)
    saved = False
    for path in pofiles:
        pfile = pofile(path)
        po_entry = pfile.find(msgid)

        if po_entry:
            po_entry.msgstr = msgtxt
            make_entry_valid(po_entry)

            # Save the pofile
            pfile.save()
            files.append(path)

            # Save the mofile
            popath, ext = os.path.splitext(path)
            pfile.save_as_mofile(popath + ".mo")

            saved = True
            logger.info('Saved to %s' % path)
            #po_filepath, ext = os.path.splitext(p)
            #save_as_mo_filepath = po_filepath + '.mo'
            #file.save_as_mofile(save_as_mo_filepath)
            #print "Msg = ", repr(po_entry), ", ", str(po_entry)

    if not saved:
        logger.error('Did not save to any file')

    return files
Exemplo n.º 23
0
def translate(request, appname, rosetta_i18n_lang_code, filter='all', page=1):
    """
    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_ += "\n"
        elif "\n" != in_[-1] and "\n" == out_[-1]:
            out_ = out_.rstrip()

        return out_

    po = pofile_by_appname(appname, rosetta_i18n_lang_code, request.user)
    rosetta_i18n_fn    = po['path']
    rosetta_i18n_write = po['writable']
    rosetta_last_save_error = False

    if '_next' in request.POST:
        rx = re.compile(r'm_([0-9a-f]+)')
        rx_plural = re.compile(r'm_([0-9a-f]+)_([0-9]+)')
        file_change = False

        for key, value in request.POST.items():
            md5hash = None
            plural_id = None

            if rx_plural.match(key):
                md5hash = str(rx_plural.match(key).groups()[0])
                # polib parses .po files into unicode strings, but
                # doesn't bother to convert plural indexes to int,
                # so we need unicode here.
                plural_id = unicode(rx_plural.match(key).groups()[1])

            elif rx.match(key):
                md5hash = str(rx.match(key).groups()[0])

            if md5hash is not None:
                entry = po['entries'].get(md5hash)
                # If someone did a makemessage, some entries might
                # have been removed, so we need to check.
                if entry:
                    entry_change = False
                    old_msgstr = entry.msgstr

                    if plural_id is not None:
                        plural_string = fix_nls(entry.msgstr_plural[plural_id], value)
                        entry_change = entry_changed or entry.msgstr_plural[plural_id] != plural_string
                        entry.msgstr_plural[plural_id] = plural_string
                    else:
                        msgstr = fix_nls(entry.msgid, value)
                        entry_change = entry_changed or entry.msgstr != msgstr
                        entry.msgstr = msgstr

                    old_fuzzy = 'fuzzy' in entry.flags
                    new_fuzzy = bool(request.POST.get('f_%s' % md5hash, False))
                    if new_fuzzy != old_fuzzy:
                        entry_change = True
                        if new_fuzzy:
                            entry.flags.append('fuzzy')
                        else:
                            entry.flags.remove('fuzzy')

                    if entry_change:
                        file_change = True
                        entry_changed.send(sender=entry,
                                           user=request.user,
                                           old_msgstr = old_msgstr,
                                           old_fuzzy = old_fuzzy,
                                           pofile = po['pofile'],
                                           language_code = rosetta_i18n_lang_code,
                                        )
                else:
                    rosetta_last_save_error = True

        if file_change:
            try:
                po['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')
                po['pofile'].metadata['X-Translated-Using'] = u"django-rosetta %s" % rosetta.get_version(False)
                po['pofile'].metadata['PO-Revision-Date'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M%z')
            except UnicodeDecodeError:
                pass

            po['last_modified'] = time.time()
            po = poutil.upd_pofile(po)

            post_save.send(sender=None,language_code=rosetta_i18n_lang_code,request=request)

            # Try auto-reloading via the WSGI daemon mode reload mechanism
            if  rosetta_settings.WSGI_AUTO_RELOAD and \
                request.environ.has_key('mod_wsgi.process_group') and \
                request.environ.get('mod_wsgi.process_group',None) and \
                request.environ.has_key('SCRIPT_FILENAME') and \
                int(request.environ.get('mod_wsgi.script_reloading', '0')):
                    try:
                        os.utime(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 # pretty easy right?
                    uwsgi.reload()
                except: # we may not be running under uwsgi :P
                    pass

            return shortcuts.redirect(request.path + '?' + request.META['QUERY_STRING'])

    if filter == 'untranslated':
        entries_source = po['pofile'].untranslated_entries()
    elif filter == 'translated':
        entries_source = po['pofile'].translated_entries()
    elif filter == 'fuzzy':
        entries_source = po['pofile'].fuzzy_entries()
    else:
        entries_source = (e for e in po['pofile'] if not e.obsolete)

    query = request.GET.get('q', '').strip()
    if query:
        rx = re.compile(re.escape(query), re.IGNORECASE)
        entries_source = (e for e in entries_source if rx.search("\n".join((smart_unicode(e.msgstr), smart_unicode(e.msgid), smart_unicode(e.comment), u"\t".join([o[0] for o in e.occurrences])))))

    paginator = Paginator(list(entries_source), rosetta_settings.MESSAGES_PER_PAGE)

    if int(page) <= paginator.num_pages and int(page) > 0:
        page = int(page)
    else:
        page = 1

    rosetta_messages = paginator.page(page).object_list
    for message in rosetta_messages:
        message.md5hash = hashlib.md5(message.msgid.encode('utf8')).hexdigest()

    if rosetta_settings.MAIN_LANGUAGE and rosetta_settings.MAIN_LANGUAGE != rosetta_i18n_lang_code:
        main_language = dict(rosetta_settings.LANGUAGES).get(rosetta_settings.MAIN_LANGUAGE)

        fl = ("/%s/" % rosetta_settings.MAIN_LANGUAGE).join(rosetta_i18n_fn.split("/%s/" % rosetta_i18n_lang_code))
        po = polib.pofile(  fl,
                            klass=poutil.SmartPOFile,
                            wrapwidth=rosetta_settings.POFILE_WRAP_WIDTH
                        )

        main_messages = []
        for message in messages:
            message.main_lang = po.find(message.msgid).msgstr

    return shortcuts.render_to_response('rosetta/pofile.html', {
            'rosetta_i18n_fn'       : rosetta_i18n_fn,
            'rosetta_i18n_write'    : rosetta_i18n_write,
            'rosetta_i18n_pofile'   : po['pofile'],
            'rosetta_i18n_lang_bidi': get_language_info(rosetta_i18n_lang_code)['bidi'],
            'rosetta_messages'      : rosetta_messages,
            'rosetta_i18n_lang_name': dict(rosetta_settings.LANGUAGES)[rosetta_i18n_lang_code],
            'rosetta_i18n_lang_code': rosetta_i18n_lang_code,
            'rosetta_i18n_app'      : appname,
            'rosetta_i18n_filter'   : filter,
            'rosetta_last_save_error' : rosetta_last_save_error,

            'ENABLE_TRANSLATION_SUGGESTIONS' : rosetta_settings.ENABLE_TRANSLATION_SUGGESTIONS and \
                                rosetta_settings.MESSAGES_SOURCE_LANGUAGE_CODE != rosetta_i18n_lang_code,
            'BING_APPID'                     : rosetta_settings.BING_APPID,
            'GOOGLE_API_KEY'                 : rosetta_settings.GOOGLE_API_KEY,

            'MESSAGES_SOURCE_LANGUAGE_NAME'  : rosetta_settings.MESSAGES_SOURCE_LANGUAGE_NAME,
            'MESSAGES_SOURCE_LANGUAGE_CODE'  : rosetta_settings.MESSAGES_SOURCE_LANGUAGE_CODE,

            'query'                   : query,
            'paginator'               : paginator,
            'needs_pagination'        : paginator.num_pages > 1,
            'page_range'              : poutil.pagination_range(1, paginator.num_pages, page),
            'page'                    : page,
        }, context_instance=template.RequestContext(request))
Exemplo n.º 24
0
#!/usr/bin/env python
import os, sys
import glob
import re

from translate import Translator
from rosetta import polib

po_files = glob.glob("*/locale/*/LC_MESSAGES/*.po")

for po_file in po_files:
    m = re.search('locale/(\w+)/LC_', po_file)

    translator = Translator(to_lang=m.group(1))

    po = polib.pofile(po_file)

    for entry in po:
        if entry.translated():
            continue

        try:
            translation = translator.translate(entry.msgid.encode('utf8'))
            try:
                translation = translation.decode('unicode_escape')
            except:
                pass
            print translation
            entry.msgstr = translation
        except KeyError:
            print "ERROR:" + entry.msgid
Exemplo n.º 25
0
def save_message(msgid, msgtxt, locale):
    """
    Saves a translated message (msgtxt) to all the po files that have the msgid

    @param msgid: msgid as it appears in the po files
    @param msgtxt: message text
    @param locale: locale of the translation, e.g.: nl_NL, nl_BE, fr_BE,
    @param request: http request
    @return: list with all the changed po files
    """
    # Validate the translated message, it must have the same amount of variable definitions
    if not validate_variables(msgid, msgtxt):
        raise ValueError('Invalid translation, unmatched variables')

    # file_ = find_pos(langid, project_apps=project_apps, django_apps=django_apps,
    #stor = storage.get_storage(request)
    files = []
    catalog = get_locale_catalog(locale)
    pofiles = find_pos(locale, third_party_apps=THIRD_PARTY_APPS)

    #print "Post 1 = ", str(source), ", ", str(target_locale), ", ", str(target_msg)
    #print "Post 1.1 = ", str(settings.SOURCE_LANGUAGE_CODE), ", ", str(request.LANGUAGE_CODE)
    #print "Post = ", repr(stor), ", ", repr(pos)

    translated = catalog.dict.get(msgid, None)

    # Update the message in the catalog
    if translated:
        translated.msgstr = msgtxt
        make_entry_valid(translated)
        catalog.dict[msgid] = translated
        request = getattr(THREAD_LOCAL_STORAGE, REQUEST, None)
        cache_key_locale = get_cache_key(locale)
        storage = get_storage(request)
        storage.set(cache_key_locale, catalog)

    # Save the translation in all the po files that have msgid
    logger.info('Saving msgid %s ' % msgid)
    saved = False
    for path in pofiles:
        pfile = pofile(path)
        po_entry = pfile.find(msgid)

        if po_entry:
            po_entry.msgstr = msgtxt
            make_entry_valid(po_entry)

            # Save the pofile
            pfile.save()
            files.append(path)

            # Save the mofile
            popath, ext = os.path.splitext(path)
            pfile.save_as_mofile(popath + ".mo")

            saved = True
            logger.info('Saved to %s' % path)
            #po_filepath, ext = os.path.splitext(p)
            #save_as_mo_filepath = po_filepath + '.mo'
            #file.save_as_mofile(save_as_mo_filepath)
            #print "Msg = ", repr(po_entry), ", ", str(po_entry)

    if not saved:
        logger.error('Did not save to any file')

    return files
Exemplo n.º 26
0
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 = rosetta.get_version(True)
    if 'rosetta_i18n_fn' in request.session:
        rosetta_i18n_fn=request.session.get('rosetta_i18n_fn')
        rosetta_i18n_app = get_app_name(rosetta_i18n_fn)
        rosetta_i18n_pofile = request.session.get('rosetta_i18n_pofile')
        rosetta_i18n_lang_code = request.session['rosetta_i18n_lang_code']
        rosetta_i18n_lang_bidi = (rosetta_i18n_lang_code in settings.LANGUAGES_BIDI)
        rosetta_i18n_write = request.session.get('rosetta_i18n_write', True)
        
        if 'filter' in request.GET:
            if request.GET.get('filter') in ('untranslated', 'translated', 'fuzzy', 'all'):
                filter_ = request.GET.get('filter')
                request.session['rosetta_i18n_filter'] = filter_
                return HttpResponseRedirect(reverse('rosetta-home'))
        
        rosetta_i18n_filter = request.session.get('rosetta_i18n_filter', 'all')
        
        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])
                    rosetta_i18n_pofile[id].msgstr_plural[str(idx)] = fix_nls(rosetta_i18n_pofile[id].msgid_plural[idx], request.POST.get(k))
                    file_change = True
                elif rx.match(k):
                    id=int(rx.match(k).groups()[0])
                    rosetta_i18n_pofile[id].msgstr = fix_nls(rosetta_i18n_pofile[id].msgid, request.POST.get(k))
                    file_change = True
                    
                if file_change and 'fuzzy' in rosetta_i18n_pofile[id].flags and not request.POST.get('f_%d' %id, False):
                    rosetta_i18n_pofile[id].flags.remove('fuzzy')
                elif file_change and 'fuzzy' not in rosetta_i18n_pofile[id].flags and request.POST.get('f_%d' %id, False):
                    rosetta_i18n_pofile[id].flags.append('fuzzy')
                    
            if file_change and rosetta_i18n_write:
                
                try:
                    rosetta_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')
                    rosetta_i18n_pofile.metadata['X-Translated-Using'] = u"django-rosetta %s" % rosetta.get_version(False)
                    rosetta_i18n_pofile.metadata['PO-Revision-Date'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M%z')
                except UnicodeDecodeError:
                    pass
                try:
                    rosetta_i18n_pofile.save()
                    rosetta_i18n_pofile.save_as_mofile(rosetta_i18n_fn.replace('.po','.mo'))
                    
                    # Try auto-reloading via the WSGI daemon mode reload mechanism
                    if  rosetta_settings.WSGI_AUTO_RELOAD and \
                        request.environ.has_key('mod_wsgi.process_group') and \
                        request.environ.get('mod_wsgi.process_group',None) and \
                        request.environ.has_key('SCRIPT_FILENAME') 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['rosetta_i18n_write'] = False
                
                request.session['rosetta_i18n_pofile']=rosetta_i18n_pofile
                
                # 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'))
                    
                    
                return HttpResponseRedirect(reverse('rosetta-home') + iri_to_uri(query_arg))
                
                
        rosetta_i18n_lang_name = _(request.session.get('rosetta_i18n_lang_name'))
        rosetta_i18n_lang_code = request.session.get('rosetta_i18n_lang_code')
                
        if 'query' in request.REQUEST and request.REQUEST.get('query','').strip():
            query=request.REQUEST.get('query').strip()
            rx=re.compile(re.escape(query), re.IGNORECASE)
            paginator = Paginator([e for e in rosetta_i18n_pofile if rx.search(smart_unicode(e.msgstr)+smart_unicode(e.msgid)+u''.join([o[0] for o in e.occurrences]))], rosetta_settings.MESSAGES_PER_PAGE)
        else:
            if rosetta_i18n_filter == 'untranslated':
                paginator = Paginator(rosetta_i18n_pofile.untranslated_entries(), rosetta_settings.MESSAGES_PER_PAGE)
            elif rosetta_i18n_filter == 'translated':
                paginator = Paginator(rosetta_i18n_pofile.translated_entries(), rosetta_settings.MESSAGES_PER_PAGE)
            elif rosetta_i18n_filter == 'fuzzy':
                paginator = Paginator(rosetta_i18n_pofile.fuzzy_entries(), rosetta_settings.MESSAGES_PER_PAGE)
            else:
                paginator = Paginator([e for e in rosetta_i18n_pofile if not e.obsolete], rosetta_settings.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
        messages = paginator.page(page).object_list
        if rosetta_settings.MAIN_LANGUAGE and rosetta_settings.MAIN_LANGUAGE != rosetta_i18n_lang_code:

            main_language = None
            for language in settings.LANGUAGES:
                if language[0] == rosetta_settings.MAIN_LANGUAGE:
                    main_language = _(language[1])
                    break

            fl = ("/%s/" % rosetta_settings.MAIN_LANGUAGE).join(rosetta_i18n_fn.split("/%s/" % rosetta_i18n_lang_code))
            po = pofile(fl)

            main_messages = []
            for message in messages:
                message.main_lang = po.find(message.msgid).msgstr
                
        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 = settings.ADMIN_MEDIA_PREFIX
        ENABLE_TRANSLATION_SUGGESTIONS = rosetta_settings.ENABLE_TRANSLATION_SUGGESTIONS
        
        return render_to_response('rosetta/pofile.html', locals(), context_instance=RequestContext(request))
        
        
    else:
        return list_languages(request)
Exemplo n.º 27
0
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 = rosetta.get_version(True)
    if 'rosetta_i18n_fn' in request.session:
        rosetta_i18n_fn=request.session.get('rosetta_i18n_fn')
        rosetta_i18n_app = get_app_name(rosetta_i18n_fn)
        rosetta_i18n_lang_code = request.session['rosetta_i18n_lang_code']
        rosetta_i18n_lang_bidi = rosetta_i18n_lang_code.split('-')[0] in settings.LANGUAGES_BIDI
        rosetta_i18n_write = request.session.get('rosetta_i18n_write', True)
        if rosetta_i18n_write:
            rosetta_i18n_pofile = pofile(rosetta_i18n_fn)
            for entry in rosetta_i18n_pofile:
                entry.md5hash = hashlib.md5(entry.msgid.encode("utf8")+entry.msgstr.encode("utf8")).hexdigest()

        else:
            rosetta_i18n_pofile = request.session.get('rosetta_i18n_pofile')

        
        if 'filter' in request.GET:
            if request.GET.get('filter') in ('untranslated', 'translated', 'fuzzy', 'all'):
                filter_ = request.GET.get('filter')
                request.session['rosetta_i18n_filter'] = filter_
                return HttpResponseRedirect(reverse('rosetta-home'))
        
        rosetta_i18n_filter = request.session.get('rosetta_i18n_filter', 'all')
        
        if '_next' in request.POST:
            rx = re.compile(r'^m_([0-9a-f]+)')
            rx_plural = re.compile(r'^m_([0-9a-f]+)_([0-9]+)')
            file_change = False
            for key, value in request.POST.items():
                md5hash = None
                plural_id = None

                if rx_plural.match(key):
                    md5hash = str(rx_plural.match(key).groups()[0])
                    # polib parses .po files into unicode strings, but
                    # doesn't bother to convert plural indexes to int,
                    # so we need unicode here.
                    plural_id = unicode(rx_plural.match(key).groups()[1])

                elif rx.match(key):
                    md5hash = str(rx.match(key).groups()[0])


                if md5hash is not None:
                    entry = rosetta_i18n_pofile.find(md5hash, 'md5hash')
                    # If someone did a makemessage, some entries might
                    # have been removed, so we need to check.
                    if entry:
                        if plural_id is not None:
                            plural_string = fix_nls(entry.msgstr_plural[plural_id], value)
                            entry.msgstr_plural[plural_id] = plural_string
                        else:
                            entry.msgstr = fix_nls(entry.msgid, value)

                        is_fuzzy = bool(request.POST.get('f_%s' % md5hash, False))

                        if 'fuzzy' in entry.flags and not is_fuzzy:
                            entry.flags.remove('fuzzy')
                        elif 'fuzzy' not in entry.flags and is_fuzzy:
                            entry.flags.append('fuzzy')
                        file_change = True
                    else:
                        request.session['rosetta_last_save_error'] = True
                        


            if file_change and rosetta_i18n_write:
                
                try:
                    rosetta_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')
                    rosetta_i18n_pofile.metadata['X-Translated-Using'] = u"django-rosetta %s" % rosetta.get_version(False)
                    rosetta_i18n_pofile.metadata['PO-Revision-Date'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M%z')
                except UnicodeDecodeError:
                    pass
                try:
                    rosetta_i18n_pofile.save()
                    rosetta_i18n_pofile.save_as_mofile(rosetta_i18n_fn.replace('.po','.mo'))
                    
                    # Try auto-reloading via the WSGI daemon mode reload mechanism
                    if  rosetta_settings.WSGI_AUTO_RELOAD and \
                        request.environ.has_key('mod_wsgi.process_group') and \
                        request.environ.get('mod_wsgi.process_group',None) and \
                        request.environ.has_key('SCRIPT_FILENAME') and \
                        int(request.environ.get('mod_wsgi.script_reloading', '0')):
                            try:
                                os.utime(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
                            # pretty easy right?
                            uwsgi.reload()
                        except:
                            # we may not be running under uwsgi :P
                            pass
                        
                except:
                    request.session['rosetta_i18n_write'] = False
                
                request.session['rosetta_i18n_pofile']=rosetta_i18n_pofile
                
                # 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'))
                    
                    
                return HttpResponseRedirect(reverse('rosetta-home') + iri_to_uri(query_arg))
                
                
        rosetta_i18n_lang_name = _(request.session.get('rosetta_i18n_lang_name'))
        rosetta_i18n_lang_code = request.session.get('rosetta_i18n_lang_code')
                
        if 'query' in request.REQUEST and request.REQUEST.get('query','').strip():
            query=request.REQUEST.get('query').strip()
            rx=re.compile(re.escape(query), re.IGNORECASE)
            paginator = Paginator([e for e in rosetta_i18n_pofile if not e.obsolete and rx.search(smart_unicode(e.msgstr)+smart_unicode(e.msgid)+u''.join([o[0] for o in e.occurrences]))], rosetta_settings.MESSAGES_PER_PAGE)
        else:
            if rosetta_i18n_filter == 'untranslated':
                paginator = Paginator(rosetta_i18n_pofile.untranslated_entries(), rosetta_settings.MESSAGES_PER_PAGE)
            elif rosetta_i18n_filter == 'translated':
                paginator = Paginator(rosetta_i18n_pofile.translated_entries(), rosetta_settings.MESSAGES_PER_PAGE)
            elif rosetta_i18n_filter == 'fuzzy':
                paginator = Paginator([e for e in rosetta_i18n_pofile.fuzzy_entries() if not e.obsolete], rosetta_settings.MESSAGES_PER_PAGE)
            else:
                paginator = Paginator([e for e in rosetta_i18n_pofile if not e.obsolete], rosetta_settings.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
        messages = paginator.page(page).object_list
        if rosetta_settings.MAIN_LANGUAGE and rosetta_settings.MAIN_LANGUAGE != rosetta_i18n_lang_code:

            main_language = None
            for language in settings.LANGUAGES:
                if language[0] == rosetta_settings.MAIN_LANGUAGE:
                    main_language = _(language[1])
                    break

            fl = ("/%s/" % rosetta_settings.MAIN_LANGUAGE).join(rosetta_i18n_fn.split("/%s/" % rosetta_i18n_lang_code))
            po = pofile(fl)

            main_messages = []
            for message in messages:
                message.main_lang = po.find(message.msgid).msgstr
                
        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 = settings.ADMIN_MEDIA_PREFIX
        ENABLE_TRANSLATION_SUGGESTIONS = rosetta_settings.ENABLE_TRANSLATION_SUGGESTIONS
        
        MESSAGES_SOURCE_LANGUAGE_NAME = rosetta_settings.MESSAGES_SOURCE_LANGUAGE_NAME
        MESSAGES_SOURCE_LANGUAGE_CODE = rosetta_settings.MESSAGES_SOURCE_LANGUAGE_CODE
        
        if 'rosetta_last_save_error' in request.session:
            del(request.session['rosetta_last_save_error'])
            rosetta_last_save_error = True
            
        
        return render_to_response('rosetta/pofile.html', locals(), context_instance=RequestContext(request))
        
        
    else:
        return list_languages(request)
Exemplo n.º 28
0
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 = rosetta.get_version(True)
    if 'rosetta_i18n_fn' in request.session:
        rosetta_i18n_fn = request.session.get('rosetta_i18n_fn')
        rosetta_i18n_app = get_app_name(rosetta_i18n_fn)
        rosetta_i18n_lang_code = request.session['rosetta_i18n_lang_code']
        rosetta_i18n_lang_bidi = rosetta_i18n_lang_code.split(
            '-')[0] in settings.LANGUAGES_BIDI
        rosetta_i18n_write = request.session.get('rosetta_i18n_write', True)
        if rosetta_i18n_write:
            rosetta_i18n_pofile = pofile(rosetta_i18n_fn)
            for entry in rosetta_i18n_pofile:
                entry.md5hash = hashlib.md5(
                    entry.msgid.encode("utf8") +
                    entry.msgstr.encode("utf8")).hexdigest()

        else:
            rosetta_i18n_pofile = request.session.get('rosetta_i18n_pofile')

        if 'filter' in request.GET:
            if request.GET.get('filter') in ('untranslated', 'translated',
                                             'fuzzy', 'all'):
                filter_ = request.GET.get('filter')
                request.session['rosetta_i18n_filter'] = filter_
                return HttpResponseRedirect(reverse('rosetta-home'))

        rosetta_i18n_filter = request.session.get('rosetta_i18n_filter', 'all')

        if '_next' in request.POST:
            rx = re.compile(r'^m_([0-9a-f]+)')
            rx_plural = re.compile(r'^m_([0-9a-f]+)_([0-9]+)')
            file_change = False
            for key, value in request.POST.items():
                md5hash = None
                plural_id = None

                if rx_plural.match(key):
                    md5hash = str(rx_plural.match(key).groups()[0])
                    # polib parses .po files into unicode strings, but
                    # doesn't bother to convert plural indexes to int,
                    # so we need unicode here.
                    plural_id = unicode(rx_plural.match(key).groups()[1])

                elif rx.match(key):
                    md5hash = str(rx.match(key).groups()[0])

                if md5hash is not None:
                    entry = rosetta_i18n_pofile.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:
                            plural_string = fix_nls(
                                entry.msgstr_plural[plural_id], value)
                            entry.msgstr_plural[plural_id] = plural_string
                        else:
                            entry.msgstr = fix_nls(entry.msgid, value)

                        is_fuzzy = bool(
                            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 != value or old_fuzzy != is_fuzzy:
                            entry_changed.send(
                                sender=entry,
                                user=request.user,
                                old_msgstr=old_msgstr,
                                old_fuzzy=old_fuzzy,
                                pofile=rosetta_i18n_fn,
                                language_code=rosetta_i18n_lang_code,
                            )

                    else:
                        request.session['rosetta_last_save_error'] = True

            if file_change and rosetta_i18n_write:

                try:
                    rosetta_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')
                    rosetta_i18n_pofile.metadata[
                        'X-Translated-Using'] = u"django-rosetta %s" % rosetta.get_version(
                            False)
                    rosetta_i18n_pofile.metadata[
                        'PO-Revision-Date'] = datetime.datetime.now().strftime(
                            '%Y-%m-%d %H:%M%z')
                except UnicodeDecodeError:
                    pass
                try:
                    rosetta_i18n_pofile.save()
                    rosetta_i18n_pofile.save_as_mofile(
                        rosetta_i18n_fn.replace('.po', '.mo'))

                    post_save.send(sender=None,
                                   language_code=rosetta_i18n_lang_code,
                                   request=request)

                    # Try auto-reloading via the WSGI daemon mode reload mechanism
                    if  rosetta_settings.WSGI_AUTO_RELOAD and \
                        request.environ.has_key('mod_wsgi.process_group') and \
                        request.environ.get('mod_wsgi.process_group',None) and \
                        request.environ.has_key('SCRIPT_FILENAME') and \
                        int(request.environ.get('mod_wsgi.script_reloading', '0')):
                        try:
                            os.utime(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
                            # pretty easy right?
                            uwsgi.reload()
                        except:
                            # we may not be running under uwsgi :P
                            pass

                except:
                    request.session['rosetta_i18n_write'] = False

                request.session['rosetta_i18n_pofile'] = rosetta_i18n_pofile

                # 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'))

                return HttpResponseRedirect(
                    reverse('rosetta-home') + iri_to_uri(query_arg))

        rosetta_i18n_lang_name = _(
            request.session.get('rosetta_i18n_lang_name'))
        rosetta_i18n_lang_code = request.session.get('rosetta_i18n_lang_code')

        if 'query' in request.REQUEST and request.REQUEST.get('query',
                                                              '').strip():
            query = request.REQUEST.get('query').strip()
            rx = re.compile(re.escape(query), re.IGNORECASE)
            paginator = Paginator([
                e for e in rosetta_i18n_pofile if not e.obsolete and rx.search(
                    smart_unicode(e.msgstr) + smart_unicode(e.msgid) +
                    u''.join([o[0] for o in e.occurrences]))
            ], rosetta_settings.MESSAGES_PER_PAGE)
        else:
            if rosetta_i18n_filter == 'untranslated':
                paginator = Paginator(
                    rosetta_i18n_pofile.untranslated_entries(),
                    rosetta_settings.MESSAGES_PER_PAGE)
            elif rosetta_i18n_filter == 'translated':
                paginator = Paginator(rosetta_i18n_pofile.translated_entries(),
                                      rosetta_settings.MESSAGES_PER_PAGE)
            elif rosetta_i18n_filter == 'fuzzy':
                paginator = Paginator([
                    e for e in rosetta_i18n_pofile.fuzzy_entries()
                    if not e.obsolete
                ], rosetta_settings.MESSAGES_PER_PAGE)
            else:
                paginator = Paginator(
                    [e for e in rosetta_i18n_pofile if not e.obsolete],
                    rosetta_settings.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
        messages = paginator.page(page).object_list
        if rosetta_settings.MAIN_LANGUAGE and rosetta_settings.MAIN_LANGUAGE != rosetta_i18n_lang_code:

            main_language = None
            for language in settings.LANGUAGES:
                if language[0] == rosetta_settings.MAIN_LANGUAGE:
                    main_language = _(language[1])
                    break

            fl = ("/%s/" % rosetta_settings.MAIN_LANGUAGE).join(
                rosetta_i18n_fn.split("/%s/" % rosetta_i18n_lang_code))
            po = pofile(fl)

            main_messages = []
            for message in messages:
                message.main_lang = po.find(message.msgid).msgstr

        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 = settings.ADMIN_MEDIA_PREFIX
        ENABLE_TRANSLATION_SUGGESTIONS = rosetta_settings.ENABLE_TRANSLATION_SUGGESTIONS

        MESSAGES_SOURCE_LANGUAGE_NAME = rosetta_settings.MESSAGES_SOURCE_LANGUAGE_NAME
        MESSAGES_SOURCE_LANGUAGE_CODE = rosetta_settings.MESSAGES_SOURCE_LANGUAGE_CODE

        if 'rosetta_last_save_error' in request.session:
            del (request.session['rosetta_last_save_error'])
            rosetta_last_save_error = True

        return render_to_response('rosetta/pofile.html',
                                  locals(),
                                  context_instance=RequestContext(request))

    else:
        return list_languages(request)
Exemplo n.º 29
0
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_

    storage = get_storage(request)
    query = ''
    if storage.has('rosetta_i18n_fn'):
        rosetta_i18n_fn = storage.get('rosetta_i18n_fn')
        rosetta_i18n_app = get_app_name(rosetta_i18n_fn)
        rosetta_i18n_lang_code = storage.get('rosetta_i18n_lang_code')
        rosetta_i18n_lang_bidi = rosetta_i18n_lang_code.split(
            '-')[0] in settings.LANGUAGES_BIDI
        rosetta_i18n_write = storage.get('rosetta_i18n_write', True)
        if rosetta_i18n_write:
            rosetta_i18n_pofile = pofile(
                rosetta_i18n_fn, wrapwidth=rosetta_settings.POFILE_WRAP_WIDTH)
            for entry in rosetta_i18n_pofile:
                entry.md5hash = hashlib.md5(
                    (six.text_type(entry.msgid) + six.text_type(entry.msgstr) +
                     six.text_type(entry.msgctxt
                                   or "")).encode('utf8')).hexdigest()

        else:
            rosetta_i18n_pofile = storage.get('rosetta_i18n_pofile')

        if 'filter' in request.GET:
            if request.GET.get('filter') in ('untranslated', 'translated',
                                             'fuzzy', 'all'):
                filter_ = request.GET.get('filter')
                storage.set('rosetta_i18n_filter', filter_)
                return HttpResponseRedirect(reverse('rosetta-home'))

        rosetta_i18n_filter = storage.get('rosetta_i18n_filter', 'all')

        if '_next' in request.POST:
            rx = re.compile(r'^m_([0-9a-f]+)')
            rx_plural = re.compile(r'^m_([0-9a-f]+)_([0-9]+)')
            file_change = False
            for key, value in request.POST.items():
                md5hash = None
                plural_id = None

                if rx_plural.match(key):
                    md5hash = str(rx_plural.match(key).groups()[0])
                    # 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(rx_plural.match(key).groups()[1])

                elif rx.match(key):
                    md5hash = str(rx.match(key).groups()[0])

                if md5hash is not None:
                    entry = rosetta_i18n_pofile.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:
                            #plural_string = fix_nls(entry.msgstr_plural[plural_id], value)
                            plural_string = fix_nls(entry.msgid_plural, value)
                            entry.msgstr_plural[plural_id] = plural_string
                        else:
                            entry.msgstr = fix_nls(entry.msgid, value)

                        is_fuzzy = bool(
                            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 != value or old_fuzzy != is_fuzzy:
                            entry_changed.send(
                                sender=entry,
                                user=request.user,
                                old_msgstr=old_msgstr,
                                old_fuzzy=old_fuzzy,
                                pofile=rosetta_i18n_fn,
                                language_code=rosetta_i18n_lang_code,
                            )

                    else:
                        storage.set('rosetta_last_save_error', True)

            if file_change and rosetta_i18n_write:
                try:
                    # Provide defaults in case authorization is not required.
                    request.user.first_name = getattr(request.user,
                                                      'first_name',
                                                      'Anonymous')
                    request.user.last_name = getattr(request.user, 'last_name',
                                                     'User')
                    request.user.email = getattr(request.user, 'email',
                                                 '*****@*****.**')

                    rosetta_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')
                    rosetta_i18n_pofile.metadata[
                        'X-Translated-Using'] = u"django-rosetta %s" % rosetta.get_version(
                            False)
                    rosetta_i18n_pofile.metadata[
                        'PO-Revision-Date'] = timestamp_with_timezone()
                except UnicodeDecodeError:
                    pass

                try:
                    rosetta_i18n_pofile.save()
                    po_filepath, ext = os.path.splitext(rosetta_i18n_fn)
                    save_as_mo_filepath = po_filepath + '.mo'
                    rosetta_i18n_pofile.save_as_mofile(save_as_mo_filepath)

                    post_save.send(sender=None,
                                   language_code=rosetta_i18n_lang_code,
                                   request=request)
                    # Try auto-reloading via the WSGI daemon mode reload mechanism
                    if rosetta_settings.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
                    # Try auto-reloading via uwsgi daemon reload mechanism
                    if rosetta_settings.UWSGI_AUTO_RELOAD:
                        try:
                            import uwsgi
                            # pretty easy right?
                            uwsgi.reload()
                        except:
                            # we may not be running under uwsgi :P
                            pass

                except:
                    storage.set('rosetta_i18n_write', False)
                storage.set('rosetta_i18n_pofile', rosetta_i18n_pofile)

                # Retain query arguments
                query_arg = '?_next=1'
                if 'query' in request.GET or 'query' in request.POST:
                    query_arg += '&query=%s' % request.REQUEST.get('query')
                if 'page' in request.GET:
                    query_arg += '&page=%d&_next=1' % int(
                        request.GET.get('page'))
                return HttpResponseRedirect(
                    reverse('rosetta-home') + iri_to_uri(query_arg))
        rosetta_i18n_lang_code = storage.get('rosetta_i18n_lang_code')

        if 'query' in request.REQUEST and request.REQUEST.get('query',
                                                              '').strip():
            query = request.REQUEST.get('query').strip()
            rx = re.compile(re.escape(query), re.IGNORECASE)
            paginator = Paginator([
                e for e in rosetta_i18n_pofile if not e.obsolete and rx.search(
                    six.text_type(e.msgstr) + six.text_type(e.msgid) +
                    u''.join([o[0] for o in e.occurrences]))
            ], rosetta_settings.MESSAGES_PER_PAGE)
        else:
            if rosetta_i18n_filter == 'untranslated':
                paginator = Paginator(
                    rosetta_i18n_pofile.untranslated_entries(),
                    rosetta_settings.MESSAGES_PER_PAGE)
            elif rosetta_i18n_filter == 'translated':
                paginator = Paginator(rosetta_i18n_pofile.translated_entries(),
                                      rosetta_settings.MESSAGES_PER_PAGE)
            elif rosetta_i18n_filter == 'fuzzy':
                paginator = Paginator([
                    e for e in rosetta_i18n_pofile.fuzzy_entries()
                    if not e.obsolete
                ], rosetta_settings.MESSAGES_PER_PAGE)
            else:
                paginator = Paginator(
                    [e for e in rosetta_i18n_pofile if not e.obsolete],
                    rosetta_settings.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 '_next' in request.GET or '_next' in request.POST:
            page += 1
            if page > paginator.num_pages:
                page = 1
            query_arg = '?page=%d' % page
            return HttpResponseRedirect(
                reverse('rosetta-home') + iri_to_uri(query_arg))

        rosetta_messages = paginator.page(page).object_list
        main_language = None
        if rosetta_settings.MAIN_LANGUAGE and rosetta_settings.MAIN_LANGUAGE != rosetta_i18n_lang_code:
            for language in settings.LANGUAGES:
                if language[0] == rosetta_settings.MAIN_LANGUAGE:
                    main_language = _(language[1])
                    break

            fl = ("/%s/" % rosetta_settings.MAIN_LANGUAGE).join(
                rosetta_i18n_fn.split("/%s/" % rosetta_i18n_lang_code))
            po = pofile(fl)

            for message in rosetta_messages:
                message.main_lang = po.find(message.msgid).msgstr

        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)
        try:
            ADMIN_MEDIA_PREFIX = settings.ADMIN_MEDIA_PREFIX
            ADMIN_IMAGE_DIR = ADMIN_MEDIA_PREFIX + 'img/admin/'
        except AttributeError:
            ADMIN_MEDIA_PREFIX = settings.STATIC_URL + 'admin/'
            ADMIN_IMAGE_DIR = ADMIN_MEDIA_PREFIX + 'img/'

        if storage.has('rosetta_last_save_error'):
            storage.delete('rosetta_last_save_error')
            rosetta_last_save_error = True
        else:
            rosetta_last_save_error = False

        return render_to_response(
            'rosetta/pofile.html',
            dict(version=rosetta.get_version(True),
                 ADMIN_MEDIA_PREFIX=ADMIN_MEDIA_PREFIX,
                 ADMIN_IMAGE_DIR=ADMIN_IMAGE_DIR,
                 rosetta_settings=rosetta_settings,
                 rosetta_i18n_lang_name=_(
                     storage.get('rosetta_i18n_lang_name')),
                 rosetta_i18n_lang_code=rosetta_i18n_lang_code,
                 rosetta_i18n_lang_bidi=rosetta_i18n_lang_bidi,
                 rosetta_last_save_error=rosetta_last_save_error,
                 rosetta_i18n_filter=rosetta_i18n_filter,
                 rosetta_i18n_write=rosetta_i18n_write,
                 rosetta_messages=rosetta_messages,
                 page_range=needs_pagination and page_range,
                 needs_pagination=needs_pagination,
                 main_language=main_language,
                 rosetta_i18n_app=rosetta_i18n_app,
                 page=page,
                 query=query,
                 paginator=paginator,
                 rosetta_i18n_pofile=rosetta_i18n_pofile),
            context_instance=RequestContext(request))
    else:
        return list_languages(request, do_session_warn=True)