From f597bf863ca11836cb647ee190e6268877683489 Mon Sep 17 00:00:00 2001 From: Sebastian Ludwig Date: Mon, 24 Aug 2015 09:08:55 +0200 Subject: [PATCH] Implemented references for keys. Closes #82. --- README.md | 7 ++++ lib/twine/stringsfile.rb | 42 +++++++++++++++++---- test/fixtures/strings-4-references.txt | 20 ++++++++++ test/fixtures/test-output-14-references.txt | 17 +++++++++ test/twine_test.rb | 8 ++++ 5 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 test/fixtures/strings-4-references.txt create mode 100644 test/fixtures/test-output-14-references.txt diff --git a/README.md b/README.md index 697c6ba..fb600c6 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,10 @@ Tags are used by Twine as a way to only work with a subset of your strings at an Whitepace in this file is mostly ignored. If you absolutely need to put spaces at the beginning or end of your translated string, you can wrap the entire string in a pair of `` ` `` characters. If your actual string needs to start *and* end with a grave accent, you can wrap it in another pair of `` ` `` characters. See the example, below. +### References + +If you want a key to inherit the values of another key, you can use a reference. Any property not specified for a key will be taken from the reference. + ### Example [[General]] @@ -57,6 +61,9 @@ Whitepace in this file is mostly ignored. If you absolutely need to put spaces a en = The network is currently unavailable. tags = app1 comment = An error describing when the device can not connect to the internet. + [dismiss_error] + ref = [yes] + en = Dismiss [[Escaping Example]] [list_item_separator] diff --git a/lib/twine/stringsfile.rb b/lib/twine/stringsfile.rb index 26987ab..b32ab2b 100644 --- a/lib/twine/stringsfile.rb +++ b/lib/twine/stringsfile.rb @@ -14,6 +14,7 @@ module Twine attr_accessor :comment attr_accessor :tags attr_reader :translations + attr_accessor :reference def initialize(key) @key = key @@ -22,13 +23,19 @@ module Twine @translations = {} end + def comment + @comment || (reference.comment if reference) + end + def matches_tags?(tags, include_untagged) - if tags == nil || tags.length == 0 + if tags == nil || tags.empty? # The user did not specify any tags. Everything passes. return true - elsif @tags == nil || @tags.length == 0 + elsif @tags == nil # This row has no tags. - return (include_untagged) ? true : false + return reference ? reference.matches_tags?(tags, include_untagged) : include_untagged + elsif @tags.empty? + return include_untagged else tags.each do |tag| if @tags.include? tag @@ -42,6 +49,9 @@ module Twine def translated_string_for_lang(lang, default_lang=nil) translation = [lang].flatten.map { |l| @translations[l] }.first + + translation = reference.translated_string_for_lang(lang, default_lang) if translation.nil? && reference + return translation if translation # TODO: get rid of all this and the default_lang parameter once all formatters are converted to the new style @@ -63,6 +73,15 @@ module Twine attr_reader :strings_map attr_reader :language_codes + private + + def match_key(text) + match = /^\[(.+)\]$/.match(text) + return match[1] if match + end + + public + def initialize @sections = [] @strings_map = {} @@ -95,9 +114,9 @@ module Twine parsed = true end elsif line.length > 2 && line[0, 1] == '[' - match = /^\[(.+)\]$/.match(line) - if match - current_row = StringsRow.new(match[1]) + key = match_key(line) + if key + current_row = StringsRow.new(key) @strings_map[current_row.key] = current_row if !current_section current_section = StringsSection.new('') @@ -116,10 +135,13 @@ module Twine end case key - when "comment" + when 'comment' current_row.comment = value when 'tags' current_row.tags = value.split(',') + when 'ref' + key = match_key(value) + current_row.reference = key if key else if !@language_codes.include? key add_language_code(key) @@ -135,6 +157,12 @@ module Twine end end end + + # resolve_references + @strings_map.each do |key, row| + next unless row.reference + row.reference = @strings_map[row.reference] + end end def write(path) diff --git a/test/fixtures/strings-4-references.txt b/test/fixtures/strings-4-references.txt new file mode 100644 index 0000000..93fff57 --- /dev/null +++ b/test/fixtures/strings-4-references.txt @@ -0,0 +1,20 @@ +[[General]] + [key1] + en = key1-english + tags = tag1 + comment = This is a comment + es = key1-spanish + fr = key1-french + +[[My Strings]] + [key2] + ref = [key1] + [key3] + ref = [key1] + fr = key3-french + [key4] + ref = [key1] + comment = Different comment + [key5] + ref = [key1] + tags = tag2 diff --git a/test/fixtures/test-output-14-references.txt b/test/fixtures/test-output-14-references.txt new file mode 100644 index 0000000..73e6ded --- /dev/null +++ b/test/fixtures/test-output-14-references.txt @@ -0,0 +1,17 @@ + + + + + + + + key1-french + + + + key1-french + + key3-french + + key1-french + diff --git a/test/twine_test.rb b/test/twine_test.rb index 76c6a9c..5522644 100644 --- a/test/twine_test.rb +++ b/test/twine_test.rb @@ -147,4 +147,12 @@ class TwineTest < Test::Unit::TestCase assert_equal(ERB.new(File.read('test/fixtures/test-output-14.txt')).result, File.read(output_path)) end end + + def test_generate_string_file_14_references + Dir.mktmpdir do |dir| + output_path = File.join(dir, 'references.xml') + Twine::Runner.run(%W(generate-string-file test/fixtures/strings-4-references.txt #{output_path} -l fr -t tag1)) + assert_equal(ERB.new(File.read('test/fixtures/test-output-14-references.txt')).result, File.read(output_path)) + end + end end