pixab 0.1.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 24860067840a1b6784ebde7e069f65f47eb42bb10c8c967b87b37c7d6d97ac4b
4
- data.tar.gz: 51d0b8dc02d032b103640154b8a5387bbd658a803ac1ef894d34d9652e948f5c
3
+ metadata.gz: 7c08c4161b4d58e4231ff4fda0f44c2e7e30746f1ac4aac389f47977640aea3d
4
+ data.tar.gz: 75710c9fd4596aaa80df25a8c39136d67adbc2b295c475f081d0899085aa716a
5
5
  SHA512:
6
- metadata.gz: 39edad6bde0aecb0030f1f1944e356d0eba931c3217eb71f6a3baa26511e1cf9503d573cc06a2cd65fe243269faf9200455060de8271a7dee0c0a4459ac05059
7
- data.tar.gz: b269bf421f0b4afb35c39242872cb5ab0639847946af250ab637f14b72b3d68e925a9092c2fe36885a48790ff7dc504372dd926d282cdeb24d811da3a4eefb33
6
+ metadata.gz: 1ed8b6d8a3d767e2c34e40bb289c5d46e951ccf027a7542c5a9e12ab464fc1a3e182de961ceb4a5cd13803ede407ddf9b31ac663df831897f2b8632b701ee6c0
7
+ data.tar.gz: 24108b17da8b4ddc10ed714ca25b0ef442c1a67e8d5c35aa7fd82aec54153fd1bacbb477cc689e5a2bc3f9f80c487675f1bb0963fea2a60e049a01d5b53bfccb
data/exe/pixab CHANGED
@@ -4,6 +4,7 @@
4
4
 
5
5
  require_relative '../lib/MergeRequest.rb'
6
6
  require_relative '../lib/ComponentSynchronizer.rb'
7
+ require_relative '../lib/Localization.rb'
7
8
 
8
9
  case ARGV[0]
9
10
  when 'merge'
@@ -25,6 +26,8 @@ when 'sync'
25
26
  end
26
27
  Pixab::MergeRequest.new(repo_manager, commands).run
27
28
  end
29
+ when 'localize'
30
+ Pixab::Localization.new.run(ARGV[1..-1])
28
31
  else
29
32
  puts "Invalid command".red
30
33
  end
@@ -0,0 +1,127 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ require 'colored2'
5
+ require 'json'
6
+ require_relative 'LocalizationPlatform.rb'
7
+
8
+ module Pixab
9
+
10
+ class Localization
11
+
12
+ ACCESS_TOKEN = '4f5379d0d26b9a2ef167b3fc84fb47f1fe2b4e87b1f95d8f5fc15269132051ef'
13
+ Project_AirBrush = '546ed49bfca9d3a4f51ccf2c8c279d0f'
14
+ Project_AirBrush_Video = 'fcb3e858aa1d991e8c21222f3696ce67'
15
+
16
+ attr_accessor :projects, :tags, :platform, :mode
17
+
18
+ def initialize()
19
+ @platform = 'iOS'
20
+ end
21
+
22
+ def run(commands = nil)
23
+ commands.each_index do |index|
24
+ command = commands[index]
25
+ case command
26
+ when '--project-ab'
27
+ add_project(Project_AirBrush)
28
+ when '--project-abv'
29
+ add_project(Project_AirBrush_Video)
30
+ when '--tags'
31
+ @tags = commands[index + 1]
32
+ when '--platform'
33
+ @platform = commands[index + 1]
34
+ when '--mode'
35
+ @mode = commands[index + 1]
36
+ when '--ab-android'
37
+ @projects = "#{Project_AirBrush},#{Project_AirBrush_Video}"
38
+ @platform = 'android'
39
+ when '--ab-iOS'
40
+ @projects = "#{Project_AirBrush}"
41
+ @mode = 'add'
42
+ when '--abv-iOS'
43
+ @projects = "#{Project_AirBrush_Video}"
44
+ @mode = 'add'
45
+ end
46
+ end
47
+
48
+ commands.each_index do |index|
49
+ command = commands[index]
50
+ case command
51
+ when '--projects'
52
+ @projects = commands[index + 1]
53
+ end
54
+ end
55
+
56
+ puts "\n》》》》》正在下载本地化文案 》》》》》》》》》》\n".green
57
+ localized_info_category = retrieveLocalizationString
58
+
59
+ puts "\n》》》》》正在替换本地化文案 》》》》》》》》》》\n".green
60
+ replace_local_files(localized_info_category)
61
+ end
62
+
63
+ # 从Phrase平台检索获取本地化文案
64
+ def retrieveLocalizationString
65
+ if projects.nil?
66
+ puts "Error: project id cannot be nil".red
67
+ exit(1)
68
+ end
69
+
70
+ localized_info_category = {}
71
+ project_array = projects.split(',')
72
+ project_array.each do |project|
73
+ command = "\"https://api.phrase.com/v2/projects/#{project}/translations"
74
+ if !tags.nil?
75
+ command += "?q=tags:#{tags}"
76
+ end
77
+ command += "\" -u #{ACCESS_TOKEN}:"
78
+ localized_string = `curl #{command}`
79
+ localized_info_category.merge!(assemble_data(localized_string)) do |key, oldval, newval|
80
+ oldval + newval
81
+ end
82
+ end
83
+ return localized_info_category
84
+ end
85
+
86
+ # 重新组装本地化数据
87
+ def assemble_data(string)
88
+ objects = JSON.parse(string)
89
+ localized_info_category = {}
90
+ objects.each do |object|
91
+ locale_name = object["locale"]["name"]
92
+ localized_infos = localized_info_category[locale_name]
93
+ if localized_infos.nil?
94
+ localized_infos = []
95
+ localized_info_category[locale_name] = localized_infos
96
+ end
97
+ localized_infos.push({'key' => "#{object['key']['name']}", 'value' => "#{object['content']}"})
98
+ end
99
+ return localized_info_category
100
+ end
101
+
102
+ # 替换本地文件
103
+ def replace_local_files(localized_info_category)
104
+ platform_commands = nil
105
+ if !mode.nil?
106
+ platform_commands = ['--mode', mode]
107
+ end
108
+ if platform == 'android'
109
+ LocalizationAndroid.new.run(localized_info_category, platform_commands)
110
+ else
111
+ LocalizationiOS.new.run(localized_info_category, platform_commands)
112
+ end
113
+ end
114
+
115
+ # 添加拉取的project
116
+ def add_project(project_id)
117
+ if projects.nil?
118
+ @projects = "#{project_id}"
119
+ else
120
+ @projects += ",#{project_id}"
121
+ end
122
+ end
123
+
124
+ end
125
+
126
+ end
127
+
@@ -0,0 +1,128 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ require "fileutils"
5
+
6
+ module Pixab
7
+
8
+ class LocalizationPlatform
9
+
10
+ def initialize()
11
+ @file_mode = 'w+'
12
+ end
13
+
14
+ def run(localized_info_category, commands)
15
+ resolve_commands(commands)
16
+ end
17
+
18
+ def resolve_commands(commands)
19
+ if !commands.nil?
20
+ commands.each_index do |index|
21
+ command = commands[index]
22
+ case command
23
+ when "--mode"
24
+ mode = commands[index + 1]
25
+ if mode == 'add'
26
+ @file_mode = 'a+'
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ def dir_name(locale)
34
+ return ''
35
+ end
36
+
37
+ end
38
+
39
+ end
40
+
41
+
42
+ module Pixab
43
+
44
+ class LocalizationiOS < LocalizationPlatform
45
+
46
+ File_name = "Localizable.strings"
47
+
48
+ def run(localized_info_category, commands)
49
+ super(localized_info_category, commands)
50
+
51
+ localized_info_category.each do |locale, localized_infos|
52
+ content_dir_path = dir_name(locale)
53
+ if !Dir.exists?(content_dir_path)
54
+ FileUtils.mkdir_p content_dir_path
55
+ end
56
+ content_file_path = "#{content_dir_path}/#{File_name}"
57
+ File.open(content_file_path, @file_mode) do |aFile|
58
+ aFile.syswrite("\n// Created from phrase api <<<<<<<<<< begin\n")
59
+ localized_infos.each do |localized_info|
60
+ aFile.syswrite("\n\"#{localized_info['key']}\" = \"#{localized_info['value']}\";\n")
61
+ end
62
+ aFile.syswrite("\n// Created from phrase api <<<<<<<<<< end\n")
63
+ end
64
+ end
65
+ end
66
+
67
+
68
+ def dir_name(locale)
69
+ "#{locale}.lproj"
70
+ end
71
+
72
+
73
+ end
74
+
75
+ end
76
+
77
+ module Pixab
78
+
79
+ class LocalizationAndroid < LocalizationPlatform
80
+
81
+ File_name = "strings_ph.xml"
82
+ Exclude_locales = ['zh-Hant']
83
+
84
+ def run(localized_info_category, commands)
85
+ localized_info_category.each do |locale, localized_infos|
86
+ if Exclude_locales.include?(locale)
87
+ next
88
+ end
89
+
90
+ content_dir_path = dir_name(locale)
91
+ if !Dir.exists?(content_dir_path)
92
+ FileUtils.mkdir_p content_dir_path
93
+ end
94
+ content_file_path = "#{content_dir_path}/#{File_name}"
95
+ File.open(content_file_path, @file_mode) do |aFile|
96
+ aFile.syswrite("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
97
+ aFile.syswrite("<resources>\n")
98
+ localized_infos.each do |localized_info|
99
+ aFile.syswrite(" <string name=\"#{localized_info['key']}\">#{localized_info['value']}</string>\n")
100
+ end
101
+ aFile.syswrite("</resources>\n")
102
+ end
103
+ end
104
+ end
105
+
106
+
107
+ def dir_name(locale)
108
+ suffix = ''
109
+ case locale
110
+ when 'en'
111
+ suffix = ''
112
+ when 'fr'
113
+ suffix = '-fr-rFR'
114
+ when 'ru'
115
+ suffix = '-ru-rRU'
116
+ when 'zh-Hans'
117
+ suffix = '-zh-rCN'
118
+ when 'tr'
119
+ suffix = '-tr-rTR'
120
+ else
121
+ suffix = "-#{locale}"
122
+ end
123
+ return "values#{suffix}"
124
+ end
125
+
126
+ end
127
+
128
+ end
data/lib/pixab/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pixab
4
- VERSION = "0.1.0"
4
+ VERSION = "1.1.0"
5
5
  end
data/pixab.gemspec CHANGED
@@ -33,7 +33,7 @@ Gem::Specification.new do |spec|
33
33
  spec.require_paths = ["lib"]
34
34
 
35
35
  # Uncomment to register a new dependency of your gem
36
- # spec.add_dependency "example-gem", "~> 1.0"
36
+ spec.add_dependency "colored2"
37
37
 
38
38
  # For more information and examples about making a new gem, check out our
39
39
  # guide at: https://bundler.io/guides/creating_gem.html
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pixab
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - 廖再润
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-12-20 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2022-12-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colored2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description: Write a longer description or delete this line.
14
28
  email:
15
29
  - lzr3@us.meitu.com
@@ -26,6 +40,8 @@ files:
26
40
  - Rakefile
27
41
  - exe/pixab
28
42
  - lib/ComponentSynchronizer.rb
43
+ - lib/Localization.rb
44
+ - lib/LocalizationPlatform.rb
29
45
  - lib/MergeRequest.rb
30
46
  - lib/RepoManager.rb
31
47
  - lib/Utilities.rb