How to use the weblate.lang.models.Language.objects.get function in weblate

To help you get started, we’ve selected a few weblate examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github WeblateOrg / weblate / weblate / lang / tests.py View on Github external
def test_edit_plural(self):
        language = Language.objects.get(code='cs')
        self.user.is_superuser = True
        self.user.save()
        response = self.client.post(
            reverse('edit-plural', kwargs={'pk': language.plural.pk}),
            {'number': '2', 'equation': 'n != 1'},
        )
        self.assertRedirects(
            response, reverse('show_language', kwargs={'lang': 'cs'}) + '#information'
        )
github WeblateOrg / weblate / weblate / trans / models / change.py View on Github external
if self.action == self.ACTION_ACCESS_EDIT:
            for number, name in Project.ACCESS_CHOICES:
                if number == self.details['access_control']:
                    return name
            return 'Unknonwn {}'.format(self.details['access_control'])
        elif self.action in user_actions:
            if 'group' in self.details:
                return '{username} ({group})'.format(**self.details)
            return self.details['username']
        elif self.action in (
            self.ACTION_ADDED_LANGUAGE,
            self.ACTION_REQUESTED_LANGUAGE,
        ):  # noqa: E501
            try:
                return Language.objects.get(code=self.details['language'])
            except Language.DoesNotExist:
                return self.details['language']
        elif self.action == self.ACTION_ALERT:
            try:
                return ALERTS[self.details['alert']].verbose
            except KeyError:
                return self.details['alert']
        elif self.action == self.ACTION_PARSE_ERROR:
            return '{filename}: {error_message}'.format(**self.details)
        elif self.action == self.ACTION_HOOK:
            return '{service_long_name}: {repo_url}, {branch}'.format(**self.details)
        elif self.action == self.ACTION_COMMENT and 'comment' in self.details:
            return render_markdown(self.details['comment'])

        return ''
github WeblateOrg / weblate / weblate / lang / admin.py View on Github external
def save_related(self, request, form, formsets, change):
        super(LanguageAdmin, self).save_related(request, form, formsets, change)
        lang = form.instance

        if lang.plural_set.exists():
            return

        # Automatically create plurals if language does not have one
        try:
            baselang = Language.objects.get(code=lang.base_code)
            baseplural = baselang.plural
            lang.plural_set.create(
                source=Plural.SOURCE_DEFAULT,
                number=baseplural.number,
                equation=baseplural.equation,
            )
        except (Language.DoesNotExist, IndexError):
            lang.plural_set.create(
                source=Plural.SOURCE_DEFAULT, number=2, equation='n != 1'
            )
github WeblateOrg / weblate / weblate / trans / views / changes.py View on Github external
def _get_queryset_language(self):
        """Filtering by language"""
        if self.translation is None and 'lang' in self.request.GET:
            try:
                self.language = Language.objects.get(
                    code=self.request.GET['lang']
                )
            except Language.DoesNotExist:
                messages.error(
                    self.request,
                    _('Failed to find matching language!')
                )
github WeblateOrg / weblate / weblate / api / views.py View on Github external
def translations(self, request, **kwargs):
        obj = self.get_object()

        if request.method == 'POST':
            if not request.user.has_perm('translation.add', obj):
                self.permission_denied(request, message='Can not create translation')

            if 'language_code' not in request.data:
                raise ParseError("Missing 'language_code' parameter")

            language_code = request.data['language_code']

            try:
                language = Language.objects.get(code=language_code)
            except Language.DoesNotExist:
                raise Http404("No language code '%s' found!" % language_code)

            obj.add_new_language(language, request)
            page = obj.translation_set.get(language=language)
            serializer = TranslationSerializer(
                page, context={'request': request}, remove_fields=('component',),
            )

            return Response(
                data={
                    'data': serializer.data,
                },
                status=status.HTTP_201_CREATED
            )
github WeblateOrg / weblate / weblate / trans / views / widgets.py View on Github external
def widgets(request, project):
    obj = get_project(request, project)

    # Parse possible language selection
    form = EngageForm(obj, request.GET)
    lang = None
    component = None
    if form.is_valid():
        if form.cleaned_data['lang']:
            lang = Language.objects.get(code=form.cleaned_data['lang']).code
        if form.cleaned_data['component']:
            component = Component.objects.get(
                slug=form.cleaned_data['component'],
                project=obj
            ).slug

    kwargs = {'project': obj.slug}
    if lang is not None:
        kwargs['lang'] = lang
    engage_url = get_site_url(reverse('engage', kwargs=kwargs))
    engage_url_track = '{0}?utm_source=widget'.format(engage_url)
    engage_link = mark_safe(
        '<a id="engage-link" href="{0}">{0}</a>'.format(escape(engage_url))
    )
    widget_base_url = get_site_url(
        reverse('widgets', kwargs={'project': obj.slug})
github WeblateOrg / weblate / weblate / trans / views / changes.py View on Github external
def _get_queryset_language(self):
        """Filtering by language"""
        if self.translation is None and self.request.GET.get('lang'):
            try:
                self.language = Language.objects.get(code=self.request.GET['lang'])
            except Language.DoesNotExist:
                messages.error(self.request, _('Failed to find matching language!'))
github WeblateOrg / weblate / weblate / memory / machine.py View on Github external
def convert_language(self, language):
        return Language.objects.get(code=language)
github WeblateOrg / weblate / weblate / trans / views.py View on Github external
def show_engage(request, project, lang = None):
    # Get project object
    obj = get_object_or_404(Project, slug = project)

    # Handle language parameter
    language = None
    if lang is not None:
        try:
            language = Language.objects.get(code = lang)
            django.utils.translation.activate(lang)
        except Language.DoesNotExist:
            pass

    return render_to_response('engage.html', RequestContext(request, {
        'object': obj,
        'project': obj.name,
        'languages': obj.get_language_count(),
        'total': obj.get_total(),
        'percent': obj.get_translated_percent(language),
        'url': obj.get_absolute_url(),
        'language_name': language,
    }))
github WeblateOrg / weblate / weblate / trans / templatetags / translations.py View on Github external
def language_name(code):
    '''
    Gets language name based on it's code.
    '''
    return Language.objects.get(code=code).__unicode__()