lesli_babel 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,215 +0,0 @@
1
- =begin
2
-
3
- Copyright (c) 2020, all rights reserved.
4
-
5
- All the information provided by this platform is protected by international laws related to
6
- industrial property, intellectual property, copyright and relative international laws.
7
- All intellectual or industrial property rights of the code, texts, trade mark, design,
8
- pictures and any other information belongs to the owner of this platform.
9
-
10
- Without the written permission of the owner, any replication, modification,
11
- transmission, publication is strictly forbidden.
12
-
13
- For more information read the license file including with this software.
14
-
15
- // · ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~
16
- // ·
17
-
18
- =end
19
-
20
- module LesliBabel
21
- class TranslationsSynchronizationService
22
-
23
- # IMPORTANT: If you modify this file please check the CloneService that contains a modified
24
- # copy of this synchronization code
25
- def self.remote_sync double_way_sync=false
26
-
27
- host = "http://localhost:8080"
28
- host = "https://api.datenbanken.dev/v2"
29
- instance_code = LC::System::Info.instance[:code].gsub("_","-")
30
-
31
- # if special namespace is configured in the lesli.yml settings
32
- # this is useful when we need install an instance and customize
33
- # the translations for a client
34
- #instance_code = Rails.application.config.lesli.dig(:configuration, :babel, :namespace)
35
-
36
- api_endpoint = "#{host}/buckets/#{instance_code}-babel/documents"
37
-
38
-
39
- # get last sync data
40
- response = Faraday.get("#{api_endpoint}/last")
41
- response = FastJsonparser.parse(response.body)
42
- response = response[:documents][0]
43
-
44
- # if first time sync
45
- response = FastJsonparser.parse({ modules: [], buckets: [], strings: [] }.to_json) if response.blank?
46
-
47
- # add new modules
48
- response[:modules].each do |babel_module|
49
- next if babel_module[:name].blank?
50
-
51
- local_module = CloudBabel::Module
52
- .create_with({
53
- created_at: babel_module[:created_at],
54
- platform: babel_module[:platform]
55
- }).find_or_create_by({ name: babel_module[:name] })
56
-
57
- platform = babel_module[:platform]
58
- platform = "rails_engine" if babel_module[:name].start_with?("Cloud")
59
- platform = "rails_core" if babel_module[:name] == "Core"
60
- platform = "rails_builder" if ["MitwerkenCloud", "DeutscheLeibrenten", "LesliCloud"].include?(babel_module[:name])
61
-
62
- local_module.platform = platform
63
- local_module.updated_at = DateTime.now
64
- local_module.save
65
-
66
- end
67
-
68
- # working with buckets
69
- babel_reference_modules = {}
70
-
71
- response[:buckets].each do |babel_bucket|
72
-
73
- next if babel_bucket[:name].blank?
74
-
75
- # reference to modules that buckets belongs to
76
- # this reference as string is necessary because the id of the module can change
77
- # between synchronizations, thats why we search for the id of the module every time
78
- if babel_reference_modules[babel_bucket[:reference_module]].blank?
79
- babel_reference_modules[babel_bucket[:reference_module]] =
80
- CloudBabel::Module.find_by(name: babel_bucket[:reference_module])
81
- end
82
-
83
- # add new bucket
84
- CloudBabel::Bucket
85
- .create_with({
86
- created_at: babel_bucket[:created_at],
87
- module: babel_reference_modules[babel_bucket[:reference_module]]
88
- }).find_or_create_by({
89
- name: babel_bucket[:name],
90
- reference_module: babel_bucket[:reference_module]
91
- })
92
-
93
- end
94
-
95
- # · working with strings
96
- babel_reference_buckets = {}
97
-
98
- response[:strings].each do |remote_string|
99
- # reference to modules that buckets belongs to
100
- # this reference as string is necessary because the id of the module or bucket can change
101
- # between synchronizations, thats why we search for the id of the module every time
102
- if babel_reference_buckets[remote_string[:reference_bucket]].blank?
103
- remote_string[:reference_bucket] ||= remote_string[:reference_module_bucket]
104
- babel_reference_buckets[remote_string[:reference_bucket]] =
105
- CloudBabel::Bucket.find_by(
106
- name: remote_string[:reference_bucket].split("-")[1],
107
- reference_module: remote_string[:reference_bucket].split("-")[0]
108
- )
109
- end
110
-
111
- # Do not sync labes that do not have a valid module and bucket reference
112
- next if babel_reference_buckets[remote_string[:reference_bucket]].blank?
113
-
114
- remote_string_reference = "#{babel_reference_buckets[remote_string[:reference_bucket]].reference_module}-#{babel_reference_buckets[remote_string[:reference_bucket]].name}"
115
-
116
- # add new string if it does not exist
117
- local_string = CloudBabel::String.with_deleted.create_with({
118
- bucket: babel_reference_buckets[remote_string[:reference_bucket]]
119
- }).find_or_create_by({
120
- label: remote_string[:label],
121
- reference_module_bucket: remote_string_reference
122
- })
123
-
124
- # if status changed
125
- if remote_string[:status] != local_string["status"]
126
-
127
- remote_string[:last_update_status] = Time.now if remote_string[:last_update_status].blank?
128
- local_string["last_update_status"] = Time.now if local_string["last_update_status"].blank?
129
-
130
- # check if remote is newer than local
131
- if (remote_string[:last_update_status] > local_string["last_update_status"])
132
-
133
- # if so, update local translation with the incoming
134
- local_string["status"] = remote_string[:status]
135
- local_string["last_update_status"] = remote_string[:last_update_status]
136
-
137
- end
138
-
139
- end
140
-
141
- # if context changed
142
- if remote_string[:context] != local_string["context"]
143
-
144
- remote_string[:last_update_context] = Time.now if remote_string[:last_update_context].blank?
145
- local_string["last_update_context"] = Time.now if local_string["last_update_context"].blank?
146
-
147
- # check if remote is newer than local
148
- if (remote_string[:last_update_context] > local_string["last_update_context"])
149
-
150
- # if so, update local translation with the incoming
151
- local_string["context"] = remote_string[:context]
152
- local_string["last_update_context"] = remote_string[:last_update_context]
153
-
154
- end
155
-
156
- end
157
-
158
- # check if necessary to update any translation
159
- # due babel can work as client and server at the same time, we have to synchronize labels for all supported languages
160
- I18n.available_locales.each do |locale|
161
-
162
- # parse the remote time for comparison, if no time use the current time
163
- remote_time = Time.parse remote_string[:"last_update_#{locale}"] rescue Time.now.getutc
164
-
165
- # parse the remote time for comparison, if no time use old time so we can update the string
166
- local_time = Time.parse local_string["last_update_#{locale}"] rescue Time.parse("Jan 1900")
167
-
168
- # if translation changed
169
- if local_string[locale] != remote_string[:"#{locale}"]
170
-
171
- # check if remote is newer than local
172
- if (remote_time > local_time)
173
-
174
- # if so, update local translation with the incoming
175
- local_string[locale] = local_time
176
- local_string["last_update_#{locale}"] = remote_time
177
-
178
- end
179
-
180
- end
181
-
182
- end
183
-
184
- local_string.save
185
-
186
- end
187
-
188
- return LC::Response.service(true) if double_way_sync == false
189
-
190
- # · Collect modules, buckets and strings for syncronization
191
-
192
- # get and parse modules
193
- modules = CloudBabel::Module.all.map do |babel_module|
194
- babel_module.as_json
195
- end
196
-
197
- # get and parse buckets
198
- buckets = CloudBabel::Bucket.all.map do |babel_bucket|
199
- babel_bucket.as_json
200
- end
201
-
202
- # get and parse strings including the deleted ones
203
- strings = CloudBabel::String.with_deleted.all.map do |babel_string|
204
- babel_string.as_json
205
- end
206
-
207
- # send latest translation to raven
208
- response = Faraday.post(api_endpoint, ({ modules: modules, buckets: buckets, strings: strings }).to_json, "Content-Type" => "application/json")
209
-
210
- LC::Response.service true
211
-
212
- end
213
-
214
- end
215
- end