I'm using __("english text", "textdomain") in a WordPress theme template.
The base language of the site is English.
I've successfully added a .pot file for German.
If I set the language in Settings, General to German, I get the German translations on the front end no problem.
I'm trying to implement a language selector, so on the front end the site visitor can select a language.
I thought it was just a case of setting the lang attribute in the html tag appropriately. So when the user selects German, I output <html lang="de-DE">. But still __() uses the language from the WordPress admin settings.
I guess I'm approaching this in the wrong way. Does __() only ever use the language set in WP Admin? Or can I force it to use a different translation file depending on what the visitor selects on the front end?
Thanks.
You might need to use the WordPress filter locale https://developer.wordpress.org/reference/hooks/locale/
You can do something like this in your function.php
//set desired locale
add_filter( 'locale', function($locale) {
if ( !is_admin() )
$locale = "de-DE";
return $locale;
});
Related
I am using a WP 6.0 with Polylang plugin. So there are english pages and german pages. I want to show or hide some block depending on a language of the page. I know about such built-in functions in WP like is_archive() or is_singular() or is_search() or is_404().
How can a function to check the language of a post or a page can look? I see that in the database in "postmeta" table there is a column "meta_key" that can content "_locale" value and a column "meta_value", that can be, im my case, "en_US" or "de_DE". How can i prepare something like is_en() or is_de() or lang_is() function?
P.S.: Maybe also table to perform this checking is not "postmeta", but "term_taxonomy", where we have columns "taxonomy" and "description"...
The get_locale() function returns the language of the currently viewed page:
https://developer.wordpress.org/reference/functions/get_locale/
like 'en_US' if you are on an english page.
I'm working on a website, which is multilingual.
We're using Polylang and the ACF custom fields plugin.
Works fine in general, the issue is with the ACF Option pages.
The option pages are translated also in different languages.
The content we are taking from there is being displayed according to translation - in english on the english version of a page, french on the french etc.
The problem: We have a contact us form, where we take the recipients email address from the ACF option pages.
(We want to send it to a different recepient when its a different language.)
Here it always takes the email address from the default language option page and I don't understand why.
We are taking the email recipient for the ajax call with get field command, like on pages displaying content:
get_field('service_email', 'option' );
Anyone got an idea what could cause this? Or where to look?
In the end we found the solution. It took a bit of digging, but I hope this helps if anyone encounters the same issue.
We needed to add the following setup in the functions.php of our theme to have the ACF options pages also translated for the each language:
// Translating Options Page Compatibility
// add filter with the path to your acf installation
add_filter('acf/settings/default_language', 'my_settings_default_language');
add_filter('acf/settings/current_language', 'my_settings_current_language');
function my_settings_default_language( $lang ) {
if($lang == "") {
$lang = pll_default_language(); // pll_ is a polylang function
}
return $lang;
}
function jfrog_settings_current_language( $lang ) {
$lang = pll_current_language();
return $lang;
}
Side Note: We're using a theme installed version of ACF.
hope this helps, Cheers
I have a question. I am using WPML plugin. In WPML->languages->Language switcher options i checked "Link to home of language for missing translations". Now when i click language switcher country flags and translation is missing it will redirect to homepage. My question is how to redirect to custom page when translation is not found. I want to create page with text "Sorry translation is missing. Please contact us for more info..."
Thank you for your time
First make sure you have WPML->languages->Language switcher options "Link to home of language for missing translations" checked. Then create your custom translation not found page in main language and translate it to other languages using WPML. Then add this code to your functions.php
add_filter('wpml_ls_language_url', 'redirect_link',10,2);
function redirect_link($url,$lang){
if($lang['missing'] == 1) {
$permalink_to_translation_not_found_page_in_main_language = get_the_permalink(40); //40 is page id of a custom translation not found page in main language
$lang['url'] = apply_filters( 'wpml_permalink', $permalink_to_translation_not_found_page_in_main_language , $lang['language_code'] );
}
return $lang['url'];
}
I hope this is useful for someone
I have this site :
example.com/watches (watches is a custom post type)
There are two available languages : EN(default language), GR.
When i change to GR (while on page example.com/watches)
it redirects to example.com/el/watches which is right.
When i am on example.com/watches/rolex and i try to change language it redirects to example.com/el/taxwatches/rolex-el but i want it just to be example.com/el/watches/rolex/.
taxwatches is my custom taxonomy.
rolex-el is the slug of the term inside that taxonomy.
Has anyone experienced an issue like this before?
I've tried to save the permalinks again and check my WMPL settings but i can't see something wrong.
EDIT 1 : If i manually go to example.com/el/watches/rolex it will work fine.
Both example.com/el/watches/rolex and example.com/el/taxwatches/rolex-el work.
EDIT 2: From what i understand WPML takes the slugs, is there a way to change it so it takes the names?
Ok so for anyone having problems with multi language sites - but want to have the "same slugs" in the urls (will never be same slugs - but going to look like that) read the below.
WPML uses slugs which are created from the wordpress core structure - you can't have two slugs with the exactly same name. If default language is EN and you have a secondary, in my case GR, then one slug is going to be "myslug" and the other "myslug-gr" (you can customize this "-gr" through WPML settings).
In functions.php you can filter the "wpml_ls_filter" function which is fired when your Language switcher is created.
Code as below :
add_filter('icl_ls_languages', 'wpml_ls_filter');
function wpml_ls_filter($languages) {
foreach ($languages as $lang_code => $language) {
$mTempString = $languages[$lang_code]['url'];
echo $mTempString; // HERE
// If "tax" is found in that string, replace it with "" - remove it.
if (strpos($mTempString, "tax") !== false) {
$languages[$lang_code]['url'] = str_replace("tax", "", $mTempString);
}
}
return $languages;
}
The above echo is going to show you the url that is created (and you can manipulate it) of each language button when pressed whenever you visit a page/post.
I haven't wrote a complete solution because that really depends on what you want to do. For example the above code helped me achieve to remove "tax" if found in the url.
This answer is providing an alternative way to achieve multiple languages with same slug
Polylang with Polylang Slug these 2 plugins can do
https://wordpress.org/plugins/polylang/
https://github.com/grappler/polylang-slug/
You can use my plugin, it is based on the newest Polylang Pro module and does what you need: https://github.com/lufton/polylang-share-slug
Is there any way to change the Wordpress language conditionally? I mean let's say we have a variable $theme_lang and if that is equal to something, change the language across the theme.
Here's the code:
if($theme_lang == 'de'){
//everything goes here in German
}
In short, I want to avoid a separate installation for another language. Thanks!
You can change Wordpress language globally in wp-config.php with the WPLANG constant like this:
define( 'WPLANG', 'de_DE' );
Though if you want to change language section per section you can't use this. You should better go for a multilingual plugin, there is many of them, some paid (WPML), some free (qTranslate, Multilingual Press...).
Then in your template you can switch the plugin language constant on some section to force a language on content.