Skip to content

Commit ca7a44a

Browse files
authored
Merge pull request #379 from wp-cli/fix/php-format
2 parents 1bc07c3 + b1801bc commit ca7a44a

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

features/makephp.feature

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ Feature: Generate PHP files from PO files
126126
"""
127127
And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain:
128128
"""
129-
'messages'=>[''=>['Foo Plugin'=>['Foo Plugin']]]
129+
'messages'=>['Foo Plugin'=>'Foo Plugin']
130130
"""
131131

132132
Scenario: Does include translations
@@ -150,9 +150,20 @@ Feature: Generate PHP files from PO files
150150
"X-Domain: foo-plugin\n"
151151
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
152152
153+
#: foo-plugin.js:15
154+
msgctxt "Plugin Name"
155+
msgid "Foo Plugin (EN)"
156+
msgstr "Foo Plugin (DE)"
157+
153158
#: foo-plugin.js:15
154159
msgid "Foo Plugin"
155160
msgstr "Bar Plugin"
161+
162+
#: foo-plugin.php:60
163+
msgid "You have %d new message"
164+
msgid_plural "You have %d new messages"
165+
msgstr[0] "Sie haben %d neue Nachricht"
166+
msgstr[1] "Sie haben %d neue Nachrichten"
156167
"""
157168

158169
When I run `wp i18n make-php foo-plugin`
@@ -163,5 +174,5 @@ Feature: Generate PHP files from PO files
163174
And the return code should be 0
164175
And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain:
165176
"""
166-
'messages'=>[''=>['Foo Plugin'=>['Bar Plugin']]]
177+
return ['domain'=>'foo-plugin','plural-forms'=>'nplurals=2; plural=(n != 1);','messages'=>['Plugin NameFoo Plugin (EN)'=>'Foo Plugin (DE)','Foo Plugin'=>'Bar Plugin','You have %d new message'=>'Sie haben %d neue Nachricht' . "\0" . 'Sie haben %d neue Nachrichten'],'language'=>'de_DE'];
167178
"""

src/PhpArrayGenerator.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace WP_CLI\I18n;
44

55
use Gettext\Generators\PhpArray;
6+
use Gettext\Translation;
67
use Gettext\Translations;
78

89
/**
@@ -39,6 +40,67 @@ public static function toString( Translations $translations, array $options = []
3940
return '<?php' . PHP_EOL . 'return ' . static::var_export( $array ) . ';';
4041
}
4142

43+
/**
44+
* Generates an array with the translations.
45+
*
46+
* @param Translations $translations
47+
* @param array $options
48+
*
49+
* @return array
50+
*/
51+
public static function generate( Translations $translations, array $options = [] ) {
52+
$options += static::$options;
53+
54+
return static::toArray( $translations, $options['includeHeaders'] );
55+
}
56+
57+
/**
58+
* Returns a flat array.
59+
*
60+
* @param Translations $translations
61+
* @param bool $include_headers
62+
* @param bool $force_array Unused.
63+
*
64+
* @return array
65+
*/
66+
protected static function toArray( Translations $translations, $include_headers, $force_array = false ) {
67+
$messages = [];
68+
69+
if ( $include_headers ) {
70+
$messages[''] = [
71+
'' => [ static::generateHeaders( $translations ) ],
72+
];
73+
}
74+
75+
/**
76+
* @var Translation $translation
77+
*/
78+
foreach ( $translations as $translation ) {
79+
if ( $translation->isDisabled() ) {
80+
continue;
81+
}
82+
83+
$context = $translation->getContext();
84+
$original = $translation->getOriginal();
85+
86+
$key = $context ? $context . "\4" . $original : $original;
87+
88+
if ( $translation->hasPluralTranslations() ) {
89+
$msg_translations = $translation->getPluralTranslations();
90+
array_unshift( $msg_translations, $translation->getTranslation() );
91+
$messages[ $key ] = implode( "\0", $msg_translations );
92+
} else {
93+
$messages[ $key ] = $translation->getTranslation();
94+
}
95+
}
96+
97+
return [
98+
'domain' => $translations->getDomain(),
99+
'plural-forms' => $translations->getHeader( 'Plural-Forms' ),
100+
'messages' => $messages,
101+
];
102+
}
103+
42104
/**
43105
* Determines if the given array is a list.
44106
*

0 commit comments

Comments
 (0)