Language Switcher in WordPress | WordPress.org

[ad_1]

Hi Arthur,

WordPress doesn’t have a language switcher out of the box.

The multilingual feature is a part of the long-term roadmap of the new WordPress site editor outlined on

Since a plugin such as PolyLang isn’t an option, you can look into the WordPress Multisite feature. Basically, you can create a subsite for each language and manage the content separately.

Your PO/MO files should be available in the /wp-content/languages/ folder. When you select a language for a subsite, the files should be loaded automatically if the theme supports internationalization.

However, the language switcher will need to be integrated into your theme, which will require coding skills. Alternatively, you can review this plugin — it works with subsites.

Yes, I understand you, well, yes, I can do it all, but can I somehow do it so that when a person changes the language of the site, he is sent to the same page but in a different language ?, well, get_page_link () shows the url of the current page and to it need to add language slug

The problem is that in the first place PolyLang for some reason can’t find line breaks __ and _e, and I also optimized the theme (each css and js file is connected only to the page in which it is needed, so that it doesn’t happen that css is connected to the page but not used) and since PolyLang adds /ru/ for switching languages, the page slug will change and styles and js scripts simply do not work, do you know the solution to this problem? if so, I’m ready to work with PolyLang

if ( is_page( 'home','ru/home' ) ) { this option does not work

The simplest option is when you list the languages (the language switcher options), you can append the name of the particular reviewed page to each language URL.

For example, we have these sites added to the network:
– (English)
– (Spanish)

When someone opens an URL like , the switcher will use these URLs:
– English:
– Spanish:

You can get the page name using the get_post_field function. For example: get_post_field( 'name' );.

When the Spanish version is clicked, the user will be redirected to the Spanish version of the page. However, in that case, you’ll need to ensure that the page is added to each language. Otherwise, the 404 error page will appear.

There are some advanced options, but the proposed solution might work for you.

  • This reply was modified 52 minutes ago by aatanasov.

Regarding the CSS/JS optimization issue: I’m not sure how exactly the optimization script works, but it can be prepared to optimize the pages based on page names.

In our case, the name of a page will be the same for each language (if added correctly), and the CSS/JS code should be loaded correctly (based on page names, not on full URLs).

  • This reply was modified 46 minutes ago by aatanasov.
  • This reply was modified 45 minutes ago by aatanasov.

and what can you say about this ?, you can somehow make the css and js scripts connect and during the language switch from PolyLang, let me show you the full code so that you understand me well
i have code `if ( is_page( ‘Home’ ) ) {
wp_enqueue_style( ‘home-css’, get_template_directory_uri() . ‘/assets/css/home-css.css’, array(), ‘1.0’ );}`
and the problem is that when I use PolyLang, /ru is added to the URL during the Russian language and /en during the English language, and in this case, styles and scripts simply do not connect, well, css and js are connected for the url example.com/home scripts but for example.com/ru/home and example.com/en/home are not connected, how to make them connect during these slugs?

You won’t need to connect the pages.

If you add a page with a name home to each language. All languages will use the same name, and if ( is_page( 'home' ) ) will work for all subsites.

In that case, you won’t need to worry about the site’s language and URL (ru, etc.).

Please feel free to create a test WordPress multisite instance and experiment with the code you shared.

  • This reply was modified 28 minutes ago by aatanasov.
  • This reply was modified 27 minutes ago by aatanasov.

Yes, I understand this and your option will definitely work, but I want to understand is it possible to somehow merge on one site and not on a multisite?, if it would be possible to make scripts connect for a page with /ru and /en slugs, then it would be cool

Yes, you can use regex to match whether the URL includes a specific name. For example: about

Something like that:

if( preg_match( '/about/', get_permalink() ) ) {
	// wp_enqueue_style ....
}

This will work with and

The home scripts/styles can be loaded by default if there is no match.

I understood you well, it looks like I’m already very tired and I need to sleep, because I remembered this option exactly when I read your answer, in fact, I myself knew that this could be done, but I was so tired that I couldn’t solve the problem myself, thank you very much ,

You’re welcome. Please feel free to mark this issue as resolved if the solution works.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer