Refactored formattes to get rid of dynamic method dispatching.

This commit is contained in:
Sebastian Ludwig 2015-12-06 14:26:18 +01:00
parent aab8179f14
commit 17efb76019
3 changed files with 33 additions and 34 deletions

View file

@ -92,21 +92,25 @@ module Twine
def format_sections(strings, lang)
sections = strings.sections.map { |section| format_section(section, lang) }
sections.join("\n")
sections.compact.join("\n")
end
def format_section_header(section)
end
def should_include_row(row, lang)
row.translated_string_for_lang(lang)
end
def format_section(section, lang)
rows = section.rows.dup
rows = section.rows.select { |row| should_include_row(row, lang) }
return if rows.empty?
result = ""
unless rows.empty?
if section.name && section.name.length > 0
section_header = format_section_header(section)
result += "\n#{section_header}" if section_header
end
if section.name && section.name.length > 0
section_header = format_section_header(section)
result += "\n#{section_header}" if section_header
end
rows.map! { |row| format_row(row, lang) }
@ -115,16 +119,8 @@ module Twine
result += rows.join
end
def row_pattern
"%{comment}%{key_value}"
end
def format_row(row, lang)
return nil unless row.translated_string_for_lang(lang)
result = row_pattern.scan(/%\{([a-z_]+)\}/).flatten
result.map! { |element| send("format_#{element}".to_sym, row, lang) }
result.flatten.join
[format_comment(row, lang), format_key_value(row, lang)].compact.join
end
def format_comment(row, lang)

View file

@ -92,7 +92,9 @@ module Twine
def format_file(strings, lang)
@default_lang = strings.language_codes[0]
super
result = super
@default_lang = nil
result
end
def format_header(lang)
@ -103,11 +105,11 @@ module Twine
"#--------- #{section.name} ---------#\n"
end
def row_pattern
"%{comment}%{base_translation}%{key_value}"
def format_row(row, lang)
[format_comment(row, lang), format_base_translation(row), format_key_value(row, lang)].compact.join
end
def format_base_translation(row, lang)
def format_base_translation(row)
base_translation = row.translations[@default_lang]
"# base translation: \"#{base_translation}\"\n" if base_translation
end

View file

@ -62,7 +62,9 @@ module Twine
def format_file(strings, lang)
@default_lang = strings.language_codes[0]
super
result = super
@default_lang = nil
result
end
def format_header(lang)
@ -73,30 +75,29 @@ module Twine
"# SECTION: #{section.name}"
end
def row_pattern
"%{comment}%{key}%{base_translation}%{value}"
end
def format_row(row, lang)
return nil unless row.translated_string_for_lang(@default_lang)
super
def should_include_row(row, lang)
super and row.translated_string_for_lang(@default_lang)
end
def format_comment(row, lang)
"#. \"#{escape_quotes(row.comment)}\"\n" if row.comment
end
def format_key(row, lang)
"msgctxt \"#{row.key.dup}\"\n"
def format_key_value(row, lang)
value = row.translated_string_for_lang(lang)
[format_key(row.key.dup), format_base_translation(row), format_value(value.dup)].compact.join
end
def format_base_translation(row, lang)
def format_key(key)
"msgctxt \"#{key}\"\n"
end
def format_base_translation(row)
"msgid \"#{row.translations[@default_lang]}\"\n"
end
def format_value(row, lang)
"msgstr \"#{row.translated_string_for_lang(lang)}\"\n"
def format_value(value)
"msgstr \"#{value}\"\n"
end
end
end