@@ -13,6 +13,7 @@ LANE_VALUE_XCFRAMEWORK_CHECKSUM_PATH = 'WP_XCFRAMEWORK_CHECKSUM_PATH'
13
13
14
14
GITHUB_REPO = 'automattic/wordpress-rs'
15
15
GIT_REMOTE_NAME = 'origin'
16
+ GIT_BASE_BRANCH = 'trunk'
16
17
17
18
# Localization constants
18
19
LOCALIZATION_FLUENT_FILES_DIR = File . join ( PROJECT_ROOT , 'wp_localization' , 'localization' )
179
180
# The resulting PO file is saved as the source file (.pot) for translations and is synced to GlotPress.
180
181
#
181
182
# @param commit_changes [Boolean] Whether to commit the generated PO file (default: false)
183
+ # @param skip_date_only_changes [Boolean] Whether to skip commit if only date headers changed (default: true)
182
184
#
183
- lane :generate_source_po_file do |commit_changes : false |
185
+ lane :generate_source_po_file do |commit_changes : false , skip_date_only_changes : true |
184
186
UI . header ( '🔄 Converting English Fluent file to PO format' )
185
187
186
188
FileUtils . mkdir_p ( File . dirname ( LOCALIZATION_PO_SOURCE_FILE ) )
@@ -202,6 +204,12 @@ lane :generate_source_po_file do |commit_changes: false|
202
204
end
203
205
204
206
if commit_changes
207
+ # Check if we should skip commit when only date headers changed
208
+ if skip_date_only_changes && only_date_headers_changed? ( LOCALIZATION_PO_SOURCE_FILE )
209
+ UI . message ( 'ℹ️ Skipping commit - only date headers changed in PO file' )
210
+ next
211
+ end
212
+
205
213
commit_changed_files (
206
214
files : LOCALIZATION_PO_SOURCE_FILE ,
207
215
message : 'Update source PO file (en-US.pot) to be synced to GlotPress'
@@ -248,6 +256,74 @@ lane :download_translations do |commit_changes: false|
248
256
end
249
257
end
250
258
259
+ # Synchronizes localization files with GlotPress and creates a PR
260
+ #
261
+ # This lane performs a complete localization sync:
262
+ # 1. Generates source PO file from English Fluent file
263
+ # 2. Downloads latest translations from GlotPress
264
+ # 3. Creates a PR with the changes instead of pushing directly to trunk
265
+ #
266
+ lane :sync_localization do
267
+ github_token = ENV [ 'GITHUB_TOKEN' ] || UI . user_error! ( 'GITHUB_TOKEN environment variable is required' )
268
+
269
+ sync_branch = 'localization/sync-translations'
270
+
271
+ Fastlane ::Helper ::GitHelper . delete_local_branch_if_exists! ( sync_branch )
272
+ Fastlane ::Helper ::GitHelper . create_branch ( sync_branch )
273
+
274
+ po_result = generate_source_po_file ( commit_changes : true , skip_date_only_changes : true )
275
+ translations_result = download_translations ( commit_changes : true )
276
+
277
+ # Check if either operation resulted in changes
278
+ has_changes = !po_result . nil? || !translations_result . nil?
279
+ unless has_changes
280
+ UI . important ( '⚠️ No changes to commit' )
281
+
282
+ if is_ci
283
+ buildkite_annotate (
284
+ context : 'localization-sync-no-changes' ,
285
+ style : 'info' ,
286
+ message : 'No localization changes to commit. No PR was created.'
287
+ )
288
+ end
289
+
290
+ next
291
+ end
292
+
293
+ Fastlane ::Helper ::GitHelper . delete_remote_branch_if_exists! ( sync_branch )
294
+
295
+ # Push the localization sync branch to remote
296
+ push_to_git_remote ( set_upstream : true , tags : false )
297
+
298
+ pr_result = create_pull_request (
299
+ repo : GITHUB_REPO ,
300
+ title : 'Sync localization files with GlotPress' ,
301
+ body : <<~BODY ,
302
+ This PR synchronizes localization files with GlotPress:
303
+
304
+ - **Source PO file**: Updated from English Fluent file for GlotPress sync
305
+ - **Translations**: Downloaded latest translations from GlotPress and converted to Fluent format
306
+
307
+ 🔄 This is an automated localization sync.
308
+ BODY
309
+ labels : 'Localization' ,
310
+ team_reviewers : [ 'wordpress-rs' ] ,
311
+ base : GIT_BASE_BRANCH ,
312
+ head : sync_branch ,
313
+ api_token : github_token
314
+ )
315
+
316
+ UI . success ( "✅ Created pull request: #{ pr_result } " ) if pr_result
317
+
318
+ if is_ci && pr_result
319
+ buildkite_annotate (
320
+ context : 'localization-sync-pr' ,
321
+ style : 'info' ,
322
+ message : "Localization Sync Pull Request: #{ pr_result } "
323
+ )
324
+ end
325
+ end
326
+
251
327
# Downloads PO files from GlotPress for existing project locales
252
328
#
253
329
# This lane fetches translated PO files from GlotPress for all supported locales
@@ -357,4 +433,34 @@ def commit_changed_files(files:, message:, push: false)
357
433
elsif push
358
434
push_to_git_remote ( set_upstream : true , tags : false )
359
435
end
436
+
437
+ result
438
+ end
439
+
440
+ # Checks if only date headers changed in a PO file
441
+ #
442
+ # This method uses git diff to check if the only changes in a PO file
443
+ # are the POT-Creation-Date and PO-Revision-Date headers.
444
+ #
445
+ # @param file_path [String] Path to the PO file to check
446
+ # @return [Boolean] true if only date headers changed, false otherwise
447
+ #
448
+ def only_date_headers_changed? ( file_path )
449
+ begin
450
+ diff_output = sh ( 'git' , 'diff' , file_path ) . strip
451
+ rescue
452
+ return false
453
+ end
454
+
455
+ return false if diff_output . empty?
456
+
457
+ # Parse diff lines that start with + or - (actual changes)
458
+ changed_lines = diff_output . split ( "\n " ) . select do |line |
459
+ line . start_with? ( '+' ) || line . start_with? ( '-' )
460
+ end . reject do |line |
461
+ # Exclude diff metadata lines
462
+ line . start_with? ( '+++' ) || line . start_with? ( '---' )
463
+ end
464
+
465
+ changed_lines . all? { |l | l . include? ( '"POT-Creation-Date:' ) || l . include? ( '"PO-Revision-Date:' ) }
360
466
end
0 commit comments