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

Bring i18n functionality up to date #12559

Merged
merged 5 commits into from
Mar 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 0 additions & 12 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,5 @@ module.exports = function( api ) {

return {
presets: [ '@wordpress/babel-preset-default' ],
env: {
production: {
plugins: [
[
'@wordpress/babel-plugin-makepot',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguably I wonder if it's still necessary to include @wordpress/babel-plugin-makepot as a dependency in package.json, since it's no longer referenced at all anywhere in the project. But I think it's probably simpler / non-harmful to just have package.json include every single package we distribute.

{
output: 'languages/gutenberg.pot',
},
],
],
},
},
};
};
4 changes: 0 additions & 4 deletions bin/build-plugin-zip.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
10 changes: 0 additions & 10 deletions languages/README.md

This file was deleted.

74 changes: 66 additions & 8 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -506,14 +569,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' );
}

/**
Expand Down Expand Up @@ -701,8 +761,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',
Expand Down
11 changes: 5 additions & 6 deletions lib/i18n.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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' );
1 change: 0 additions & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
<exclude-pattern>./vendor</exclude-pattern>

<!-- Exclude generated files -->
<exclude-pattern>./languages/gutenberg-translations.php</exclude-pattern>
<exclude-pattern>./packages/block-serialization-spec-parser/parser.php</exclude-pattern>

<rule ref="PHPCompatibility.PHP.NewKeywords.t_namespaceFound">
Expand Down
32 changes: 0 additions & 32 deletions phpunit/class-i18n-functions-test.php

This file was deleted.

21 changes: 19 additions & 2 deletions phpunit/class-override-script-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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'] );
}
Expand All @@ -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'] );
}
Expand Down