예제 #1
0
    def render(self, context):
        if "user" in context:
            user = context["user"]
        elif "request" in context:
            user = getattr(context.get("request"), "user", None)
        else:
            user = None
        if not (user and get_user_can_translate(user)):
            self.filter_expression.var.translate = not self.noop
            output = self.filter_expression.resolve(context)
            return _render_value_in_context(output, context)

        if getattr(self.filter_expression.var, "literal"):
            msgid = self.filter_expression.var.literal
        else:
            msgid = self.filter_expression.resolve(context)
        cat = copy.copy(catalog())
        cat.add_fallback(NotTranslated)
        styles = ["translatable"]
        try:
            if sys.version_info[0] == 2:
                msgstr = cat.ugettext(msgid)
            else:
                msgstr = cat.gettext(msgid)
        except (ValueError, AttributeError):
            styles.append("untranslated")
            msgstr = msgid
        return render_to_string(
            "inlinetrans/inline_trans.html", {"msgid": msgid, "styles": " ".join(styles), "value": msgstr}
        )
예제 #2
0
    def render(self, context):
        if 'user' in context:
            user = context['user']
        elif 'request' in context:
            user = getattr(context.get('request'), 'user', None)
        else:
            user = None
        if not (user and get_user_can_translate(user)):
            self.filter_expression.var.translate = not self.noop
            output = self.filter_expression.resolve(context)
            return _render_value_in_context(output, context)

        if getattr(self.filter_expression.var, 'literal'):
            msgid = self.filter_expression.var.literal
        else:
            msgid = self.filter_expression.resolve(context)
        cat = copy.copy(catalog())
        cat.add_fallback(NotTranslated)
        styles = ['translatable']
        try:
            if sys.version_info[0] == 2:
                msgstr = cat.ugettext(msgid)
            else:
                msgstr = cat.gettext(msgid)
        except (ValueError, AttributeError):
            styles.append("untranslated")
            msgstr = msgid
        return render_to_string('inlinetrans/inline_trans.html', {
            'msgid': msgid,
            'styles': ' '.join(styles),
            'value': msgstr
        })
예제 #3
0
    def render(self, context):
        if 'user' in context:
            user = context['user']
        elif 'request' in context:
            user = getattr(context.get('request'), 'user', None)
        else:
            user = None
        if not (user and user.is_staff):
            self.filter_expression.var.translate = not self.noop
            output = self.filter_expression.resolve(context)
            return _render_value_in_context(output, context)

        if getattr(self.filter_expression.var, 'literal'):
            msgid = self.filter_expression.var.literal
        else:
            msgid = self.filter_expression.resolve(context)
        cat = copy.copy(catalog())
        cat.add_fallback(NotTranslated)
        styles = ['translatable']
        try:
            msgstr = cat.ugettext(msgid)
        except ValueError:
            styles.append("untranslated")
            msgstr = msgid
        return render_to_string('inlinetrans/inline_trans.html',
                                {'msgid': msgid,
                                 'styles': ' '.join(styles),
                                 'value': msgstr})
 def apply_dummy_translations(self):
     catalog_dict = trans_real.catalog()._catalog
     if hasattr(catalog_dict, '_catalogs'):
         catalog_dict = catalog_dict._catalogs[0]
     for term in self.TRANSLATION_TERMS:
         catalog_dict[term] = term[1:]
     yield
     for term in self.TRANSLATION_TERMS:
         del catalog_dict[term]
예제 #5
0
def try_set_language(lang):
    """Try to activate language"""

    try:
        django.utils.translation.activate(lang)
        # workaround for https://code.djangoproject.com/ticket/26050
        # pylint: disable=W0212
        if trans_real.catalog()._catalog is None:
            raise Exception('Invalid language!')
    except Exception:
        # Ignore failure on activating language
        django.utils.translation.activate('en')
예제 #6
0
파일: helper.py 프로젝트: saily/weblate
def try_set_language(lang):
    """Try to activate language"""

    try:
        django.utils.translation.activate(lang)
        # workaround for https://code.djangoproject.com/ticket/26050
        # pylint: disable=W0212
        if trans_real.catalog()._catalog is None:
            raise Exception('Invalid language!')
    except Exception:
        # Ignore failure on activating language
        django.utils.translation.activate('en')
    def render(self, context):
        if not ('user' in context and context['user'].is_staff):
            return super(TranshetteTranslateNode, self).render(context)

        msgid = self.value.resolve(context)
        cat = copy.copy(catalog())
        cat.add_fallback(NotTranslated)
        styles = ['translatable']
        try:
            msgstr = cat.ugettext(msgid)
        except ValueError:
            styles.append("untranslated")
            msgstr = msgid
        return render_to_string('transhette/transhette_trans.html',
                                {'msgid': msgid,
                                 'styles': ' '.join(styles),
                                 'value': msgstr})
예제 #8
0
def try_set_language(lang):
    '''
    Tries to activate language, returns matching Language object.
    '''

    try:
        django.utils.translation.activate(lang)
        # workaround for https://code.djangoproject.com/ticket/26050
        # pylint: disable=W0212
        if trans_real.catalog()._catalog is None:
            raise Exception('Invalid language!')
    except Exception:
        # Ignore failure on activating language
        django.utils.translation.activate('en')
    try:
        return Language.objects.get(code=lang)
    except Language.DoesNotExist:
        return None
예제 #9
0
def try_set_language(lang):
    '''
    Tries to activate language, returns matching Language object.
    '''

    try:
        django.utils.translation.activate(lang)
        # workaround for https://code.djangoproject.com/ticket/26050
        # pylint: disable=W0212
        if trans_real.catalog()._catalog is None:
            raise Exception('Invalid language!')
    except Exception:
        # Ignore failure on activating language
        django.utils.translation.activate('en')
    try:
        return Language.objects.get(code=lang)
    except Language.DoesNotExist:
        return None
    def render(self, context):
        if not ('user' in context and context['user'].is_staff):
            return super(TranshetteTranslateNode, self).render(context)

        msgid = self.value.resolve(context)
        cat = copy.copy(catalog())
        cat.add_fallback(NotTranslated)
        styles = ['translatable']
        try:
            msgstr = cat.ugettext(msgid)
        except ValueError:
            styles.append("untranslated")
            msgstr = msgid
        return render_to_string('transhette/transhette_trans.html', {
            'msgid': msgid,
            'styles': ' '.join(styles),
            'value': msgstr
        })
예제 #11
0
    def render(self, context):
        # get the user
        if 'user' in context:
            user = context['user']
        elif 'request' in context:
            user = getattr(context.get('request'), 'user', None)
        else:
            user = None

        # if the user is no translator just render the translated value
        user_is_translator = user and (user.groups.filter(name=app_settings.TRANSLATORS_GROUP).count() > 0)
        if not (user and user_is_translator):
            self.filter_expression.var.translate = not self.noop
            output = self.filter_expression.resolve(context)

            return _render_value_in_context(output, context)

        if getattr(self.filter_expression.var, 'literal'):
            msgid = self.filter_expression.var.literal
        else:
            msgid = self.filter_expression.resolve(context)

        cat = copy.copy(catalog())
        cat.add_fallback(NotTranslated)
        styles = ['translatable']

        try:
            msgstr = cat.ugettext(msgid)

        except ValueError:
            styles.append("untranslated")
            msgstr = msgid

        return render_to_string('inlinetrans/inline_trans.html',{
            'msgid': msgid,
            'styles': ' '.join(styles),
            'value': msgstr
        })
예제 #12
0
    def render(self, context):

        if 'user' in context:
            user = context['user']
        elif 'request' in context:
            user = getattr(context.get('request'), 'user', None)
        else:
            user = None
        if not (user and user.is_staff):
            return super(InlineTranslateNode, self).render(context)

        msgid = self.value.resolve(context)
        cat = copy.copy(catalog())
        cat.add_fallback(NotTranslated)
        styles = ['translatable']
        try:
            msgstr = cat.ugettext(msgid)
        except ValueError:
            styles.append("untranslated")
            msgstr = msgid
        return render_to_string('inlinetrans/inline_trans.html',
                                {'msgid': msgid,
                                 'styles': ' '.join(styles),
                                 'value': msgstr})