Commit bc39e36d authored by Michal Čihař's avatar Michal Čihař

Added XLIFF and Gettext PO export for all translations.

Fixes #810
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent 3947ceaa
...@@ -43,6 +43,7 @@ Released on ? 2015. ...@@ -43,6 +43,7 @@ Released on ? 2015.
* Added option to import components with duplicate name to import_project. * Added option to import components with duplicate name to import_project.
* Improved support for translating PHP files * Improved support for translating PHP files
* Added XLIFF export for dictionary. * Added XLIFF export for dictionary.
* Added XLIFF and Gettext PO export for all translations.
weblate 2.4 weblate 2.4
----------- -----------
......
{% load i18n %} {% load i18n %}
<li><a href="{% url 'download_translation' project=object.subproject.project.slug subproject=object.subproject.slug lang=object.language.code %}" title="{% trans "Download for an offline translation." %}">{% trans "Download source file" %}</a></li> <li><a href="{% url 'download_translation' project=object.subproject.project.slug subproject=object.subproject.slug lang=object.language.code %}" title="{% trans "Download for an offline translation." %}">{% trans "Download source file" %}</a></li>
<li><a href="{% url 'download_translation_format' project=object.subproject.project.slug subproject=object.subproject.slug lang=object.language.code format='xliff'%}" title="{% trans "Download for an offline translation." %}">{% trans "Download translation as XLIFF" %}</a></li>
<li><a href="{% url 'download_translation_format' project=object.subproject.project.slug subproject=object.subproject.slug lang=object.language.code format='po'%}" title="{% trans "Download for an offline translation." %}">{% trans "Download translation as Gettext PO" %}</a></li>
{% if object.supports_language_pack %} {% if object.supports_language_pack %}
<li><a href="{% url 'download_language_pack' project=object.subproject.project.slug subproject=object.subproject.slug lang=object.language.code %}" title="{% trans "Download for using within an application." %}">{% trans "Download compiled translation" %}</a></li> <li><a href="{% url 'download_language_pack' project=object.subproject.project.slug subproject=object.subproject.slug lang=object.language.code %}" title="{% trans "Download for using within an application." %}">{% trans "Download compiled translation" %}</a></li>
{% endif %} {% endif %}
......
...@@ -22,6 +22,7 @@ from __future__ import unicode_literals ...@@ -22,6 +22,7 @@ from __future__ import unicode_literals
from django.http import HttpResponse from django.http import HttpResponse
from translate.misc.multistring import multistring
from translate.storage.po import pofile from translate.storage.po import pofile
from translate.storage.xliff import xlifffile from translate.storage.xliff import xlifffile
from translate.storage.tbx import tbxfile from translate.storage.tbx import tbxfile
...@@ -70,6 +71,26 @@ class BaseExporter(object): ...@@ -70,6 +71,26 @@ class BaseExporter(object):
unit.target = word.target unit.target = word.target
self.storage.addunit(unit) self.storage.addunit(unit)
def add_unit(self, unit):
if unit.is_plural():
output = self.storage.UnitClass(
multistring(unit.get_source_plurals())
)
output.target = multistring(unit.get_target_plurals())
else:
output = self.storage.UnitClass(unit.source)
output.target = multistring(unit.target)
output.context = unit.context
for location in unit.location.split():
output.addlocation(unit.location)
output.addnote(unit.comment)
if hasattr(output, 'settypecomment'):
for flag in unit.flags.split(','):
output.settypecomment(flag)
if unit.fuzzy:
output.markfuzzy(True)
self.storage.addunit(output)
def get_response(self, filetemplate='{project}-{language}.{extension}'): def get_response(self, filetemplate='{project}-{language}.{extension}'):
filename = filetemplate.format( filename = filetemplate.format(
project=self.project.slug, project=self.project.slug,
...@@ -108,6 +129,7 @@ class PoExporter(BaseExporter): ...@@ -108,6 +129,7 @@ class PoExporter(BaseExporter):
project_id_version='%s (%s)' % ( project_id_version='%s (%s)' % (
self.language.name, self.project.name self.language.name, self.project.name
), ),
plural_forms= self.language.get_plural_form(),
language_team='%s <%s>' % ( language_team='%s <%s>' % (
self.language.name, self.language.name,
self.url, self.url,
......
...@@ -373,6 +373,35 @@ class ExportTest(ViewTestCase): ...@@ -373,6 +373,35 @@ class ExportTest(ViewTestCase):
'attachment; filename=test-test-cs.po' 'attachment; filename=test-test-cs.po'
) )
def export_format(self, fmt):
kwargs = {'format': fmt}
kwargs.update(self.kw_translation)
return self.client.get(
reverse(
'download_translation_format',
kwargs=kwargs
)
)
def test_export_po(self):
response = self.export_format('po')
self.assertContains(
response, 'Orangutan has %d bananas'
)
self.assertContains(
response, '/projects/test/test/cs/'
)
def test_export_xliff(self):
response = self.export_format('xliff')
self.assertContains(
response, 'Orangutan has %d banana'
)
def test_export_invalid(self):
response = self.export_format('invalid')
self.assertEquals(response.status_code, 404)
def test_language_pack(self): def test_language_pack(self):
response = self.client.get( response = self.client.get(
reverse( reverse(
......
...@@ -29,6 +29,8 @@ from django.contrib.auth.decorators import permission_required ...@@ -29,6 +29,8 @@ from django.contrib.auth.decorators import permission_required
from django.views.decorators.http import require_POST from django.views.decorators.http import require_POST
from django.http import Http404 from django.http import Http404
from weblate.trans.site import get_site_url
from weblate.trans.exporters import get_exporter
from weblate.trans.util import report_error from weblate.trans.util import report_error
from weblate.trans.forms import get_upload_form from weblate.trans.forms import get_upload_form
from weblate.trans.views.helper import get_translation, import_message from weblate.trans.views.helper import get_translation, import_message
...@@ -37,6 +39,29 @@ from weblate.trans.permissions import ( ...@@ -37,6 +39,29 @@ from weblate.trans.permissions import (
) )
def download_translation_format(request, project, subproject, lang, format):
obj = get_translation(request, project, subproject, lang)
try:
exporter = get_exporter(format)(
obj.subproject.project,
obj.language,
get_site_url(obj.get_absolute_url())
)
except KeyError:
raise Http404('File format not supported')
for unit in obj.unit_set.iterator():
exporter.add_unit(unit)
# Save to response
return exporter.get_response(
'{{project}}-{0}-{{language}}.{{extension}}'.format(
subproject
)
)
def download_translation(request, project, subproject, lang): def download_translation(request, project, subproject, lang):
obj = get_translation(request, project, subproject, lang) obj = get_translation(request, project, subproject, lang)
......
...@@ -189,6 +189,11 @@ urlpatterns = [ ...@@ -189,6 +189,11 @@ urlpatterns = [
weblate.trans.views.files.download_translation, weblate.trans.views.files.download_translation,
name='download_translation', name='download_translation',
), ),
url(
r'^download/' + TRANSLATION + '(?P<format>[a-z]+)/$',
weblate.trans.views.files.download_translation_format,
name='download_translation_format',
),
url( url(
r'^language-pack/' + TRANSLATION + '$', r'^language-pack/' + TRANSLATION + '$',
weblate.trans.views.files.download_language_pack, weblate.trans.views.files.download_language_pack,
......
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