Class: Pixab::Localization

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

Constant Summary collapse

ACCESS_TOKEN =
'bdbda2cc022951235808a4f6c7a7330d4de7dcf719650d7d2ceee260e07d3f01'
Project_AirBrush =
'546ed49bfca9d3a4f51ccf2c8c279d0f'
Project_AirBrush_Video =
'fcb3e858aa1d991e8c21222f3696ce67'
Project_AirBrush_Studio =
'16a0814854c608d496e8a410f567666b'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLocalization

Returns a new instance of Localization.



19
20
21
# File 'lib/Localization.rb', line 19

def initialize()
  @platform = 'iOS'
end

Instance Attribute Details

#modeObject

Returns the value of attribute mode.



17
18
19
# File 'lib/Localization.rb', line 17

def mode
  @mode
end

#platformObject

Returns the value of attribute platform.



17
18
19
# File 'lib/Localization.rb', line 17

def platform
  @platform
end

#projectsObject

Returns the value of attribute projects.



17
18
19
# File 'lib/Localization.rb', line 17

def projects
  @projects
end

#tagsObject

Returns the value of attribute tags.



17
18
19
# File 'lib/Localization.rb', line 17

def tags
  @tags
end

Instance Method Details

#add_project(project_id) ⇒ Object

添加拉取的project



143
144
145
146
147
148
149
# File 'lib/Localization.rb', line 143

def add_project(project_id)
  if projects.nil?
    @projects = "#{project_id}"
  else
    @projects += ",#{project_id}"
  end
end

#assemble_data(string) ⇒ Object

重新组装本地化数据



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/Localization.rb', line 103

def assemble_data(string)
  if string.nil? || string.empty?
    return {}
  end

  localized_info_category = {}
  objects = JSON.parse(string)
  objects.each do |object|
    locale_name = object["locale"]["name"]
    localized_infos = localized_info_category[locale_name]
    if localized_infos.nil?
      localized_infos = []
      localized_info_category[locale_name] = localized_infos
    end
    local_key = object['key']
    local_key_name = local_key['name']
    if local_key['plural']
      local_key_name += "_#{object['plural_suffix']}"
    end
    localized_infos.push({'key' => local_key_name, 'value' => "#{object['content']}"})
  end
  return localized_info_category
end

#replace_local_files(localized_info_category) ⇒ Object

替换本地文件



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/Localization.rb', line 128

def replace_local_files(localized_info_category)
  platform_commands = nil
  if !mode.nil? 
    platform_commands = ['--mode', mode]
  end
  if platform == 'android'
    LocalizationAndroid.new.run(localized_info_category, platform_commands)
  elsif platform == 'Mac'
    LocalizationMac.new.run(localized_info_category, platform_commands)
  else
    LocalizationiOS.new.run(localized_info_category, platform_commands)
  end
end

#retrieveLocalizationStringObject

从Phrase平台检索获取本地化文案



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
# File 'lib/Localization.rb', line 71

def retrieveLocalizationString
  if projects.nil?
    puts "Error: project id cannot be nil".red
    exit(1)
  end

  localized_info_category = {}
  project_array = projects.split(',')
  project_array.each do |project|
    page_number = 1
    while true
      link = "\"https://api.phrase.com/v2/projects/#{project}/translations?page=#{page_number}&per_page=100&sort=created_at" 
      if !tags.nil?
        link += "&q=tags:#{tags}%20excluded:false" 
      end
      link += "\""
      access_token = "-u #{ACCESS_TOKEN}:"
      localized_string = `curl #{link} #{access_token}`
      per_localized_info_category = assemble_data(localized_string)
      if per_localized_info_category.empty?
        break
      end
      localized_info_category.merge!(per_localized_info_category) do |key, oldval, newval|
        oldval + newval
      end
      page_number += 1
    end
  end
  return localized_info_category
end

#run(commands = nil) ⇒ Object



23
24
25
26
27
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
# File 'lib/Localization.rb', line 23

def run(commands = nil)
  commands.each_index do |index|
    command = commands[index]
    case command
    when '--project-ab'
      add_project(Project_AirBrush)
    when '--project-abv'
      add_project(Project_AirBrush_Video)
    when '--ab-android'
      @projects = "#{Project_AirBrush},#{Project_AirBrush_Video}"
      @platform = 'android'
      @tags = 'android'
    when '--ab-iOS'
      @projects = "#{Project_AirBrush},#{Project_AirBrush_Video}"
      @tags = 'iOS'
    when '--abv-iOS'
      @projects = "#{Project_AirBrush_Video}"
      @mode = 'add'
      @tags = 'iOS'
    when '--ab-Mac'
      @projects = "#{Project_AirBrush_Studio}"
      @platform = 'Mac'
      @mode = 'add'
    end
  end

  commands.each_index do |index|
    command = commands[index]
    case command
    when '--projects'
      @projects = commands[index + 1] 
    when '--tags' 
      @tags = commands[index + 1]
    when '--platform'
      @platform = commands[index + 1]
    when '--mode'
      @mode = commands[index + 1]
    end
  end

  puts "\n》》》》》正在下载本地化文案 》》》》》》》》》》\n".green
  localized_info_category = retrieveLocalizationString

  puts "\n》》》》》正在替换本地化文案 》》》》》》》》》》\n".green
  replace_local_files(localized_info_category)
end