Skip to content
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

Merged
merged 23 commits into from Dec 28, 2023
Merged

Add new PHP format #1626

merged 23 commits into from Dec 28, 2023

Conversation

swissspidy
Copy link
Member

@swissspidy swissspidy commented May 10, 2023

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 for array_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

  1. Export translations as PHP
  2. Check the syntax to see it's valid
  3. Include/require that file in PHP, ideally in WP using that feature plugin.

Screenshots or screencast

@swissspidy swissspidy added the [Type] Enhancement A suggestion for improvement. label Sep 22, 2023
@swissspidy swissspidy marked this pull request as ready for review September 22, 2023 19:12
@swissspidy swissspidy changed the title [POC] Add new PHP format Add new PHP format Sep 22, 2023
Copy link
Member

@amieiro amieiro left a 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.

image

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 ) {

image

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.

@akirk akirk added this to the 4.0 milestone Dec 20, 2023
@swissspidy
Copy link
Member Author

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.

Good point! I also updated gp_get_import_file_format() accordingly so that you'll get a better error message when choosing "Auto" for the import.

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.

Done 👍

@amieiro amieiro self-requested a review December 22, 2023 12:31
gp-includes/formats/format-php.php Outdated Show resolved Hide resolved
gp-includes/formats/format-php.php Outdated Show resolved Hide resolved
gp-includes/formats/format-php.php Show resolved Hide resolved
Co-authored-by: Jesús Amieiro Becerra <1667814+amieiro@users.noreply.github.com>
gp-includes/formats/format-php.php Outdated Show resolved Hide resolved
gp-includes/formats/format-php.php Show resolved Hide resolved
swissspidy and others added 4 commits December 22, 2023 18:53
Co-authored-by: Jesús Amieiro Becerra <1667814+amieiro@users.noreply.github.com>
@amieiro amieiro self-requested a review December 28, 2023 15:49
@amieiro amieiro merged commit f9eb8ae into develop Dec 28, 2023
6 checks passed
@amieiro amieiro deleted the try/format-php branch December 28, 2023 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants