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.
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 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;
});
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
I am very beginner in drupal.
I want to change the theme every day of week for users.
for example in saturday show theme 'garland', on sunday show theme 'seven' and so on.
how can I implement it.
I have searched a lot but I found nothing especial.
thanks.
sounds like you are looking for the themekey module!?
ThemeKey allows you to define simple or sophisticated theme-switching rules which allow automatic selection of a theme depending on current path, taxonomy terms, language, node-type, and many, many other properties.
The easiest way to change your frontend theme is to set it in your sites/default/settings.php:
if(date("w")==0)
$conf['theme_default'] = 'seven';
else if(date("w")==1)
$conf['theme_default'] = 'garland';
...
You can easily achieve it by implementing hook_custom_theme
You can figure out what active theme key is by calling:
global $theme_key
inside your hook implementation. Of course every theme that you want to use should be enabled.
I've been building basic themes now for nearly a year, and I'm trying to clean up my style as much as possible. But I don't know where to look to find out what this does...
In the kubrick theme php files, for example you get a php tag like this...
<?php the_content('<p class="serif">' . __('Read the rest of this entry »', 'kubrick') . '</p>'); ?>
You see the theme name 'kubrick' weaved in. What is the purpose of this?
You see it in all themes, twentyten, twentyeleven, etc.. but I never notice a difference if I leave it the same. What benefits does this have if I change it to my current theme name?
Can anyone enlighten me? or point me in the right direction?
Thanks
Josh
This is the 'theme text domain' and is typically used for localization. You can find out more by reading the gettext filter reference. One note is that the text domain is not required to be the same as your theme name. You can make it whatever you want as long as you are consistent with what you load using load_theme_textdomain. It's just convention to make it the same as your theme name. Finally, as to why you should bother including a domain here is a quote from an article called How to localize WordPress themes and plugins with GetText:
Have you noticed the 2nd argument in the GetText calls? It’s an
optional argument that tells GetText what the scope (domain) of the
texts is. If supplied, this GetText will return the translations only
from the dictionary that you supply with that domain name. Although
optional, specifying the translation domain is highly recommended.
Without it, GetText might return a different translation, if the same
string also appears in a different plugin, or in WordPress.