Class: Twine::Formatters::Apple

Inherits:
Abstract
  • Object
show all
Defined in:
lib/twine/formatters/apple.rb

Constant Summary collapse

FORMAT_NAME =
'apple'
EXTENSION =
'.strings'
DEFAULT_FILE_NAME =
'Localizable.strings'

Instance Attribute Summary

Attributes inherited from Abstract

#options, #strings

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Abstract

#androidify_substitutions, #initialize, #iosify_substitutions, #set_comment_for_key, #set_translation_for_key, #write_all_files

Constructor Details

This class inherits a constructor from Twine::Formatters::Abstract

Class Method Details

.can_handle_directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/twine/formatters/apple.rb', line 8

def self.can_handle_directory?(path)
  Dir.entries(path).any? { |item| /^.+\.lproj$/.match(item) }
end

Instance Method Details

#default_file_nameObject



12
13
14
# File 'lib/twine/formatters/apple.rb', line 12

def default_file_name
  return DEFAULT_FILE_NAME
end

#determine_language_given_path(path) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/twine/formatters/apple.rb', line 16

def determine_language_given_path(path)
  path_arr = path.split(File::SEPARATOR)
  path_arr.each do |segment|
    match = /^(.+)\.lproj$/.match(segment)
    if match
      return match[1]
    end
  end

  return
end

#read_file(path, lang) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/twine/formatters/apple.rb', line 28

def read_file(path, lang)
  encoding = Twine::Encoding.encoding_for_path(path)
  sep = nil
  if !encoding.respond_to?(:encode)
    # This code is not necessary in 1.9.3 and does not work as it did in 1.8.7.
    if encoding.end_with? 'LE'
      sep = "\x0a\x00"
    elsif encoding.end_with? 'BE'
      sep = "\x00\x0a"
    else
      sep = "\n"
    end
  end

  if encoding.index('UTF-16')
    mode = "rb:#{encoding}"
  else
    mode = "r:#{encoding}"
  end

  File.open(path, mode) do |f|
    last_comment = nil
    while line = (sep) ? f.gets(sep) : f.gets
      if encoding.index('UTF-16')
        if line.respond_to? :encode!
          line.encode!('UTF-8')
        else
          require 'iconv'
          line = Iconv.iconv('UTF-8', encoding, line).join
        end
      end
      match = /"((?:[^"\\]|\\.)+)"\s*=\s*"((?:[^"\\]|\\.)*)"/.match(line)
      if match
        key = match[1]
        key.gsub!('\\"', '"')
        value = match[2]
        value.gsub!('\\"', '"')
        value = iosify_substitutions(value)
        set_translation_for_key(key, lang, value)
        if last_comment
          set_comment_for_key(key, last_comment)
        end
      end
      if @options[:consume_comments]
        match = /\/\* (.*) \*\//.match(line)
        if match
          last_comment = match[1]
        else
          last_comment = nil
        end
      end
    end
  end
end

#write_file(path, lang) ⇒ Object



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
# File 'lib/twine/formatters/apple.rb', line 83

def write_file(path, lang)
  default_lang = @strings.language_codes[0]
  encoding = @options[:output_encoding] || 'UTF-8'
  File.open(path, "w:#{encoding}") do |f|
    f.puts "/**\n * Apple Strings File\n * Generated by Twine #{Twine::VERSION}\n * Language: #{lang}\n */"
    @strings.sections.each do |section|
      printed_section = false
      section.rows.each do |row|
        if row.matches_tags?(@options[:tags], @options[:untagged])
          f.puts ''
          if !printed_section
            if section.name && section.name.length > 0
              f.print "/********** #{section.name} **********/\n\n"
            end
            printed_section = true
          end

          key = row.key
          key = key.gsub('"', '\\\\"')

          value = row.translated_string_for_lang(lang, default_lang)
          if value
            value = value.gsub('"', '\\\\"')

            comment = row.comment
            if comment
              comment = comment.gsub('*/', '* /')
            end

            if comment && comment.length > 0
              f.print "/* #{comment} */\n"
            end

            f.print "\"#{key}\" = \"#{value}\";\n"
          end
        end
      end
    end
  end
end