Skip to content

Commit 07c72a3

Browse files
authored
Merge pull request #171 from wp-cli/copilot/fix-language-installation-status
2 parents 54e3f64 + dfdb648 commit 07c72a3

3 files changed

Lines changed: 101 additions & 2 deletions

File tree

features/language-plugin.feature

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,3 +444,41 @@ Feature: Manage plugin translation files for a WordPress install
444444
| akismet | en_US | active |
445445
| akismet | nl_NL | installed |
446446
And STDERR should be empty
447+
448+
@require-wp-4.0
449+
Scenario: Handle plugins with text domain different from slug
450+
Given a WP install
451+
And an empty cache
452+
453+
# Create a test plugin with a different text domain
454+
And a wp-content/plugins/test-plugin/test-plugin.php file:
455+
"""
456+
<?php
457+
/**
458+
* Plugin Name: Test Plugin
459+
* Text Domain: different-text-domain
460+
* Domain Path: /languages
461+
*/
462+
"""
463+
464+
# Manually create a translation file using the text domain (not the plugin slug)
465+
And a wp-content/languages/plugins/different-text-domain-de_DE.l10n.php file:
466+
"""
467+
"""
468+
And a wp-content/languages/plugins/different-text-domain-de_DE.mo file:
469+
"""
470+
"""
471+
And a wp-content/languages/plugins/different-text-domain-de_DE.po file:
472+
"""
473+
"""
474+
475+
When I run `wp language plugin list test-plugin --fields=language,status --format=csv`
476+
Then STDOUT should contain:
477+
"""
478+
en_US,active
479+
"""
480+
And STDERR should be empty
481+
482+
# If the fix is working, installed languages should be detected via text domain
483+
When I run `wp language plugin is-installed test-plugin de_DE`
484+
Then the return code should be 0

features/language-theme.feature

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,39 @@ Feature: Manage theme translation files for a WordPress install
328328
| en_US | active |
329329
| nl_NL | installed |
330330
And STDERR should be empty
331+
332+
@require-wp-4.0
333+
Scenario: Handle themes with text domain different from slug
334+
Given a WP install
335+
And an empty cache
336+
337+
# Create a test theme with a different text domain
338+
And a wp-content/themes/test-theme/style.css file:
339+
"""
340+
/*
341+
Theme Name: Test Theme
342+
Text Domain: different-text-domain
343+
*/
344+
"""
345+
346+
# Manually create a translation file using the text domain (not the theme slug)
347+
And a wp-content/languages/themes/different-text-domain-de_DE.l10n.php file:
348+
"""
349+
"""
350+
And a wp-content/languages/themes/different-text-domain-de_DE.mo file:
351+
"""
352+
"""
353+
And a wp-content/languages/themes/different-text-domain-de_DE.po file:
354+
"""
355+
"""
356+
357+
When I run `wp language theme list test-theme --fields=language,status --format=csv`
358+
Then STDOUT should contain:
359+
"""
360+
en_US,active
361+
"""
362+
And STDERR should be empty
363+
364+
# If the fix is working, installed languages should be detected via text domain
365+
When I run `wp language theme is-installed test-theme de_DE`
366+
Then the return code should be 0

src/WP_CLI/CommandWithTranslation.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,33 @@ protected function get_installed_languages( $slug = 'default' ) {
283283
/**
284284
* @var array<string, array<string, array<string, mixed>>> $available
285285
*/
286-
$available = wp_get_installed_translations( $this->obj_type );
287-
$available = ! empty( $available[ $slug ] ) ? array_keys( $available[ $slug ] ) : array();
286+
$available = wp_get_installed_translations( $this->obj_type );
287+
288+
// For plugins and themes, check if the text domain differs from the slug.
289+
$text_domain = $slug;
290+
if ( 'default' !== $slug ) {
291+
if ( 'plugins' === $this->obj_type ) {
292+
$plugins = get_plugins( '/' . $slug );
293+
if ( ! empty( $plugins ) ) {
294+
$plugin_data = array_shift( $plugins );
295+
// Use the TextDomain header if available, otherwise fall back to slug.
296+
if ( ! empty( $plugin_data['TextDomain'] ) ) {
297+
$text_domain = $plugin_data['TextDomain'];
298+
}
299+
}
300+
} elseif ( 'themes' === $this->obj_type ) {
301+
$theme_data = wp_get_theme( $slug );
302+
if ( $theme_data->exists() ) {
303+
// Use the TextDomain property if available, otherwise fall back to slug.
304+
$theme_text_domain = $theme_data->get( 'TextDomain' );
305+
if ( ! empty( $theme_text_domain ) ) {
306+
$text_domain = $theme_text_domain;
307+
}
308+
}
309+
}
310+
}
311+
312+
$available = ! empty( $available[ $text_domain ] ) ? array_keys( $available[ $text_domain ] ) : array();
288313
$available[] = 'en_US';
289314

290315
return $available;

0 commit comments

Comments
 (0)