Commit 0010a54a authored by Michal Čihař's avatar Michal Čihař

Add support for CSV file format

Issue #644
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent f34e8a38
......@@ -28,6 +28,7 @@ Released on ? 2015.
* Improved layout of most of pages.
* Support for adding words to dictionary while translating.
* Added support for filtering languages to be managed by Weblate.
* Added support for translating and importing CSV files.
weblate 2.3
-----------
......
......@@ -310,6 +310,23 @@ Example file:
You need translate-toolkit 1.13.0 or newer to include support for this format.
CSV files
---------
.. index::
pair: CSV; file format
pair: Comma separated values; file format
.. versionadded:: 2.4
CSV files can contain simple list of source and translation. Weblate supports
following files:
* Files with header defining fields (source, translation, location, ...)
* Files with two fileld - source and translation (in this order)
* Files with fields as defined by translate-toolkit: location, source,
target, id, fuzzy, context, translator_comments, developer_comments
Others
------
......
......@@ -36,6 +36,7 @@ import weblate
import subprocess
import os.path
import re
import csv
import traceback
import importlib
from StringIO import StringIO
......@@ -1059,6 +1060,61 @@ class JSONFormat(FileFormat):
return 'json'
@register_fileformat
class CSVFormat(FileFormat):
name = _('CSV file')
format_id = 'csv'
loader = ('csvl10n', 'csvfile')
unit_class = JSONUnit
autoload = ('.csv',)
@property
def mimetype(self):
'''
Returns most common mime type for format.
'''
return 'text/csv'
@property
def extension(self):
'''
Returns most common file extension for format.
'''
return 'csv'
@classmethod
def parse_store(cls, storefile):
"""
Parses the store.
"""
storeclass = cls.get_class()
# Read content for fixups
content = storefile.read()
storefile.seek(0)
# Parse file
store = storeclass.parsefile(storefile)
# Did headers detection work?
if store.fieldnames != ['location', 'source', 'target']:
return store
fileobj = StringIOMode(storefile.name, content)
# Try reading header
reader = csv.reader(fileobj, store.dialect)
header = reader.next()
# We seem to have match
if len(header) != 2:
fileobj.close()
return store
fileobj.seek(0)
return storeclass(fileobj, ['source', 'target'])
FILE_FORMAT_CHOICES = [
(fmt, FILE_FORMATS[fmt].name) for fmt in sorted(FILE_FORMATS)
]
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