Class: Twine::Formatters::Apple

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

Constant Summary

Constants inherited from Abstract

Twine::Formatters::Abstract::LANGUAGE_CODE_WITH_OPTIONAL_REGION_CODE

Instance Attribute Summary

Attributes inherited from Abstract

#options, #twine_file

Instance Method Summary collapse

Methods inherited from Abstract

#escape_quotes, #format_definition, #format_file, #format_key_value, #format_section, #format_sections, #initialize, #set_comment_for_key, #set_translation_for_key, #should_include_definition

Constructor Details

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

Instance Method Details

#can_handle_directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#default_file_nameObject



16
17
18
# File 'lib/twine/formatters/apple.rb', line 16

def default_file_name
  'Localizable.strings'
end

#determine_language_given_path(path) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/twine/formatters/apple.rb', line 20

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

  return super
end

#extensionObject



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

def extension
  '.strings'
end

#format_comment(definition, lang) ⇒ Object



78
79
80
# File 'lib/twine/formatters/apple.rb', line 78

def format_comment(definition, lang)
  "/* #{definition.comment.gsub('*/', '* /')} */\n" if definition.comment
end

#format_header(lang) ⇒ Object



66
67
68
# File 'lib/twine/formatters/apple.rb', line 66

def format_header(lang)
  "/**\n * Apple Strings File\n * Generated by Twine #{Twine::VERSION}\n * Language: #{lang}\n */"
end

#format_key(key) ⇒ Object



82
83
84
# File 'lib/twine/formatters/apple.rb', line 82

def format_key(key)
  escape_quotes(key)
end

#format_nameObject



4
5
6
# File 'lib/twine/formatters/apple.rb', line 4

def format_name
  'apple'
end

#format_section_header(section) ⇒ Object



70
71
72
# File 'lib/twine/formatters/apple.rb', line 70

def format_section_header(section)
  "/********** #{section.name} **********/\n"
end

#format_value(value) ⇒ Object



86
87
88
# File 'lib/twine/formatters/apple.rb', line 86

def format_value(value)
  escape_quotes(value)
end

#key_value_patternObject



74
75
76
# File 'lib/twine/formatters/apple.rb', line 74

def key_value_pattern
  "\"%{key}\" = \"%{value}\";\n"
end

#output_path_for_language(lang) ⇒ Object



36
37
38
# File 'lib/twine/formatters/apple.rb', line 36

def output_path_for_language(lang)
  "#{lang}.lproj"
end

#read(io, lang) ⇒ Object



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

def read(io, lang)
  last_comment = nil
  while line = io.gets
    # matches a `key = "value"` line, where key may be quoted or unquoted. The former may also contain escaped characters
    match = /^\s*((?:"(?:[^"\\]|\\.)+")|(?:[^"\s=]+))\s*=\s*"((?:[^"\\]|\\.)*)"/.match(line)
    if match
      key = match[1]
      key = key[1..-2] if key[0] == '"' and key[-1] == '"'
      key.gsub!('\\"', '"')
      value = match[2]
      value.gsub!('\\"', '"')
      set_translation_for_key(key, lang, value)
      if last_comment
        set_comment_for_key(key, last_comment)
      end
    end

    match = /\/\* (.*) \*\//.match(line)
    if match
      last_comment = match[1]
    else
      last_comment = nil
    end
  end
end