From 84cf07d3534f27a9fb0c43dc812e8adaa6de172a Mon Sep 17 00:00:00 2001 From: Sebastian Ludwig Date: Sat, 28 Nov 2015 14:08:19 +0100 Subject: [PATCH] Adapted Django formatter to the modular style. --- lib/twine/formatters/django.rb | 82 +++++++++++++++---------------- test/fixtures/formatter_django.po | 7 +-- 2 files changed, 45 insertions(+), 44 deletions(-) diff --git a/lib/twine/formatters/django.rb b/lib/twine/formatters/django.rb index 981f4d7..1fbf8b8 100644 --- a/lib/twine/formatters/django.rb +++ b/lib/twine/formatters/django.rb @@ -90,53 +90,53 @@ module Twine end end - def write_file(path, lang) - default_lang = @strings.language_codes[0] - encoding = @options[:output_encoding] || 'UTF-8' - File.open(path, "w:#{encoding}") do |f| - f.puts "##\n # Django Strings File\n # Generated by Twine #{Twine::VERSION}\n # Language: #{lang}\n " - @strings.sections.each do |section| - printed_section = false - section.rows.each do |row| - if row.matches_tags?(@options[:tags], @options[:untagged]) - f.puts '' - if !printed_section - if section.name && section.name.length > 0 - f.print "#--------- #{section.name} ---------#\n\n" - end - printed_section = true - end - - basetrans = row.translated_string_for_lang(default_lang) + def format_file(strings, lang) + @default_lang = strings.language_codes[0] + super + end - key = row.key - key = key.gsub('"', '\\\\"') + def format_header(lang) + "##\n # Django Strings File\n # Generated by Twine #{Twine::VERSION}\n # Language: #{lang}\n" + end - value = row.translated_string_for_lang(lang, default_lang) - if value - value = value.gsub('"', '\\\\"') + def format_section_header(section) + "#--------- #{section.name} ---------#\n" + end - comment = row.comment + def format_row(row, lang) + value = row.translated_string_for_lang(lang) - if comment - comment = comment.gsub('"', '\\\\"') - end + return nil unless value - if comment && comment.length > 0 - f.print "#. #{comment} \n" - end - - if basetrans && basetrans.length > 0 - f.print "# base translation: \"#{basetrans}\"\n" - end - - f.print "msgid \"#{key}\"\n" - f.print "msgstr \"#{value}\"\n" - end - end - end - end + result = "" + if row.comment + comment = format_comment(row.comment) + result += comment + "\n" if comment end + + result += "# base translation: \"#{row.translations[@default_lang]}\"\n" + result += key_value_pattern % { key: format_key(row.key.dup), value: format_value(value.dup) } + end + + def key_value_pattern + "msgid \"%{key}\"\n" + + "msgstr \"%{value}\"\n" + end + + def format_comment(comment) + "#. #{escape_quotes(comment)}" + end + + def format_key(key) + escape_quotes(key) + end + + def format_value(value) + escape_quotes(value) + end + + def escape_quotes(text) + text.gsub('"', '\\\\"') end end end diff --git a/test/fixtures/formatter_django.po b/test/fixtures/formatter_django.po index 81569ed..895f0bb 100644 --- a/test/fixtures/formatter_django.po +++ b/test/fixtures/formatter_django.po @@ -2,11 +2,11 @@ # Django Strings File # Generated by Twine 0.7.0 # Language: en - + #--------- Section 1 ---------# -#. comment key1 +#. comment key1 # base translation: "value1-english" msgid "key1" msgstr "value1-english" @@ -15,13 +15,14 @@ msgstr "value1-english" msgid "key2" msgstr "value2-english" + #--------- Section 2 ---------# # base translation: "value3-english" msgid "key3" msgstr "value3-english" -#. comment key4 +#. comment key4 # base translation: "value4-english" msgid "key4" msgstr "value4-english"