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

Interactivity Router: Use script data passing for translated strings #65129

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
18 changes: 17 additions & 1 deletion lib/interactivity-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,21 @@
$default_version
);
}

add_action( 'init', 'gutenberg_reregister_interactivity_script_modules' );

function gutenberg_register_interactivity_script_module_data_hooks() {
if ( ! has_filter( 'script_module_data_@wordpress/interactivity-router', array( wp_interactivity(), 'filter_script_module_interactivity_router_data' ) ) ) {

Check failure on line 34 in lib/interactivity-api.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected 1 tabs, found 2
add_filter(
'script_module_data_@wordpress/interactivity-router',
function ( $data ) {
if ( ! isset( $data['i18n'] ) ) {

Check failure on line 38 in lib/interactivity-api.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected 4 tabs, found 5
$data['i18n'] = array();
}

Check failure on line 40 in lib/interactivity-api.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected 4 tabs, found 5
$data['i18n']['loading'] = __( 'Loading page, please wait.', 'gutenberg' );
$data['i18n']['loaded'] = __( 'Page Loaded.', 'gutenberg' );
return $data;
}

Check failure on line 44 in lib/interactivity-api.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected 3 tabs, found 4
);
}

Check failure on line 46 in lib/interactivity-api.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected 1 tabs, found 2
}
add_action( 'after_setup_theme', 'gutenberg_register_interactivity_script_module_data_hooks', 20 );
29 changes: 22 additions & 7 deletions packages/interactivity-router/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,31 @@ const isValidEvent = ( event: MouseEvent ) =>
// Variable to store the current navigation.
let navigatingTo = '';

const navigationTexts = {
loading: 'Loading page, please wait.',
loaded: 'Page Loaded.',
};
try {
const content = document.getElementById(
'wp-script-module-data-@wordpress/interactivity-router'
)?.textContent;
if ( content ) {
const parsed = JSON.parse( content );
if ( typeof parsed?.i18n?.loading === 'string' ) {
navigationTexts.loading = parsed.i18n.loading;
}
if ( typeof parsed?.i18n?.loaded === 'string' ) {
navigationTexts.loaded = parsed.i18n.loaded;
}
}
} catch {}

export const { state, actions } = store( 'core/router', {
state: {
url: window.location.href,
navigation: {
hasStarted: false,
hasFinished: false,
texts: {
loading: '',
loaded: '',
},
message: '',
},
},
Expand Down Expand Up @@ -275,7 +290,7 @@ export const { state, actions } = store( 'core/router', {
navigation.hasFinished = false;
}
if ( screenReaderAnnouncement ) {
navigation.message = navigation.texts.loading;
navigation.message = navigationTexts.loading;
}
}, 400 );

Expand Down Expand Up @@ -319,8 +334,8 @@ export const { state, actions } = store( 'core/router', {
// same, we use a no-break space similar to the @wordpress/a11y
// package: https://github.com/WordPress/gutenberg/blob/c395242b8e6ee20f8b06c199e4fc2920d7018af1/packages/a11y/src/filter-message.js#L20-L26
navigation.message =
navigation.texts.loaded +
( navigation.message === navigation.texts.loaded
navigationTexts.loaded +
( navigation.message === navigationTexts.loaded
? '\u00A0'
: '' );
}
Expand Down
Loading