Commit 95cb8009 authored by Michal Čihař's avatar Michal Čihař

Added support for filtering languages to be managed by Weblate.

Fixes #786
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent d788cdcf
......@@ -240,6 +240,10 @@ Committer name
Committer email
Email of commiter used on Weblate commits, the author will be always the
real translator. On some VCS this might be not supported.
Language filter
Regullar expression which is used to filter translation when scanning for
file mask. This can be used to limit list of languages mnaged by Weblate
(eg. ``^(cs|de|es)$`` will include only those there languages.
.. _commit-message:
......
......@@ -27,6 +27,7 @@ Released on ? 2015.
* Improved dictionary matching for several languages.
* Improved layout of most of pages.
* Support for adding words to dictionary while translating.
* Added support for filtering languages to be managed by Weblate.
weblate 2.3
-----------
......
# -*- coding: utf-8 -*-
#
# Copyright © 2012 - 2015 Michal Čihař <michal@cihar.com>
#
# This file is part of Weblate <http://weblate.org/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from django.db.models.fields import CharField
from django.db.models import SubfieldBase
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext as _
import re
def validate_re(value):
try:
re.compile(value)
except re.error as error:
raise ValidationError(_('Failed to compile: {0}').format(error))
class RegexField(CharField):
default_validators = [validate_re]
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
import weblate.trans.fields
class Migration(migrations.Migration):
dependencies = [
('trans', '0039_remove_project_owner'),
]
operations = [
migrations.AddField(
model_name='subproject',
name='language_regex',
field=weblate.trans.fields.RegexField(default=b'^[^.]+$', help_text='Regullar expression which is used to filter translation when scanning for file mask.', max_length=200, verbose_name='Language filter'),
),
]
......@@ -34,6 +34,7 @@ import re
from weblate.trans.formats import FILE_FORMAT_CHOICES, FILE_FORMATS
from weblate.trans.mixins import PercentMixin, URLMixin, PathMixin
from weblate.trans.filelock import FileLock
from weblate.trans.fields import RegexField
from weblate.trans.util import (
is_repo_link, get_site_url, cleanup_repo_url, cleanup_path,
)
......@@ -389,6 +390,16 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
default='noreply@weblate.org'
)
language_regex = RegexField(
verbose_name=ugettext_lazy('Language filter'),
max_length=200,
default='^[^.]+$',
help_text=ugettext_lazy(
'Regullar expression which is used to filter '
'translation when scanning for file mask.'
),
)
objects = SubProjectManager()
is_lockable = True
......@@ -939,11 +950,15 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
translations = set()
languages = set()
matches = self.get_mask_matches()
language_re = re.compile(self.language_regex)
for pos, path in enumerate(matches):
code = self.get_lang_code(path)
if langs is not None and code not in langs:
self.log_info('skipping %s', path)
continue
if not language_re.match(code):
self.log_info('skipping language %s', code)
continue
self.log_info(
'checking %s (%s) [%d/%d]',
......@@ -952,9 +967,6 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
pos + 1,
len(matches)
)
if '.' in code:
self.log_info('skipping language: {0}'.format(code))
continue
lang = Language.objects.auto_get_or_create(code=code)
if lang.code in languages:
self.log_error('duplicate language found: %s', lang.code)
......
......@@ -133,7 +133,7 @@ class RepoTestCase(TestCase):
return project
def _create_subproject(self, file_format, mask, template='',
new_base='', vcs='git'):
new_base='', vcs='git', **kwargs):
"""
Creates real test subproject.
"""
......@@ -161,7 +161,8 @@ class RepoTestCase(TestCase):
repoweb=REPOWEB_URL,
save_history=True,
new_base=new_base,
vcs=vcs
vcs=vcs,
**kwargs
)
def create_subproject(self):
......@@ -442,6 +443,14 @@ class SubProjectTest(RepoTestCase):
)
self.verify_subproject(project, 3, 'cs', 4, fail=True)
def test_create_filtered(self):
project = self._create_subproject(
'po',
'po/*.po',
language_regex='^cs$',
)
self.verify_subproject(project, 1, 'cs', 4)
def test_create_auto_pot(self):
project = self._create_subproject(
'auto',
......@@ -635,6 +644,15 @@ class SubProjectTest(RepoTestCase):
project.full_clean
)
def test_validation_languge_re(self):
subproject = self.create_subproject()
subproject.language_regex = '[-'
self.assertRaisesMessage(
ValidationError,
'Failed to compile: unexpected end of regular expression',
subproject.full_clean
)
def test_validation_newlang(self):
subproject = self.create_subproject()
subproject.new_base = 'po/project.pot'
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment