Skip to content

Commit

Permalink
Get the locale of current site and send it to lang-guess (#626)
Browse files Browse the repository at this point in the history
* Get the locale of the current site and send it to lang-guess API.
  • Loading branch information
renintw authored Jun 17, 2024
1 parent 5c6616a commit 2543908
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
17 changes: 17 additions & 0 deletions mu-plugins/blocks/language-suggest/language-suggest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,20 @@ function language_suggest_block_init() {
);
}
add_action( 'init', __NAMESPACE__ . '\language_suggest_block_init' );

function language_suggest_enqueue_scripts() {
wp_enqueue_script(
'language-suggest-front',
plugins_url( 'src/front.js', __FILE__ ),
array(),
null,
true
);

wp_add_inline_script(
'language-suggest-front',
'var languageSuggestData = ' . wp_json_encode( array( 'locale' => get_locale() ) ) . ';',
'before'
);
}
add_action( 'plugins_loaded', __NAMESPACE__ . '\language_suggest_enqueue_scripts' );
11 changes: 7 additions & 4 deletions mu-plugins/blocks/language-suggest/src/front.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
/* global languageSuggestData */

const init = () => {
const container = document.querySelector( '.wp-block-wporg-language-suggest' );

if ( ! container ) {
return;
}

const endpoint =
container.dataset.endpoint ||
'https://wordpress.org/lang-guess/lang-guess-ajax.php?uri=' +
encodeURIComponent( window.location.pathname );
const endpoint = new URL(
container.dataset.endpoint || 'https://wordpress.org/lang-guess/lang-guess-ajax.php'
);
endpoint.searchParams.set( 'uri', encodeURIComponent( window.location.pathname ) );
endpoint.searchParams.set( 'locale', languageSuggestData.locale );

fetch( endpoint )
.then( ( response ) => {
Expand Down

0 comments on commit 2543908

Please sign in to comment.