Class: IosStrings

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n_po_tools/utils/ios_strings.rb

Overview

Format description: developer.apple.com/library/mac/documentation/macosx/conceptual/bpinternational/Articles/StringsFiles.html

Example: /* Insert Element menu item */ “Insert Element” = “Insert Element”; “Windows must have at least %d columns and %d rows.” = “Les fenêtres doivent être composes au minimum de %d colonnes et %d lignes.”; “File %@ not found.” = “Le fichier %@ n’existe pas.”; “%@ Error! %@ failed!” = “%2$@ blah blah, %1$@ blah!”; “File "%@" cannot be opened” = “ … ”;

Localizable.strings is a default filename. It is recommended that you save strings files using the UTF-16 encoding.

Use /usr/bin/plutil -lint (part of Mac OS X) to check for syntax errors.

Constant Summary collapse

DEFAULT_FILE_NAME =
'Localizable.strings'
EXTENSION =
'.strings'

Class Method Summary collapse

Class Method Details

.encoding_for_path(path) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/i18n_po_tools/utils/ios_strings.rb', line 24

def self.encoding_for_path(path)
  File.open(path, 'rb') do |f|
    begin
      a = f.readbyte
      b = f.readbyte
      if (a == 0xfe && b == 0xff)
        return 'UTF-16BE'
      elsif (a == 0xff && b == 0xfe)
        return 'UTF-16LE'
      end
    rescue EOFError
    end
  end

  'UTF-8'
end

.hash_to_strings(h, comment = '') ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/i18n_po_tools/utils/ios_strings.rb', line 63

def self.hash_to_strings(h, comment = '')      
  content = ""
  content = "/* "+comment.gsub('*/','* /')+" */\n" if comment.present?

  h.each_pair do |k,v|
    key = escape(k)
    value = escape(v)
    content += %Q{"#{key}" = "#{value}";\n}
  end unless h.nil?

  content
end

.strings_to_hash(data) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/i18n_po_tools/utils/ios_strings.rb', line 41

def self.strings_to_hash(data)
  data = data.encode('UTF-8')
  h = {}

  #remove comments
  data.gsub!(/\/\*.*\*\//,'') # /* lala */
  data.gsub!(/^\s*\/\/.*/,'') # // lala

  regexp = /"((?:[^"\\]|\\.)+)"\s*=\s*"((?:[^"\\]|\\.)*)"/
  data.split("\n").each do |line|
    match = regexp.match(line)
    next unless match

    key = unescape(match[1])
    value = unescape(match[2])

    h[key] = value
  end

  h
end