12/8/2022 0 Comments Wp language switcher plugin![]() ![]() Now that we have a way to store the language we choose, let’s use that to filter the WordPress locale using the method get_locale which is hooked to the filter pre_determine_locale. Once we are setting the cookie, we are also switching it directly just in case (although, that might be skipped since we filter it later). You can change that and the cookie name for your needs. We are setting the cookie my_language to 1 year. Saving the locale from WordPress Language Switcher In this method, we are returning the second language as hr which is the Croatian locale. If you want, this method can return directly the $_GET if it’s set and instead of using locale=en, you could use locale=en_US. Let’s now create a method that will check for the locale that we need from the $_GET global. The method get_locale will filter the locale before WordPress loads the default locale. The method set_locale will set the cookie for our language switcher if we have such query parameter. WP LANGUAGE SWITCHER PLUGIN CODEAdd this code to the wp-content/plugins/ folder. I’ll create a plugin since it’s easier to remove the code from running. ![]() You can add this code in your theme or as a separate plugin. Get value from the cookie if it exists and change the locale.Let’s now create the code behind the WordPress Language Switcher. How do you know which locale is correct? You can go to this page: and find out which locale you need. This will then, on any page, append ?locale query and load the same page with the changed language. Then, you can switch it back to English (if that was your default language). WP LANGUAGE SWITCHER PLUGIN DOWNLOADYour language (for example: Croatian with URL ?locale=hr)īe sure to go to Settings > General and select the second language so WordPress core can download their core translations as well.So, go to your menu builder in WordPress and create two menu custom links for this example: Due to that, you can create the switcher using the WordPress menu builder. Creating the Language Switcherįor this WordPress language switcher, we will use a simple flow. That way, we’ll skip everything else what WordPress does to provide the language for the site and return the language we want early in the process. This locale is used by WordPress core to determine a locale before it begins loading the locale from the settings. po/.mo files that WordPress will load on its own.īut, how will this be provided? We will use the filter pre_determine_locale. This plugin provides a simple way of translating text and create new. Translating Textįor translating the text with our WordPress Language Switcher I decided to use the Loco Translate plugin. This solution won’t be there to change slugs or URLs or anything like that, but if you have a solution for that, this should work as well since it changes the locale of WordPress. ![]() Since this project is an MVP, I wanted a simple solution for this without too much work. While I was working on a new project idea, I wanted to provide two languages: English and Croatian. There are already a lot of great WordPress plugins used for creating multilingual sites which incorporate their own language switcher. This tutorial is just a showcase on how something can be done. In this tutorial, we’ll create a WordPress Language Switcher which can work with any translation plugin. When working with Multilingual WordPress site, you’ll need a Language Switcher as well. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |