Class: Twine::StringsFile
- Inherits:
-
Object
- Object
- Twine::StringsFile
- Defined in:
- lib/twine/stringsfile.rb
Instance Attribute Summary collapse
-
#language_codes ⇒ Object
readonly
Returns the value of attribute language_codes.
-
#sections ⇒ Object
readonly
Returns the value of attribute sections.
-
#strings_map ⇒ Object
readonly
Returns the value of attribute strings_map.
Instance Method Summary collapse
- #add_language_code(code) ⇒ Object
-
#initialize ⇒ StringsFile
constructor
A new instance of StringsFile.
- #read(path) ⇒ Object
- #set_developer_language_code(code) ⇒ Object
- #write(path) ⇒ Object
Constructor Details
#initialize ⇒ StringsFile
Returns a new instance of StringsFile.
74 75 76 77 78 |
# File 'lib/twine/stringsfile.rb', line 74 def initialize @sections = [] @strings_map = {} @language_codes = [] end |
Instance Attribute Details
#language_codes ⇒ Object (readonly)
Returns the value of attribute language_codes.
63 64 65 |
# File 'lib/twine/stringsfile.rb', line 63 def language_codes @language_codes end |
#sections ⇒ Object (readonly)
Returns the value of attribute sections.
61 62 63 |
# File 'lib/twine/stringsfile.rb', line 61 def sections @sections end |
#strings_map ⇒ Object (readonly)
Returns the value of attribute strings_map.
62 63 64 |
# File 'lib/twine/stringsfile.rb', line 62 def strings_map @strings_map end |
Instance Method Details
#add_language_code(code) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/twine/stringsfile.rb', line 80 def add_language_code(code) if @language_codes.length == 0 @language_codes << code elsif !@language_codes.include?(code) dev_lang = @language_codes[0] @language_codes << code @language_codes.delete(dev_lang) @language_codes.sort! @language_codes.insert(0, dev_lang) end end |
#read(path) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/twine/stringsfile.rb', line 97 def read(path) if !File.file?(path) raise Twine::Error.new("File does not exist: #{path}") end File.open(path, 'r:UTF-8') do |f| line_num = 0 current_section = nil current_row = nil while line = f.gets parsed = false line.strip! line_num += 1 if line.length == 0 next end if line.length > 4 && line[0, 2] == '[[' match = /^\[\[(.+)\]\]$/.match(line) if match current_section = StringsSection.new(match[1]) @sections << current_section parsed = true end elsif line.length > 2 && line[0, 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('') @sections << current_section end current_section.rows << current_row parsed = true end else match = /^([^=]+)=(.*)$/.match(line) if match key = match[1].strip value = match[2].strip value = value[1..-2] if value[0] == '`' && value[-1] == '`' case key when 'comment' current_row.comment = value when 'tags' current_row. = value.split(',') when 'ref' current_row.reference_key = value if value else if !@language_codes.include? key add_language_code(key) end current_row.translations[key] = value end parsed = true end end if !parsed raise Twine::Error.new("Unable to parse line #{line_num} of #{path}: #{line}") end end end # resolve_references @strings_map.each do |key, row| next unless row.reference_key row.reference = @strings_map[row.reference_key] end end |
#set_developer_language_code(code) ⇒ Object
92 93 94 95 |
# File 'lib/twine/stringsfile.rb', line 92 def set_developer_language_code(code) @language_codes.delete(code) @language_codes.insert(0, code) end |
#write(path) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/twine/stringsfile.rb', line 172 def write(path) dev_lang = @language_codes[0] File.open(path, 'w:UTF-8') do |f| @sections.each do |section| if f.pos > 0 f.puts '' end f.puts "[[#{section.name}]]" section.rows.each do |row| f.puts "\t[#{row.key}]" value = write_value(row, dev_lang, f) if !value && !row.reference_key puts "Warning: #{row.key} does not exist in developer language '#{dev_lang}'" end if row.reference_key f.puts "\t\tref = #{row.reference_key}" end if row. && row..length > 0 tag_str = row..join(',') f.puts "\t\ttags = #{tag_str}" end if row.raw_comment and row.raw_comment.length > 0 f.puts "\t\tcomment = #{row.raw_comment}" end @language_codes[1..-1].each do |lang| write_value(row, lang, f) end end end end end |