Skip to content

Commit

Permalink
Update locale forwarding via path
Browse files Browse the repository at this point in the history
  • Loading branch information
sarayourfriend committed Feb 15, 2022
1 parent 8c36a0e commit dd98c6d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace WordPressdotorg\Openverse\Theme;

use function WordPressdotorg\Locales\get_locales;

/**
* This is the URL on which the frontend site of Openverse is hosted. Unless
* overridden from the Customizer UI, this is the URL for the embedded `iframe`.
Expand Down Expand Up @@ -83,11 +85,15 @@ function enqueue_assets() {
/* ver */ filemtime( __DIR__ . '/js/message.js' ),
/* in_footer */ true
);

$locale = get_locales()[ get_locale() ];
$locale_slug = $locale->slug;

wp_add_inline_script(
/* handle */ 'openverse-message',
/* JS */ 'const openverseUrl = ' . wp_json_encode( get_theme_mod( 'ov_src_url', OPENVERSE_URL ) ) . ";\n" .
/* JS */ 'const openverseSubpath = ' . wp_json_encode( OPENVERSE_SUBPATH ) . ";\n" .
/* JS */ 'const currentLocale = ' . wp_json_encode( get_locale() ) . ";\n",
/* JS */ 'const localeSlug = ' . wp_json_encode( $locale_slug ) . ";\n",
/* position */ 'before'
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ document.addEventListener('DOMContentLoaded', () => {
.toLocaleLowerCase()
.replace(openverseSubpath, '')
.replace(/^\/$/, ''); // Remove Openverse site subpath
iframePath = `${openverseUrl}${iframePath}${query}`; // Add domain and query
iframePath = `${openverseUrl}${localeSlug === 'en' ? '' : localeSlug}${iframePath}${query}`; // Add domain and query

console.log(`Navigating iframe to ${iframePath}`);
const iframe = document.getElementById('openverse_embed');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
function updatePath(value) {
const path = value.path;
const url = `${openverseSubpath}${path}`; // openverseSubpath defined in `index.php`
const url = `${openverseSubpath}${path}`.replace(`/${localeSlug}`, ''); // openverseSubpath defined in `index.php`

console.log(`Replacing state URL: ${url}`);
history.replaceState(
Expand Down

0 comments on commit dd98c6d

Please sign in to comment.