Commit 1ff72bf6 authored by Michal Čihař's avatar Michal Čihař

Properly implement file upload for template based translations

Fixes #504
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent 5a0f5c46
......@@ -41,7 +41,7 @@ from weblate.trans.formats import AutoFormat
from weblate.trans.checks import CHECKS
from weblate.trans.models.project import Project
from weblate.trans.util import (
get_site_url, sleep_while_git_locked, translation_percent
get_site_url, sleep_while_git_locked, translation_percent, split_plural,
)
from weblate.accounts.avatar import get_user_display
from weblate.trans.mixins import URLMixin, PercentMixin
......@@ -1119,6 +1119,31 @@ class Translation(models.Model, URLMixin, PercentMixin):
return result
def merge_translations(self, request, author, store2, overwrite,
add_fuzzy):
"""
Merges translation unit wise, needed for template based translations to
add new strings.
"""
for unit2 in store2.all_units():
# No translated -> skip
if not unit2.is_translated() or unit2.unit.isheader():
continue
try:
unit = self.unit_set.get(
source=unit2.get_source(),
context=unit2.get_context(),
)
except Unit.DoesNotExist:
continue
unit.translate(
request,
split_plural(unit2.get_target()),
add_fuzzy or unit2.is_fuzzy()
)
def merge_store(self, request, author, store2, overwrite, merge_header,
add_fuzzy):
'''
......@@ -1241,6 +1266,17 @@ class Translation(models.Model, URLMixin, PercentMixin):
if method in ('', 'fuzzy'):
# Do actual merge
if self.subproject.has_template():
# Merge on units level
self.merge_translations(
request,
author,
store,
overwrite,
(method == 'fuzzy')
)
else:
# Merge on file level
for translation in translations:
ret |= translation.merge_store(
request,
......
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<resources>
<string name="hello">Nazdar světe!\n</string>
<string name="orangutan">Orangutan má %d banány.\n</string>
</resources>
......@@ -28,6 +28,7 @@ from weblate.trans.tests.test_util import get_test_file
TEST_PO = get_test_file('cs.po')
TEST_MO = get_test_file('cs.mo')
TEST_ANDROID = get_test_file('strings-cs.xml')
TRANSLATION_OURS = u'Nazdar světe!\n'
TRANSLATION_PO = u'Ahoj světe!\n'
......@@ -183,6 +184,26 @@ class ImportMoPoTest(ImportTest):
return self.create_po()
class AndroidImportTest(ViewTestCase):
def create_subproject(self):
return self.create_android()
def test_import(self):
with open(TEST_ANDROID) as handle:
self.client.post(
reverse(
'upload_translation',
kwargs=self.kw_translation
),
{'file': handle}
)
# Verify stats
translation = self.get_translation()
self.assertEqual(translation.translated, 2)
self.assertEqual(translation.fuzzy, 0)
self.assertEqual(translation.total, 4)
class ExportTest(ViewTestCase):
'''
Testing of file export.
......
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