Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSV formatter #188

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -94,6 +94,7 @@ Twine currently supports the following output formats:
* [Django PO Files][djangopo] (format: django)
* [Tizen String Resources][tizen] (format: tizen)
* [Flash/Flex Properties][flash] (format: flash)
* [Comma-separated values (CSV)][csv] (format: csv)

If you would like to enable Twine to create localization files in another format, read the wiki page on how to create an appropriate formatter.

Expand Down Expand Up @@ -229,4 +230,5 @@ Many thanks to all of the contributors to the Twine project, including:
[djangopo]: https://docs.djangoproject.com/en/dev/topics/i18n/translation/
[tizen]: https://developer.tizen.org/documentation/articles/localization
[flash]: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/resources/IResourceManager.html#getString()
[csv]: http://en.wikipedia.org/wiki/Comma-separated_values
[printf]: https://en.wikipedia.org/wiki/Printf_format_string
1 change: 1 addition & 0 deletions lib/twine.rb
Expand Up @@ -31,6 +31,7 @@ class Error < StandardError
require 'twine/formatters/abstract'
require 'twine/formatters/android'
require 'twine/formatters/apple'
require 'twine/formatters/csv'
require 'twine/formatters/django'
require 'twine/formatters/flash'
require 'twine/formatters/gettext'
Expand Down
64 changes: 64 additions & 0 deletions lib/twine/formatters/csv.rb
@@ -0,0 +1,64 @@
require 'csv'

module Twine
module Formatters
class CSV < Abstract
def format_name
'CSV'
end

def extension
'.csv'
end

def default_file_name
'strings.csv'
end

def determine_language_given_path(path)
path_arr = path.split(File::SEPARATOR)
path_arr.each do |segment|
match = /-(.+)\.csv$/.match(segment)
return match[1] if match
end

return
end

def read(io, lang)
while line = io.gets
fields = ::CSV.parse_line(line)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't this also add the header as translation?


key = fields[0]
value = fields[1]
comment = fields[2]

# checks for nil, empty and whitespace
if key =~ /\S/ and value =~ /\S/
set_translation_for_key(key, lang, value)
set_comment_for_key(key, comment) if comment =~ /\S/
end
end
end

def format_header(lang)
'Key,Value,Comment'
end

def format_section(section, lang)
# removes empty lines
super.gsub(/^$\n/, '')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's important to not have empty lines in the CSV, we should add a unit test for this

end

def format_definition(definition, lang)
key = definition.key
value = definition.translation_for_lang(lang)
comment = definition.comment

::CSV.generate_line([key, value, comment], row_sep: '')
end
end
end
end

Twine::Formatters.formatters << Twine::Formatters::CSV.new
5 changes: 5 additions & 0 deletions test/fixtures/formatter_csv.csv
@@ -0,0 +1,5 @@
Key,Value,Comment
key1,value1-english,comment key1
key2,value2-english,
key3,value3-english,
key4,value4-english,comment key4
19 changes: 19 additions & 0 deletions test/test_formatters.rb
Expand Up @@ -229,6 +229,25 @@ def test_format_value_with_trailing_space
end
end

class TestCSVFormatter < FormatterTest

def setup
super Twine::Formatters::CSV
end

def test_read_format
@formatter.read content_io('formatter_csv.csv'), 'en'

assert_translations_read_correctly
end

def test_format_file
formatter = Twine::Formatters::CSV.new
formatter.twine_file = @twine_file
assert_equal content('formatter_csv.csv'), formatter.format_file('en')
end
end

class TestJQueryFormatter < FormatterTest

def setup
Expand Down