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

[EXPERIMENTAL] react-i18n: client-side locale switching #27973

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 6 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -1751,6 +1751,12 @@
"markdown_source": "../packages/project-management-automation/README.md",
"parent": "packages"
},
{
"title": "@wordpress/react-i18n",
"slug": "packages-react-i18n",
"markdown_source": "../packages/react-i18n/README.md",
"parent": "packages"
},
{
"title": "@wordpress/react-native-aztec",
"slug": "packages-react-native-aztec",
Expand Down
87 changes: 87 additions & 0 deletions lib/class-wp-rest-translations-controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* REST API: WP_REST_Translations_Controller class
*
* @package Gutenberg
*/

/**
* Core class representing a controller for translations.
*/
class WP_REST_Translations_Controller extends WP_REST_Controller {
/**
* Constructor.
*/
public function __construct() {
$this->namespace = '__experimental';
$this->rest_base = 'translations';
}

/**
* Registers the widget routes for the controller.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
$this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => array(
'locale' => array(
'description' => __( 'Locale', 'gutenberg' ),
'type' => 'string',
),
'handles' => array(
'description' => __( 'Script Handles', 'gutenberg' ),
'type' => 'array',
'required' => true,
'items' => array(
'type' => 'string',
),
),
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
),
),
'allow_batch' => array( 'v1' => true ),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}

public function get_item_permissions_check( $request ) {
return true;
}

public function get_item( $request ) {
$switched = $request['locale'] && switch_to_locale( $request['locale'] );

$result = array();

foreach( $request['handles'] as $handle ) {
if ( ! isset( wp_scripts()->registered[ $handle ] ) ) {
continue;
}

/* @var \_WP_Dependency $script */
$script = wp_scripts()->registered[ $handle ];

if( ! $script ) {
continue;
}

$translations = load_script_textdomain( $handle, $script->textdomain, $script->translations_path );
if ( $translations ) {
$result[ $handle ] = json_decode( $translations );
}
}

if( $switched ) {
restore_previous_locale();
}

return $result;
}
}
12 changes: 12 additions & 0 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ function gutenberg_override_script( $scripts, $handle, $src, $deps = array(), $v
*/
if ( 'wp-i18n' !== $handle && 'wp-polyfill' !== $handle ) {
$scripts->set_translations( $handle, 'default' );

if ( $script && 'wp-react-i18n' !== $handle ) {
// $script->deps[] = 'wp-react-i18n';

$data = <<<JS
( function( handle ) {
wp?.reactI18n?.addHandle( handle );
} )( "{$handle}" );
JS;

$scripts->add_inline_script( $handle, $data );
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ function gutenberg_is_experiment_enabled( $name ) {
if ( ! class_exists( 'WP_REST_Batch_Controller' ) ) {
require_once __DIR__ . '/class-wp-rest-batch-controller.php';
}
if ( ! class_exists( 'WP_REST_Translations_Controller' ) ) {
require_once __DIR__ . '/class-wp-rest-translations-controller.php';
}
/**
* End: Include for phase 2
*/
Expand Down
9 changes: 9 additions & 0 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ function gutenberg_register_batch_endpoint() {
}
add_action( 'rest_api_init', 'gutenberg_register_batch_endpoint' );

/**
* Registers the Translations REST API routes.
*/
function gutenberg_register_translations_endpoint() {
$translations = new WP_REST_Translations_Controller();
$translations->register_routes();
}
add_action( 'rest_api_init', 'gutenberg_register_translations_endpoint' );

/**
* Hook in to the nav menu item post type and enable a post type rest endpoint.
*
Expand Down
Loading