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

Factor out translate-toolkit based exporters

Issue #810
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent a77501dc
# -*- coding: utf-8 -*-
#
# Copyright © 2012 - 2016 Michal Čihař <michal@cihar.com>
#
# This file is part of Weblate <https://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/>.
#
"""Exporter using translate-toolkit"""
from __future__ import unicode_literals
from django.http import HttpResponse
from translate.storage.po import pofile
from translate.storage.xliff import xlifffile
from translate.storage.tbx import tbxfile
import weblate
from weblate.trans.formats import FileFormat
EXPORTERS = {}
def register_exporter(exporter):
"""Registerss exporter"""
EXPORTERS[exporter.name] = exporter
return exporter
def get_exporter(name):
"""Returns registered exporter"""
return EXPORTERS[name]
class BaseExporter(object):
content_type = 'text/plain'
extension = 'txt'
name = ''
has_lang = False
def __init__(self, project, language, url):
self.project = project
self.language = language
self.url = url
self.storage = self.get_storage()
self.storage.setsourcelanguage(project.source_language.code)
self.storage.settargetlanguage(language.code)
def get_storage(self):
raise NotImplementedError()
def add_dictionary(self, word):
"""Adds dictionary word"""
unit = self.storage.UnitClass(word.source)
if self.has_lang:
unit.settarget(word.target, self.language.code)
else:
unit.target = word.target
self.storage.addunit(unit)
def get_response(self, filetemplate='{project}-{language}.{extension}'):
filename = filetemplate.format(
project=self.project.slug,
language=self.language.code,
extension=self.extension
)
response = HttpResponse(
content_type='{0}; charset=utf-8'.format(self.content_type)
)
response['Content-Disposition'] = 'attachment; filename={0}'.format(
filename
)
# Save to response
response.write(FileFormat.serialize(self.storage))
return response
@register_exporter
class PoExporter(BaseExporter):
name = 'po'
content_type = 'text/x-po'
extension = 'po'
has_lang = False
def get_storage(self):
store = pofile()
# 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
),
language_team='%s <%s>' % (
self.language.name,
self.url,
)
)
return store
@register_exporter
class XliffExporter(BaseExporter):
name = 'xliff'
content_type = 'application/x-xliff+xml'
extension = 'xlf'
has_lang = True
def get_storage(self):
return xlifffile()
@register_exporter
class TBXExporter(BaseExporter):
name = 'tbx'
content_type = 'application/x-tbx'
extension = 'tbx'
has_lang = True
def get_storage(self):
return tbxfile()
......@@ -33,6 +33,7 @@ from django.core.urlresolvers import reverse
import six
from six.moves.urllib.parse import urlencode
from weblate.trans.exporters import get_exporter
from weblate.trans.models import Translation, Dictionary, Change
from weblate.lang.models import Language
from weblate.trans.formats import FileFormat
......@@ -183,68 +184,22 @@ def download_dictionary_ttkit(export_format, prj, lang, words):
'''
Translate-toolkit builder for dictionary downloads.
'''
# Use translate-toolkit for other formats
if export_format == 'po':
# Construct store
from translate.storage.po import pofile
store = pofile()
# Export parameters
content_type = 'text/x-po'
extension = 'po'
has_lang = False
# Set po file header
store.updateheader(
add=True,
language=lang.code,
x_generator='Weblate %s' % weblate.VERSION,
project_id_version='%s (%s)' % (lang.name, prj.name),
language_team='%s <%s>' % (
lang.name,
get_site_url(reverse(
'show_dictionary',
kwargs={'project': prj.slug, 'lang': lang.code}
)),
)
)
elif export_format == 'xliff':
# Construct store
from translate.storage.xliff import xlifffile
store = xlifffile()
# Export parameters
content_type = 'application/x-xliff+xml'
extension = 'xlf'
has_lang = True
else:
# Construct store
from translate.storage.tbx import tbxfile
store = tbxfile()
# Export parameters
content_type = 'application/x-tbx'
extension = 'tbx'
has_lang = True
# Setup response and headers
response = HttpResponse(content_type='%s; charset=utf-8' % content_type)
filename = 'glossary-%s-%s.%s' % (prj.slug, lang.code, extension)
response['Content-Disposition'] = 'attachment; filename=%s' % filename
exporter = get_exporter(export_format)(
prj, lang,
get_site_url(reverse(
'show_dictionary',
kwargs={'project': prj.slug, 'lang': lang.code}
))
)
# Add words
for word in words.iterator():
unit = store.UnitClass(word.source)
if has_lang:
unit.settarget(word.target, lang.code)
else:
unit.target = word.target
store.addunit(unit)
exporter.add_dictionary(word)
# Save to response
response.write(FileFormat.serialize(store))
return response
return exporter.get_response(
'glossary-{project}-{language}.{extension}'
)
def download_dictionary(request, project, lang):
......
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