New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new PHP format #1626
Add new PHP format #1626
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before reviewing this PR, I reviewed the existing format headers.
- Android. XML file (in a XML comment):
<!--
Translation-Revision-Date: 2023-09-26 13:29:05+0000
Plural-Forms: nplurals=2; plural=n != 1;
Generator: GlotPress/4.0.0-alpha.11
Language: gl_ES
-->
- Jed 1.x. JSON file:
{
"translation-revision-date": "2023-09-26 13:29:05+0000",
"generator": "GlotPress/4.0.0-alpha.11",
"domain": "messages",
"locale_data": {
"messages": {
"": {
"domain": "messages",
"plural-forms": "nplurals=2; plural=n != 1;",
"lang": "gl_ES"
},
- JSON format. JSON file. Doesn't have any header.
- NGX-Translate. JSON file. Doesn't have any header.
- Portable Object Message Catalog (.po/.pot)
"PO-Revision-Date: 2023-09-26 13:29:05+0000\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
"X-Generator: GlotPress/4.0.0-alpha.11\n"
"Language: gl_ES\n"
"Project-Id-Version: Plugins - Elementor\n"
- Java Properties File (.properties). Comments, starting the line with #.
# Translation-Revision-Date: 2023-09-26 13:29:05+0000
# Plural-Forms: nplurals=2; plural=n != 1;
# Generator: GlotPress/4.0.0-alpha.11
# Language: gl_ES
- .NET Resource (.resx).
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="translation_revision_date">
<value>2023-09-26 13:29:05+0000</value>
</resheader>
<resheader name="plural_forms">
<value>nplurals=2; plural=n != 1;</value>
</resheader>
<resheader name="generator">
<value>GlotPress/4.0.0-alpha.11</value>
</resheader>
<resheader name="language">
<value>gl_ES</value>
</resheader>
- Mac OS X / iOS Strings File (.strings)
/* Translation-Revision-Date: 2023-09-26 13:29:05+0000 */
/* Plural-Forms: nplurals=2; plural=n != 1; */
/* Generator: GlotPress/4.0.0-alpha.11 */
/* Language: gl_ES */
New format. Headers
'x-generator' => 'GlotPress/4.0.0-alpha.11',
'translation-revision-date' => '2023-09-29 17:24:57+0000',
'plural-forms' => 'nplurals=2; plural=n != 1;',
I will propose these small improvements in the header:
- Add the
language
item. E.g.'language' => 'gl_ES'
. - Add
project-id-version
item to be able to easily identify the project related with this translation file. E.g.'project-id-version' => Plugins - Elementor
.
Importing new files (.php extension)
If a user tries to import a .php file with this new format, she gets an error with the text "Couldn't load translations from file!", because this PR didn't have this code implemented.
I suggest an improvement, filtering this type of import file in the dropdown element, so the used won't see any PHP format to import.
In the gp-templates/project-import.php
I suggest this improvement:
Replace this code:
foreach ( GP::$formats as $slug => $format ) {
with
$formats = GP::$formats;
unset( $formats['php'] ); // Remove PHP format from the list of formats until we have an import process.
foreach ( $formats as $slug => $format ) {
Testing the functionality
To test this functionality:
- In a local GlotPress, I created a tree structure with a
Plugins
project where I store all plugins: Projects -> Plugins -> Elementor. - I downloaded the .po file with the translations from the w.org site for Elementor in Galician language and imported this file.
- I created the .php file with the translations and change the name to the same used by the Performant Translations plugin:
elementor-gl_ES.mo.php
.
In a new WordPress installation:
- I remove all the plugins.
- I installed the Performant Translations plugin, to be able to use it with WordPress, as a regular translation format.
- I installed the Query Monitor plugin, to be able to see the loaded translations.
- I installed the Elementor, to review its translations.
- I overwrite the .php file (elementor-gl_ES.mo.php) in the
wp-content/languages/plugins
folder from GlotPress. - I checked the translations in the Elementor backend and in the Query Monitor (to see the file used) and it works fine.
Good point! I also updated
Done 👍 |
Co-authored-by: Jesús Amieiro Becerra <1667814+amieiro@users.noreply.github.com>
Co-authored-by: Jesús Amieiro Becerra <1667814+amieiro@users.noreply.github.com>
What?
This adds a new
.php
output format for translations.Note
Import is not yet implemented. It requires something like PHP-Parser to do that safely, as you don't want to include/require a random user-provided PHP file. This is best done in a separate PR.
Why?
The WordPress core performance team is working on a Performant Translations feature plugin that uses a new approach to handle translation files in WordPress, making localization blazing fast. This plugin helps to make localized WordPress sites faster by replacing the traditional MO translation files with PHP files, which are much faster to parse.
Learn more at https://make.wordpress.org/core/2023/09/05/call-for-testing-performant-translations/
Ideally, these PHP files are served directly from translate.wordpress.org, which is why support needs to be added to GlotPress first.
See https://meta.trac.wordpress.org/ticket/7296 for enabling that on the dotorg side of things.
How?
Translations are exported using an optimized version of
var_export()
to use short array notation. A polyfill forarray_is_list
is included. Both are used to keep the file size as small as possible.See https://make.wordpress.org/core/2023/09/05/call-for-testing-performant-translations/ and https://github.com/swissspidy/performant-translations
Testing Instructions
Screenshots or screencast