Commit 2c813d52 authored by Michal Čihař's avatar Michal Čihař

Migrate language_pack to use exporters framework

This makes the code way easier to manage and reuses the same code for
all downloads.
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent a581a480
......@@ -4,6 +4,7 @@
<li><a href="{% url 'download_translation_format' project=object.subproject.project.slug subproject=object.subproject.slug lang=object.language.code fmt='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 fmt='xliff12'%}" title="{% trans "Download for an offline translation." %}">{% trans "Download translation as XLIFF 1.2" %}</a></li>
<li><a href="{% url 'download_translation_format' project=object.subproject.project.slug subproject=object.subproject.slug lang=object.language.code fmt='po'%}" title="{% trans "Download for an offline translation." %}">{% trans "Download translation as Gettext PO" %}</a></li>
<li><a href="{% url 'download_translation_format' project=object.subproject.project.slug subproject=object.subproject.slug lang=object.language.code fmt='mo'%}" title="{% trans "Download for an offline translation." %}">{% trans "Download translation as Gettext MO" %}</a></li>
{% 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>
{% endif %}
......
......@@ -24,6 +24,7 @@ from django.http import HttpResponse
from translate.misc.multistring import multistring
from translate.storage.po import pofile
from translate.storage.mo import mofile
from translate.storage.poxliff import PoXliffFile
from translate.storage.xliff import xlifffile
from translate.storage.tbx import tbxfile
......@@ -185,3 +186,35 @@ class TBXExporter(BaseExporter):
def get_storage(self):
return tbxfile()
@register_exporter
class MoExporter(BaseExporter):
name = 'mo'
content_type = 'application/x-gettext-catalog'
extension = 'mo'
has_lang = False
def get_storage(self):
store = mofile()
# Set po file header
store.updateheader(
add=True,
language=self.language.code,
x_generator='Weblate %s' % weblate.VERSION,
project_id_version='%s (%s)' % (
self.language.name, self.project.name
),
plural_forms=self.language.get_plural_form(),
language_team='%s <%s>' % (
self.language.name,
self.url,
)
)
return store
def add_unit(self, unit):
if not unit.translated:
return
super(MoExporter, self).add_unit(unit)
......@@ -453,6 +453,7 @@ class FileFormat(object):
unit_class = FileUnit
new_translation = None
autoload = ()
language_pack = None
@staticmethod
def serialize(store):
......@@ -699,13 +700,6 @@ class FileFormat(object):
else:
return self.store.Extensions[0]
@classmethod
def supports_language_pack(cls):
'''
Checks whether backend store supports generating language pack.
'''
return hasattr(cls, 'get_language_pack')
@classmethod
def is_valid(cls, store):
'''
......@@ -857,40 +851,7 @@ class PoFormat(FileFormat):
loader = pofile
monolingual = False
autoload = ('.po', '.pot')
def get_language_pack(self):
'''
Generates compiled messages file.
'''
outputfile = mofile()
for unit in self.store.units:
if not unit.istranslated() and not unit.isheader():
continue
outunit = mounit()
if unit.isheader():
outunit.source = ""
else:
outunit.source = unit.source
context = unit.getcontext()
if context:
outunit.msgctxt = [context]
outunit.target = unit.target
outputfile.addunit(outunit)
return self.serialize(outputfile)
def get_language_pack_meta(self):
'''
Returns language pack filename and mime type.
'''
basefile = os.path.splitext(
os.path.basename(self.storefile)
)[0]
return (
'%s.mo' % basefile,
'application/x-gettext-catalog'
)
language_pack = 'mo'
@classmethod
def supports_new_language(cls):
......
......@@ -402,7 +402,7 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
def supports_language_pack(self):
"""Checks whether we support language pack download."""
return self.subproject.file_format_cls.supports_language_pack()
return self.subproject.file_format_cls.language_pack is not None
@property
def store(self):
......
......@@ -417,5 +417,5 @@ class ExportTest(ViewTestCase):
)
self.assertEqual(
response['Content-Disposition'],
'attachment; filename=cs.mo'
'attachment; filename=test-test-cs.mo',
)
......@@ -52,22 +52,15 @@ def download_translation(request, project, subproject, lang):
def download_language_pack(request, project, subproject, lang):
obj = get_translation(request, project, subproject, lang)
if not obj.supports_language_pack():
raise Http404('Language pack download not supported')
filename, mime = obj.store.get_language_pack_meta()
# Create response
response = HttpResponse(
obj.store.get_language_pack(),
content_type=mime
return download_translation_file(
obj,
obj.subproject.file_format_cls.language_pack
)
# Fill in response headers
response['Content-Disposition'] = 'attachment; filename=%s' % filename
return response
@require_POST
@permission_required('trans.upload_translation')
......
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