Skip to content

Commit 10d896b

Browse files
kraftbjpkevan
andauthored
Translations: add script to remove 90% translations (#110)
Co-authored-by: Paul Kevan <[email protected]>
1 parent bfdcf27 commit 10d896b

File tree

3 files changed

+144
-2
lines changed

3 files changed

+144
-2
lines changed

bin/prepare-release.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function run() {
4949
$this->build_assets();
5050
$this->run_tests();
5151
$this->generate_docs();
52+
$this->update_translations();
5253
$this->commit_changes();
5354

5455
$current_version = $this->get_current_version();
@@ -186,7 +187,7 @@ private function commit_changes() {
186187
if ( 0 !== $return ) {
187188
exit( $return );
188189
}
189-
passthru( 'git commit -m "Build assets and documentation"', $return );
190+
passthru( 'git commit -m "Build assets, documentation, and translations"', $return );
190191
if ( 0 !== $return ) {
191192
exit( $return );
192193
}
@@ -346,6 +347,15 @@ private function confirm( $message ) {
346347
fclose( $handle );
347348
return 'y' === $line;
348349
}
350+
351+
/**
352+
* Update plugin translations and cleanup
353+
*/
354+
private function update_translations() {
355+
require_once __DIR__ . '/update-translations.php';
356+
$updater = new Translation_Updater();
357+
$updater->run();
358+
}
349359
}
350360

351361
// Run the script

bin/update-translations.php

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#!/usr/bin/env php
2+
<?php
3+
/**
4+
* Update plugin translations
5+
*
6+
* @package wordpress/secure-custom-fields
7+
*/
8+
9+
namespace WordPress\SCF\Scripts;
10+
11+
// phpcs:disable WordPress.PHP.DiscouragedPHPFunctions
12+
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
13+
// phpcs:disable WordPress.WP.AlternativeFunctions
14+
15+
/**
16+
* Handles the translation update process for Secure Custom Fields.
17+
*/
18+
class Translation_Updater {
19+
/**
20+
* Plugin slug
21+
*
22+
* @var string
23+
*/
24+
private $plugin_slug = 'secure-custom-fields';
25+
26+
/**
27+
* Language directory
28+
*
29+
* @var string
30+
*/
31+
private $lang_dir;
32+
33+
/**
34+
* Constructor
35+
*/
36+
public function __construct() {
37+
$this->lang_dir = __DIR__ . '/../lang';
38+
}
39+
40+
/**
41+
* Run the translation update
42+
*/
43+
public function run() {
44+
if ( ! is_dir( $this->lang_dir ) ) {
45+
echo "Language directory not found: {$this->lang_dir}\n";
46+
return;
47+
}
48+
49+
// Get available translations from WordPress.org
50+
$translations = $this->get_available_translations();
51+
if ( empty( $translations ) ) {
52+
echo "No translations available from WordPress.org\n";
53+
return;
54+
}
55+
56+
// Get existing translation files
57+
$pattern = $this->lang_dir . '/' . $this->plugin_slug . '-*.po';
58+
$po_files = glob( $pattern );
59+
60+
if ( empty( $po_files ) ) {
61+
echo "No translation files found in {$this->lang_dir}\n";
62+
return;
63+
}
64+
65+
foreach ( $po_files as $po_file ) {
66+
$basename = basename( $po_file );
67+
$locale = str_replace( array( $this->plugin_slug . '-', '.po' ), '', $basename );
68+
69+
echo "→ Checking $locale...\n";
70+
71+
$po_path = $this->lang_dir . '/' . $this->plugin_slug . '-' . $locale . '.po';
72+
$mo_path = $this->lang_dir . '/' . $this->plugin_slug . '-' . $locale . '.mo';
73+
$l10n_path = $this->lang_dir . '/' . $this->plugin_slug . '-' . $locale . '.l10n.php';
74+
75+
// If translation is in WordPress.org list, delete local files
76+
if ( isset( $translations[ $locale ] ) ) {
77+
if ( file_exists( $po_path ) ) {
78+
unlink( $po_path );
79+
echo " ✓ Deleted .po file\n";
80+
}
81+
if ( file_exists( $mo_path ) ) {
82+
unlink( $mo_path );
83+
echo " ✓ Deleted .mo file\n";
84+
}
85+
if ( file_exists( $l10n_path ) ) {
86+
unlink( $l10n_path );
87+
echo " ✓ Deleted .l10n.php file\n";
88+
}
89+
echo " ✓ Translation available on WordPress.org\n\n";
90+
continue;
91+
}
92+
93+
echo " ⚠️ Translation not available on WordPress.org\n";
94+
}
95+
}
96+
97+
/**
98+
* Get available translations from WordPress.org
99+
*
100+
* @return array
101+
*/
102+
private function get_available_translations() {
103+
$api_url = sprintf(
104+
'https://api.wordpress.org/translations/plugins/1.0/?slug=%s',
105+
$this->plugin_slug
106+
);
107+
108+
$response = file_get_contents( $api_url );
109+
if ( false === $response ) {
110+
return array();
111+
}
112+
113+
$data = json_decode( $response, true );
114+
if ( ! isset( $data['translations'] ) ) {
115+
return array();
116+
}
117+
118+
$translations = array();
119+
foreach ( $data['translations'] as $translation ) {
120+
$translations[ $translation['language'] ] = array(
121+
'package' => $translation['package'],
122+
);
123+
}
124+
125+
return $translations;
126+
}
127+
}
128+
129+
// Run the script
130+
$updater = new Translation_Updater();
131+
$updater->run();

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
],
7070
"test:php": "phpunit",
7171
"test:phpstan": "phpstan analyze --memory-limit=2G",
72-
"test:phpstan:baseline": "phpstan analyze --memory-limit=2G --generate-baseline=bin/baseline.neon"
72+
"test:phpstan:baseline": "phpstan analyze --memory-limit=2G --generate-baseline=bin/baseline.neon",
73+
"update-translations": "php bin/update-translations.php"
7374
}
7475
}

0 commit comments

Comments
 (0)