From c037ce16b9a110115af2289053d87430d78eec97 Mon Sep 17 00:00:00 2001 From: Brandon Kraft Date: Mon, 14 Apr 2025 10:46:35 -0500 Subject: [PATCH 1/3] Add automatic language updating to release script --- bin/prepare-release.php | 12 +++- bin/update-translations.php | 134 ++++++++++++++++++++++++++++++++++++ composer.json | 3 +- 3 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 bin/update-translations.php diff --git a/bin/prepare-release.php b/bin/prepare-release.php index 7d20fd5c..2895a23f 100644 --- a/bin/prepare-release.php +++ b/bin/prepare-release.php @@ -49,6 +49,7 @@ public function run() { $this->build_assets(); $this->run_tests(); $this->generate_docs(); + $this->update_translations(); $this->commit_changes(); $current_version = $this->get_current_version(); @@ -186,7 +187,7 @@ private function commit_changes() { if ( 0 !== $return ) { exit( $return ); } - passthru( 'git commit -m "Build assets and documentation"', $return ); + passthru( 'git commit -m "Build assets, documentation, and translations"', $return ); if ( 0 !== $return ) { exit( $return ); } @@ -346,6 +347,15 @@ private function confirm( $message ) { fclose( $handle ); return 'y' === $line; } + + /** + * Update plugin translations and cleanup + */ + private function update_translations() { + require_once __DIR__ . '/update-translations.php'; + $updater = new Translation_Updater(); + $updater->run(); + } } // Run the script diff --git a/bin/update-translations.php b/bin/update-translations.php new file mode 100644 index 00000000..81526708 --- /dev/null +++ b/bin/update-translations.php @@ -0,0 +1,134 @@ +#!/usr/bin/env php +lang_dir = __DIR__ . '/../lang'; + } + + /** + * Run the translation update + */ + public function run() { + if ( ! is_dir( $this->lang_dir ) ) { + echo "Language directory not found: {$this->lang_dir}\n"; + return; + } + + // Get available translations from WordPress.org + $translations = $this->get_available_translations(); + if ( empty( $translations ) ) { + echo "No translations available from WordPress.org\n"; + return; + } + + // Get existing translation files + $pattern = $this->lang_dir . '/' . $this->plugin_slug . '-*.po'; + $po_files = glob( $pattern ); + + if ( empty( $po_files ) ) { + echo "No translation files found in {$this->lang_dir}\n"; + return; + } + + foreach ( $po_files as $po_file ) { + $basename = basename( $po_file ); + $locale = str_replace( array( $this->plugin_slug . '-', '.po' ), '', $basename ); + + echo "→ Checking $locale...\n"; + + $po_path = $this->lang_dir . '/' . $this->plugin_slug . '-' . $locale . '.po'; + $mo_path = $this->lang_dir . '/' . $this->plugin_slug . '-' . $locale . '.mo'; + $l10n_path = $this->lang_dir . '/' . $this->plugin_slug . '-' . $locale . '.l10n.php'; + + // If translation is in WordPress.org list, delete local files + if ( isset( $translations[ $locale ] ) ) { + if ( file_exists( $po_path ) ) { + unlink( $po_path ); + echo " ✓ Deleted .po file\n"; + } + if ( file_exists( $mo_path ) ) { + unlink( $mo_path ); + echo " ✓ Deleted .mo file\n"; + } + if ( file_exists( $l10n_path ) ) { + unlink( $l10n_path ); + echo " ✓ Deleted .l10n.php file\n"; + } + echo " ✓ Translation available on WordPress.org\n\n"; + continue; + } + + echo " ⚠️ Translation not available on WordPress.org\n"; + } + } + + /** + * Get available translations from WordPress.org + * + * @return array + */ + private function get_available_translations() { + $api_url = sprintf( + 'https://api.wordpress.org/translations/plugins/1.0/?slug=%s', + $this->plugin_slug + ); + + $response = file_get_contents( $api_url ); + if ( false === $response ) { + return array(); + } + + $data = json_decode( $response, true ); + if ( ! isset( $data['translations'] ) ) { + return array(); + } + + $translations = array(); + foreach ( $data['translations'] as $translation ) { + $translations[ $translation['language'] ] = array( + 'package' => $translation['package'], + ); + } + + return $translations; + } +} + +// Run the script +$updater = new Translation_Updater(); +$updater->run(); diff --git a/composer.json b/composer.json index d2487154..d582de57 100644 --- a/composer.json +++ b/composer.json @@ -69,6 +69,7 @@ ], "test:php": "phpunit", "test:phpstan": "phpstan analyze --memory-limit=2G", - "test:phpstan:baseline": "phpstan analyze --memory-limit=2G --generate-baseline=bin/baseline.neon" + "test:phpstan:baseline": "phpstan analyze --memory-limit=2G --generate-baseline=bin/baseline.neon", + "update-translations": "php bin/update-translations.php" } } From 8bb09f78b0f7fc13f7f967127cf89657a05c6600 Mon Sep 17 00:00:00 2001 From: Paul Kevan <2290623+pkevan@users.noreply.github.com> Date: Thu, 17 Apr 2025 11:26:28 +0100 Subject: [PATCH 2/3] Return to correct directory When running `update_translations` we end up in a different directory (https://github.com/WordPress/secure-custom-fields/blob/c037ce16b9a110115af2289053d87430d78eec97/bin/update-translations.php#L16), this resets that to where it is expected. --- bin/prepare-release.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bin/prepare-release.php b/bin/prepare-release.php index 2895a23f..2e449e00 100644 --- a/bin/prepare-release.php +++ b/bin/prepare-release.php @@ -50,6 +50,8 @@ public function run() { $this->run_tests(); $this->generate_docs(); $this->update_translations(); + // need to return to correct directory after running `update_translations`. + exec( chdir( dirname( __DIR__ ) ) ); $this->commit_changes(); $current_version = $this->get_current_version(); From 2980c15cd108df3621a1ae700c8d53f8f6b9c35c Mon Sep 17 00:00:00 2001 From: Brandon Kraft Date: Thu, 17 Apr 2025 12:03:24 -0500 Subject: [PATCH 3/3] Remove unneeded chdir --- bin/prepare-release.php | 2 -- bin/update-translations.php | 3 --- 2 files changed, 5 deletions(-) diff --git a/bin/prepare-release.php b/bin/prepare-release.php index 2e449e00..2895a23f 100644 --- a/bin/prepare-release.php +++ b/bin/prepare-release.php @@ -50,8 +50,6 @@ public function run() { $this->run_tests(); $this->generate_docs(); $this->update_translations(); - // need to return to correct directory after running `update_translations`. - exec( chdir( dirname( __DIR__ ) ) ); $this->commit_changes(); $current_version = $this->get_current_version(); diff --git a/bin/update-translations.php b/bin/update-translations.php index 81526708..e66ae40e 100644 --- a/bin/update-translations.php +++ b/bin/update-translations.php @@ -12,9 +12,6 @@ // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped // phpcs:disable WordPress.WP.AlternativeFunctions -// Ensure we're in the right directory. -chdir( dirname( dirname( __DIR__ ) ) ); - /** * Handles the translation update process for Secure Custom Fields. */