Implemented references for keys. Closes #82.

This commit is contained in:
Sebastian Ludwig 2015-08-24 09:08:55 +02:00
parent d7e3145417
commit f597bf863c
5 changed files with 87 additions and 7 deletions

View file

@ -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]

View file

@ -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)

20
test/fixtures/strings-4-references.txt vendored Normal file
View file

@ -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

View file

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Android Strings File -->
<!-- Generated by Twine <%= Twine::VERSION %> -->
<!-- Language: fr -->
<resources>
<!-- SECTION: General -->
<!-- This is a comment -->
<string name="key1">key1-french</string>
<!-- SECTION: My Strings -->
<!-- This is a comment -->
<string name="key2">key1-french</string>
<!-- This is a comment -->
<string name="key3">key3-french</string>
<!-- Different comment -->
<string name="key4">key1-french</string>
</resources>

View file

@ -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