Class: Pixab::LocalizationiOS

Inherits:
LocalizationPlatform show all
Defined in:
lib/LocalizationPlatform.rb

Direct Known Subclasses

LocalizationMac

Constant Summary collapse

File_name =
"Localizable.strings"

Instance Method Summary collapse

Methods inherited from LocalizationPlatform

#file_mode, #resolve_commands

Instance Method Details

#begin_promptObject



119
120
121
# File 'lib/LocalizationPlatform.rb', line 119

def begin_prompt
  return "\n// Created from phrase api <<<<<<<<<< begin\n"
end

#dir_name(locale) ⇒ Object



75
76
77
# File 'lib/LocalizationPlatform.rb', line 75

def dir_name(locale)
  "#{locale}.lproj"
end

#end_promptObject



123
124
125
# File 'lib/LocalizationPlatform.rb', line 123

def end_prompt
  return "\n// Created from phrase api <<<<<<<<<< end\n"
end

#genetate_localized_string(localized_info) ⇒ Object



127
128
129
130
131
# File 'lib/LocalizationPlatform.rb', line 127

def genetate_localized_string(localized_info)
  value = localized_info['value'].gsub(/["]/, '\\\\\0')
  value = value.gsub(/%s/, '%@')
  return "\n\"#{localized_info['key']}\" = \"#{value}\";\n"
end

#run(localized_info_category, commands) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/LocalizationPlatform.rb', line 52

def run(localized_info_category, commands)
  super(localized_info_category, commands)

  localized_info_category.each do |locale, localized_infos|
    content_dir_path = dir_name(locale)
    if !Utilities.dir_exist(content_dir_path)
      FileUtils.mkdir_p content_dir_path
    end
    content_file_path = "#{content_dir_path}/#{File_name}"
    
    case @mode
    when 'add'
      write_file_add(content_file_path, localized_infos)
    when 'replace'
      write_file_replace(content_file_path, localized_infos)
    when 'append'
      write_file_append(content_file_path, localized_infos)
    else 
      write_file_by_mode(content_file_path, localized_infos, 'w')
    end
  end
end

#write_file_add(content_file_path, localized_infos) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/LocalizationPlatform.rb', line 79

def write_file_add(content_file_path, localized_infos)
  if File::exists?(content_file_path)
    # 移除以前脚本添加的本地化文案
    content = File.read(content_file_path)
    content = content.sub(/#{begin_prompt}.*#{end_prompt}/m, '')
    File.open(content_file_path, 'w+') do |file|
      file.syswrite(content)
    end
  end
  write_file_by_mode(content_file_path, localized_infos, file_mode)
end

#write_file_append(content_file_path, localized_infos) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/LocalizationPlatform.rb', line 95

def write_file_append(content_file_path, localized_infos)
  if File::exists?(content_file_path)
    file_content = File.read(content_file_path)
    if index = file_content.index(end_prompt)
      localized_infos.each do |localized_info|
        file_content.insert(index, genetate_localized_string(localized_info))        
      end
      File.open(content_file_path, 'w') { |file| file.write(file_content) }
      return
    end
  end
  write_file_by_mode(content_file_path, localized_infos, 'a+')
end

#write_file_by_mode(content_file_path, localized_infos, mode) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/LocalizationPlatform.rb', line 109

def write_file_by_mode(content_file_path, localized_infos, mode)
  File.open(content_file_path, mode) do |file|
    file.syswrite(begin_prompt)
    localized_infos.each do |localized_info|
      file.syswrite(genetate_localized_string(localized_info))
    end
    file.syswrite(end_prompt)
  end
end

#write_file_replace(content_file_path, localized_infos) ⇒ Object



91
92
93
# File 'lib/LocalizationPlatform.rb', line 91

def write_file_replace(content_file_path, localized_infos)
  # write_file_by_mode(content_file_path, localized_infos, file_mode)
end