From 82581c55916156eeb1963638ac5b62165060bd3b Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 1 Mar 2019 22:08:52 +0100 Subject: [PATCH] Bring i18n functionality up to date (#12559) * Plugin: Add Text Domain to plugin headers * Bring i18n functionality up to date * Plugin: Set Gutenberg script translations as default domain Filter loading behavior to load from plugin translation files * Testing: Update gutenberg_override_script per localization changes * Plugin: Provide second argument for set script translations Technically optional, only after WP5.1+, which is newer than the current minimum version supported by Gutenberg --- .gitignore | 2 - babel.config.js | 12 --- bin/build-plugin-zip.sh | 4 - .../backward-compatibility/deprecations.md | 6 ++ gutenberg.php | 1 + languages/README.md | 10 --- lib/client-assets.php | 74 +++++++++++++++++-- lib/i18n.php | 11 ++- phpcs.xml.dist | 1 - phpunit/class-i18n-functions-test.php | 32 -------- phpunit/class-override-script-test.php | 21 +++++- 11 files changed, 97 insertions(+), 77 deletions(-) delete mode 100644 languages/README.md delete mode 100644 phpunit/class-i18n-functions-test.php diff --git a/.gitignore b/.gitignore index 5ba04948b2faf..2fb814f9616e0 100644 --- a/.gitignore +++ b/.gitignore @@ -4,8 +4,6 @@ build-module build-style node_modules gutenberg.zip -languages/gutenberg.pot -/languages/gutenberg-translations.php # Directories/files that may appear in your environment .DS_Store diff --git a/babel.config.js b/babel.config.js index 4dc16df8337b2..b56ad5b149478 100644 --- a/babel.config.js +++ b/babel.config.js @@ -3,17 +3,5 @@ module.exports = function( api ) { return { presets: [ '@wordpress/babel-preset-default' ], - env: { - production: { - plugins: [ - [ - '@wordpress/babel-plugin-makepot', - { - output: 'languages/gutenberg.pot', - }, - ], - ], - }, - }, }; }; diff --git a/bin/build-plugin-zip.sh b/bin/build-plugin-zip.sh index a95f2ef74b23d..1a6344fa2e67a 100755 --- a/bin/build-plugin-zip.sh +++ b/bin/build-plugin-zip.sh @@ -97,8 +97,6 @@ status "Installing dependencies... 📦" npm install status "Generating build... 👷‍♀️" npm run build -status "Generating PHP file for wordpress.org to parse translations... 👷‍♂️" -npx pot-to-php ./languages/gutenberg.pot ./languages/gutenberg-translations.php gutenberg # Temporarily modify `gutenberg.php` with production constants defined. Use a # temp file because `bin/generate-gutenberg-php.php` reads from `gutenberg.php` @@ -118,8 +116,6 @@ zip -r gutenberg.zip \ post-content.php \ $vendor_scripts \ $build_files \ - languages/gutenberg.pot \ - languages/gutenberg-translations.php \ README.md # Reset `gutenberg.php`. diff --git a/docs/designers-developers/developers/backward-compatibility/deprecations.md b/docs/designers-developers/developers/backward-compatibility/deprecations.md index 966b33d77cafe..8e8f8ee2d3c37 100644 --- a/docs/designers-developers/developers/backward-compatibility/deprecations.md +++ b/docs/designers-developers/developers/backward-compatibility/deprecations.md @@ -2,6 +2,12 @@ The Gutenberg project's deprecation policy is intended to support backward compatibility for releases, when possible. The current deprecations are listed below and are grouped by _the version at which they will be removed completely_. If your plugin depends on these behaviors, you must update to the recommended alternative before the noted version. +## 5.4.0 + +- The PHP function `gutenberg_load_plugin_textdomain` has been removed. +- The PHP function `gutenberg_get_jed_locale_data` has been removed. +- The PHP function `gutenberg_load_locale_data` has been removed. + ## 5.3.0 - The PHP function `gutenberg_redirect_to_classic_editor_when_saving_posts` has been removed. diff --git a/gutenberg.php b/gutenberg.php index db7f70d1cab95..993f9e4566834 100644 --- a/gutenberg.php +++ b/gutenberg.php @@ -5,6 +5,7 @@ * Description: Printing since 1440. This is the development plugin for the new block editor in core. * Version: 5.1.1 * Author: Gutenberg Team + * Text Domain: gutenberg * * @package gutenberg */ diff --git a/languages/README.md b/languages/README.md deleted file mode 100644 index 3f3ba1d4478c6..0000000000000 --- a/languages/README.md +++ /dev/null @@ -1,10 +0,0 @@ -Languages -========= - -The generated POT template file is not included in this repository. To create this file locally, follow instructions from [CONTRIBUTING.md](https://github.com/WordPress/gutenberg/blob/master/CONTRIBUTING.md) to install the project, then run the following command: - -``` -npm run build -``` - -After the build completes, you'll find a `gutenberg.pot` strings file in this directory. diff --git a/lib/client-assets.php b/lib/client-assets.php index 9bd7cdb8be025..040557e3cfb11 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -83,8 +83,71 @@ function gutenberg_override_script( $handle, $src, $deps = array(), $ver = false } else { wp_register_script( $handle, $src, $deps, $ver, $in_footer ); } + + /* + * `WP_Dependencies::set_translations` will fall over on itself if setting + * translations on the `wp-i18n` handle, since it internally adds `wp-i18n` + * as a dependency of itself, exhausting memory. The same applies for the + * polyfill script, which is a dependency _of_ `wp-i18n`. + * + * See: https://core.trac.wordpress.org/ticket/46089 + */ + if ( 'wp-i18n' !== $handle && 'wp-polyfill' !== $handle ) { + wp_set_script_translations( $handle, 'default' ); + } } +/** + * Filters the default translation file load behavior to load the Gutenberg + * plugin translation file, if available. + * + * @param string|false $file Path to the translation file to load. False if + * there isn't one. + * @param string $handle Name of the script to register a translation + * domain to. + * + * @return string|false Filtered path to the Gutenberg translation file, if + * available. + */ +function gutenberg_override_translation_file( $file, $handle ) { + if ( ! $file ) { + return $file; + } + + // Only override script handles generated from the Gutenberg plugin. + $packages_dependencies = include dirname( __FILE__ ) . '/packages-dependencies.php'; + if ( ! isset( $packages_dependencies[ $handle ] ) ) { + return $file; + } + + /* + * The default file will be in the plugins language directory, omitting the + * domain since Gutenberg assigns the script translations as the default. + * + * Example: /www/wp-content/languages/plugins/de_DE-07d88e6a803e01276b9bfcc1203e862e.json + * + * The logic of `load_script_textdomain` is such that it will assume to + * search in the plugins language directory, since the assigned source of + * the overridden Gutenberg script originates in the plugins directory. + * + * The plugin translation files each begin with the slug of the plugin, so + * it's a simple matter of prepending the Gutenberg plugin slug. + */ + $path_parts = pathinfo( $file ); + $plugin_translation_file = ( + $path_parts['dirname'] . + '/gutenberg-' . + $path_parts['basename'] + ); + + if ( ! is_readable( $plugin_translation_file ) ) { + return $file; + } + + return $plugin_translation_file; +} +add_filter( 'load_script_translation_file', 'gutenberg_override_translation_file', 10, 2 ); + /** * Registers a style according to `wp_register_style`. Honors this request by * deregistering any style by the same handler before registration. @@ -485,14 +548,11 @@ function gutenberg_get_autosave_newer_than_post_save( $post ) { /** * Loads Gutenberg Locale Data. + * + * @deprecated 5.2.0 */ function gutenberg_load_locale_data() { - // Prepare Jed locale data. - $locale_data = gutenberg_get_jed_locale_data( 'gutenberg' ); - wp_add_inline_script( - 'wp-i18n', - 'wp.i18n.setLocaleData( ' . json_encode( $locale_data ) . ' );' - ); + _deprecated_function( __FUNCTION__, '5.2.0' ); } /** @@ -680,8 +740,6 @@ function gutenberg_editor_scripts_and_styles( $hook ) { $initial_edits = null; } - gutenberg_load_locale_data(); - // Preload server-registered block schemas. wp_add_inline_script( 'wp-blocks', diff --git a/lib/i18n.php b/lib/i18n.php index ab347c8334285..72aadb0598f27 100644 --- a/lib/i18n.php +++ b/lib/i18n.php @@ -13,12 +13,15 @@ * Returns Jed-formatted localization data. * * @since 0.1.0 + * @deprecated 5.2.0 * * @param string $domain Translation domain. * * @return array */ function gutenberg_get_jed_locale_data( $domain ) { + _deprecated_function( __FUNCTION__, '5.2.0' ); + $translations = get_translations_for_domain( $domain ); $locale = array( @@ -43,12 +46,8 @@ function gutenberg_get_jed_locale_data( $domain ) { * Load plugin text domain for translations. * * @since 0.1.0 + * @deprecated 5.2.0 */ function gutenberg_load_plugin_textdomain() { - load_plugin_textdomain( - 'gutenberg', - false, - plugin_basename( gutenberg_dir_path() ) . '/languages/' - ); + _deprecated_function( __FUNCTION__, '5.2.0' ); } -add_action( 'plugins_loaded', 'gutenberg_load_plugin_textdomain' ); diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 85a39fbd82a4a..e328ac8dde3fc 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -32,7 +32,6 @@ ./vendor - ./languages/gutenberg-translations.php ./packages/block-serialization-spec-parser/parser.php diff --git a/phpunit/class-i18n-functions-test.php b/phpunit/class-i18n-functions-test.php deleted file mode 100644 index db8abe35bb7a7..0000000000000 --- a/phpunit/class-i18n-functions-test.php +++ /dev/null @@ -1,32 +0,0 @@ -markTestSkipped( 'The path is not matching in docker test environment. Needs further investigation.' ); - return; - } - - add_action( 'load_textdomain', array( $this, 'check_arguments_in_load_textdomaincheck' ), 10, 2 ); - gutenberg_load_plugin_textdomain(); - } - - public function check_arguments_in_load_textdomaincheck( $domain, $mofile ) { - $content_language_file_pattern = 'languages[\\/]plugins[\\/]gutenberg-[a-z]{2}_[A-Z]{2}.mo$'; - $plugin_language_file_pattern = 'gutenberg[\\/]languages[\\/]gutenberg-[a-z]{2}_[A-Z]{2}.mo$'; - $regex_pattern = '#' . $content_language_file_pattern . '|' . $plugin_language_file_pattern . '#'; - - $this->assertEquals( 'gutenberg', $domain ); - $this->assertRegExp( $regex_pattern, $mofile ); - } -} diff --git a/phpunit/class-override-script-test.php b/phpunit/class-override-script-test.php index af9e5d73fff19..034fa2f25a618 100644 --- a/phpunit/class-override-script-test.php +++ b/phpunit/class-override-script-test.php @@ -24,6 +24,23 @@ function tearDown() { wp_deregister_script( 'gutenberg-dummy-script' ); } + /** + * Tests that script is localized. + */ + function test_localizes_script() { + gutenberg_override_script( + 'gutenberg-dummy-script', + 'https://example.com/', + array( 'dependency' ), + 'version', + false + ); + + global $wp_scripts; + $script = $wp_scripts->query( 'gutenberg-dummy-script', 'registered' ); + $this->assertEquals( array( 'dependency', 'wp-i18n' ), $script->deps ); + } + /** * Tests that script properties are overridden. */ @@ -39,7 +56,7 @@ function test_replaces_registered_properties() { global $wp_scripts; $script = $wp_scripts->query( 'gutenberg-dummy-script', 'registered' ); $this->assertEquals( 'https://example.com/updated', $script->src ); - $this->assertEquals( array( 'updated-dependency' ), $script->deps ); + $this->assertEquals( array( 'updated-dependency', 'wp-i18n' ), $script->deps ); $this->assertEquals( 'updated-version', $script->ver ); $this->assertEquals( 1, $script->extra['group'] ); } @@ -59,7 +76,7 @@ function test_registers_new_script() { global $wp_scripts; $script = $wp_scripts->query( 'gutenberg-second-dummy-script', 'registered' ); $this->assertEquals( 'https://example.com/', $script->src ); - $this->assertEquals( array( 'dependency' ), $script->deps ); + $this->assertEquals( array( 'dependency', 'wp-i18n' ), $script->deps ); $this->assertEquals( 'version', $script->ver ); $this->assertEquals( 1, $script->extra['group'] ); }