Class: Twine::StringsFile

Inherits:
Object
  • Object
show all
Defined in:
lib/twine/stringsfile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStringsFile

Returns a new instance of StringsFile.



64
65
66
67
68
# File 'lib/twine/stringsfile.rb', line 64

def initialize
  @sections = []
  @strings_map = {}
  @language_codes = []
end

Instance Attribute Details

#language_codesObject (readonly)

Returns the value of attribute language_codes.



62
63
64
# File 'lib/twine/stringsfile.rb', line 62

def language_codes
  @language_codes
end

#sectionsObject (readonly)

Returns the value of attribute sections.



60
61
62
# File 'lib/twine/stringsfile.rb', line 60

def sections
  @sections
end

#strings_mapObject (readonly)

Returns the value of attribute strings_map.



61
62
63
# File 'lib/twine/stringsfile.rb', line 61

def strings_map
  @strings_map
end

Instance Method Details

#add_language_code(code) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/twine/stringsfile.rb', line 182

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
# File 'lib/twine/stringsfile.rb', line 70

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].strip)
          @sections << current_section
          parsed = true
        end
      elsif line.length > 2 && line[0, 1] == '['
        match = /^\[(.+)\]$/.match(line)
        if match
          current_row = StringsRow.new(match[1].strip)
          @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
          if value[0,1] == '`' && value[-1,1] == '`'
            value = value[1..-2]
          end

          case key
          when "comment"
            current_row.comment = value
          when 'tags'
            current_row.tags = value.split(',')
          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
end

#write(path) ⇒ Object



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
171
172
173
174
175
176
177
178
179
180
# File 'lib/twine/stringsfile.rb', line 138

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 = row.translations[dev_lang]
        if !value
          puts "Warning: #{row.key} does not exist in developer language '#{dev_lang}'"
        else
          if value[0,1] == ' ' || value[-1,1] == ' ' || (value[0,1] == '`' && value[-1,1] == '`')
            value = '`' + value + '`'
          end
          f.puts "\t\t#{dev_lang} = #{value}"
        end

        if row.tags && row.tags.length > 0
          tag_str = row.tags.join(',')
          f.puts "\t\ttags = #{tag_str}"
        end
        if row.comment && row.comment.length > 0
          f.puts "\t\tcomment = #{row.comment}"
        end
        @language_codes[1..-1].each do |lang|
          value = row.translations[lang]
          if value && value != row.translations[dev_lang]
            if value[0,1] == ' ' || value[-1,1] == ' ' || (value[0,1] == '`' && value[-1,1] == '`')
              value = '`' + value + '`'
            end
            f.puts "\t\t#{lang} = #{value}"
          end
        end
      end
    end
  end
end