Skip to content
Merged
38 changes: 38 additions & 0 deletions features/language-plugin.feature
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,41 @@ Feature: Manage plugin translation files for a WordPress install
| akismet | en_US | active |
| akismet | nl_NL | installed |
And STDERR should be empty

@require-wp-4.0
Scenario: Handle plugins with text domain different from slug
Given a WP install
And an empty cache

# Create a test plugin with a different text domain
And a wp-content/plugins/test-plugin/test-plugin.php file:
"""
<?php
/**
* Plugin Name: Test Plugin
* Text Domain: different-text-domain
* Domain Path: /languages
*/
"""

# Manually create a translation file using the text domain (not the plugin slug)
And a wp-content/languages/plugins/different-text-domain-de_DE.l10n.php file:
"""
"""
And a wp-content/languages/plugins/different-text-domain-de_DE.mo file:
"""
"""
And a wp-content/languages/plugins/different-text-domain-de_DE.po file:
"""
"""

When I run `wp language plugin list test-plugin --fields=language,status --format=csv`
Then STDOUT should contain:
"""
en_US,active
"""
And STDERR should be empty

# If the fix is working, installed languages should be detected via text domain
When I run `wp language plugin is-installed test-plugin de_DE`
Then the return code should be 0
36 changes: 36 additions & 0 deletions features/language-theme.feature
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,39 @@ Feature: Manage theme translation files for a WordPress install
| en_US | active |
| nl_NL | installed |
And STDERR should be empty

@require-wp-4.0
Scenario: Handle themes with text domain different from slug
Given a WP install
And an empty cache

# Create a test theme with a different text domain
And a wp-content/themes/test-theme/style.css file:
"""
/*
Theme Name: Test Theme
Text Domain: different-text-domain
*/
"""

# Manually create a translation file using the text domain (not the theme slug)
And a wp-content/languages/themes/different-text-domain-de_DE.l10n.php file:
"""
"""
And a wp-content/languages/themes/different-text-domain-de_DE.mo file:
"""
"""
And a wp-content/languages/themes/different-text-domain-de_DE.po file:
"""
"""

When I run `wp language theme list test-theme --fields=language,status --format=csv`
Then STDOUT should contain:
"""
en_US,active
"""
And STDERR should be empty

# If the fix is working, installed languages should be detected via text domain
When I run `wp language theme is-installed test-theme de_DE`
Then the return code should be 0
29 changes: 27 additions & 2 deletions src/WP_CLI/CommandWithTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,33 @@ protected function get_installed_languages( $slug = 'default' ) {
/**
* @var array<string, array<string, array<string, mixed>>> $available
*/
$available = wp_get_installed_translations( $this->obj_type );
$available = ! empty( $available[ $slug ] ) ? array_keys( $available[ $slug ] ) : array();
$available = wp_get_installed_translations( $this->obj_type );

// For plugins and themes, check if the text domain differs from the slug.
$text_domain = $slug;
if ( 'default' !== $slug ) {
if ( 'plugins' === $this->obj_type ) {
$plugins = get_plugins( '/' . $slug );
if ( ! empty( $plugins ) ) {
$plugin_data = array_shift( $plugins );
// Use the TextDomain header if available, otherwise fall back to slug.
if ( ! empty( $plugin_data['TextDomain'] ) ) {
$text_domain = $plugin_data['TextDomain'];
}
}
} elseif ( 'themes' === $this->obj_type ) {
$theme_data = wp_get_theme( $slug );
if ( $theme_data->exists() ) {
// Use the TextDomain property if available, otherwise fall back to slug.
$theme_text_domain = $theme_data->get( 'TextDomain' );
if ( ! empty( $theme_text_domain ) ) {
$text_domain = $theme_text_domain;
}
}
}
}

$available = ! empty( $available[ $text_domain ] ) ? array_keys( $available[ $text_domain ] ) : array();
$available[] = 'en_US';

return $available;
Expand Down