Custom multilingual themes
This guide will show you how to create multilingual WordPress themes. A multilingual theme is more than just wrapping all the texts in GetText calls and providing a .mo file. Localizing the theme’s texts is one major step, but there are other things that have to be done.
A theme for a multilingual site needs to show available languages, allow selecting which language to view in and display correctly in each language.
These are the things that we’ll need to do:
- Providing localization for different languages.
- Allowing users to switch from one language to the other.
- Returning the right content per language.
- Telling WPML what needs translation.
Getting the theme ready for translation
In order for the theme to display in different languages, the texts need to pass translation before they are displayed. WordPress uses GetText for this and so should your theme.
We’ve written a post about localizing WordPress themes. It tells how to wrap the theme’s texts in GetText calls, create translations (.mo files) and apply them to the theme.
Switching between different languages
A language switcher shows visitors which languages are available and lets them choose.
Language switchers can come in different forms. Some people prefer a compact drop down menu with language names. Others display country flags and others show horizontal or vertical lists of languages.
WPML makes implementing language switchers easy. It comes built in with its own drop-down language switcher which can also be included as a widget. This language switcher can display country flags, language names and their translations. It can be configured from the WPML->Languages menu.
You can also create your own custom language switcher and insert to different places in the theme. The custom language switcher manual page explains how you can create it and provides examples for common uses. Adding a custom language switcher to your theme can add flare and elegance and make it obvious to users that the theme is intended for multilingual sites.
Getting content in the right language
Many themes include hard-coded link to pages, categories and tags.
WPML hooks to the standard WordPress API functions and filters them by language.
For example, when you call:
$args = array( 'category' => 1 );
$myposts = get_posts( $args );
Supposing category “1″ is an English category and the translation to Spanish is category “4″. When view a Spanish page and run this code, WPML will replace the category number in the filter to “4″.
For this to happen, your code must use only WordPress API calls and not manually query the database (which is a bad practice by itself).
You can control this functionality in WPML->Languages. Look for the option to automatically adjust IDs.
Telling WPML what needs translation
WPML can read a language configuration file. This file tells it everything it needs to know about your theme.
This includes admin texts, custom fields and custom types you’re using.
Once you create this XML file, WPML will auto configure and allow users to run a multilingual site without clicking on a single setting screen.
Hello, great plugin, It work prefect,
But I have change all my hard coded link in thme , but How to change the image localised for the theme (same as your logo in multilanguage but for other images on my theme).
thanks.
WPML doesn’t yet include any built in support for localizing images (service different images for languages).
You can implement it yourself using some simple PHP, which will check the current language and determine which image to display. I would just create different folders for each language and prefix that to the image path.
ok,
Thank you very much.
For other :
the function to check the current language and determinate which image to display is here and work :
http://forum.wpml.org/topic.php?id=164#post-1288
@easysofts,
Read about Body class based on language in the Forum to learn how to deal with background-image in your CSS.
Hi, where it says ‘At the end of functions.php file, add:
include "wpml-integration.php";Now, you can start using these helper functions to make your theme multilingual ready (but still run normally without any plugin installed).’
I think it should say ‘At the end of functions.php file, but before the ?>’, add:’
I’ve been taking some instructions literally and learning some things the hard way.
[...] Cómo crear temas multidioma en WordPress Comparte esto [...]
How to e.g. show the list of languages inside a post? Is there a post tag code lige <<>> to put in a post or page, that will be swapped with the list of languages?
Best regards
Svend K.
Hi I wanted to post a quick-fix for relative links. I use Inline Posts plugin to have a single edit point for a table which is in three different pages – one for each language.
For example, my images are all kept in “/wp-content/uploads”. I have three languages, English, French, Spanish. My WordPress install folder is “wordpress2″.
Add this to your WPML active .htacces file:
#strip lang code for images
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/wordpress2/(en|fr|es)/(.*)\.(jpg|gif|png)/?$
RewriteRule /(.*) $1
#redirect ‘safe’ url (lang code removed)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^.*/+$
RewriteRule (.*)\.(jpg|gif|png)/+$ $1.$2 [L]
#redirect when there’s no prefix slash, prevents looping
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/wordpress2/(en|fr|es)/?
RewriteRule (.*) /wordpress2/wp-content/uploads/$1 [L]
#redirect when there’s a prefix slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/wordpress2/wp-content
RewriteCond %{REQUEST_URI} ^/wordpress2/(.*)\.(jpg|gif|png)/?$
RewriteRule /(.*) /wordpress2/wp-content/uploads/$1 [L]
before these default lines:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.) /wordpress2/index.php [L]
# END WordPress
Hello,
Congratulations for your fantastic work.
I have a worpress site, with its own theme, and I am using iframes. The iframes are not translated (although the strings are). What is the instruction to tell a php page contained in an iFrame to be properly translated?
Many thanks!!!
Philippe
Hi,
Thank you for this excellent plugin. I have a small problem with page menu. On the default language (French), we have a menu on the top. When I select the second language (English) through the UK flag on the top-right, the top-menu disappears. Can someone advice me?
Regards,
GM.
You descripe my problem;
“Returning the right contents per language
Some themes display a ‘featured posts’ or ‘news’ box on their home page.”
But where must I build in the helper function:
wpml_get_object_id($element_id, $element_type='post', $return_original_if_missing=false, $ulanguage_code=null)in functions.php or home.php or am I missing something??
thanks
Don’t worry about that. WPML 1.7.0 (coming out soon) is going to make all this logic obsolete. It will automatically adjust your theme’s queries to return results in the right language.
This will include those ‘featured posts’ and anything else you can do in a WP theme. Stay tuned to the blog, we’ll introduce it there.
dear Amir, WPML is now at version 1.7.6 and it still has the same problem: it does not show automatically the right posts if you switch languages. How can one do this? I still have the same question as Lindebjerg! I do not know where to use this helper function:
wpml_get_object_id($element_id, $element_type=’post’, $return_original_if_missing=false, $ulanguage_code=null)
I and many others would be happy to get a clear solution to this!
Mihai, WPML’s lead developer is watching the forum and helping folks there. Did you post this question there?
Amir, your post before Horia’s mentioned the automisation (WPML 1.7.0 (coming out soon) is going to make all this logic obsolete) of this but you’re pointing us off to the forums? What happened to the magic you promised?
Sounds great! Looking foreward too WPML 1.7.0
But I need to bring a concept for this site http://keine-werbung-bitte.de in a few days.
Maybee you could just show me on this piece of code here:
have_posts()) : $recent->the_post();?>
ID, "thumb", true) ): ?>
<a href="" rel="bookmark"><img class="thumb" src="/tools/timthumb.php?src=ID, "thumb", $single = true); ?>&h=&w=&zc=1" alt="" />
how too make the changes, too understand a bit more for me and maybee a few people out there.
Thank you for doing yours to makeing WordPress the best CMS in the World:-)
something went wrong, sorry
here the code again:
have_posts()) : $recent->the_post();?>
ID, "thumb", true) ): ?>
<a href="" rel="bookmark"><img class="thumb" src="/tools/timthumb.php?src=ID, "thumb", $single = true); ?>&h=&w=&zc=1" alt="" />
My Navigation menu and pages widgets disappears when I switch to any language other than English, how do I solve this? I know thre is an answer some where do damned if I can find it!
Hello,
i’m french and my english is not very good, I have a problem with my theme, the link to the home page is working but not translated when i switch in english
Her is the code in the header: have you an idea ….
<a href="” title=”Accueil”>ACCUEIL
excude me here is the entire code //<a href="” title=”Accueil”>ACCUEIL
//
[...] linguagens em todo o seu temas, imagens e etc. O próprio plugin tem um tutorial sobre isto chamado creating multilingual wordpress themes, muito bom e simples, ja estou usando aqui no site, e apresentou ótimos resultados. Submit this to [...]
Hi there I was wondering what code to use to tell WPML to Not translate or change a particular section of the home page.
On the front page I have the picture slideshow that is being affected by this and there is also a live feed from the blog menu that is also getting affected by this.
Thank you for your help.
I will also add to that in the theme that I am using some of the text are not being picked up by the string translation system.
I was wondering how I could adjust the php pages so that WPML could pick up on those ares?
Hi.
Congrats for your well fasntastic plug-in.
I am quite new to WP and not a php developer. My theme has a Menu Manager and a also a Menu Manager for categories – (I am using newscast theme from Kriesi.at). WPML does not interact with this admin menu. So, I cannot localise the Menu neither the Categories menu. I guess this data goes into the database. I am quite lost. Can I do something in the theme admin pages so that WPML adds language fileds to the menu managers?
BR
dinis
Again.
I guess the solution ought to be in the “Translating texts entered in the theme’s admin screen” section right?
Another idea,
There should be a way that the theme saves the menu items to the database as a _e() or __() and when it is retrieving them, WPML will translate those. At some point, I should have a place where to write the final language outputs … Do you think it might work that way?
do you know that weed can make you want to fly?
Hi there,
I’m looking for event management solution for multilanguage website. And so far this gives me a very hard times. Events should be simple (dates, venues, descriptions) and mostly shown as lists for upcomming events.
Already tried in my opinion most promising plugins – The Event Calendar (http://www.shaneandpeter.com/) and amr-events (http://icalevents.anmari.com/)
Could you recommend a solution that would work seamlessly with WPML?
when i translate a category with same name for example (suites) to German
it adds @de after it
is there a way to get it with same name in the front end with out doing actions like
sub string !?
I am on a wp-ecommerce website project with a particular theme named Mazine.
I’m trying to fully make it multilingual.
I’m using the lastest free version 2.0.4 and some troubles appear in my shopping cart, my product page and the widgets.
My question is simple, does the commercial 2.3 version correct these bugs ?
If not, I am ready to make it multilingual by my self.
Thanks for your replies.
PS : Sorry for my bad english, I’m french !
Right now, WPML doesn’t work fully with WPEC. It’s our main goal for the following release.
This week we’re wrapping up WPML 2.3.0 and starting on WPML 2.4.0. This is the #1 feature for that release.
Is there a way to leave the blog section of my site NOT translated, the client wants it just in English, all other sections are translated and its working great. I just need the English Blog to be accessible from both language versions
WPML has an option for that. Go to WPML->Languages and choose which blog posts to display. You can change it from ‘only translated’ to ‘all’.
great, thanks for that, i’ll give it a try.
How to skip the multilingual without uncheck Adjust IDs for multilingual functionality
in the admin
like for example in get_posts(‘category’=>23)
i want the same result in any different language
is there a way for that like disable hook or something
wondering whether it is still necessary to “include “wpml-integration.php”;” in functions.php or has that already become redundant some time ago?
This file includes helper functions. If you’re not using any of these helpers, you don’t need to include the file.
When I do a search query on my wp site some of the results turn up in a different
language i.e. I used a search term in German and get results for that word in spanish.
Is there a way of influencing the search results?
Hello,
Thanks for this amazing plugin.
I only get stuck when i use it with the Nivo player in my header.
The images in the header will not appear, when it is used in a WPML page.
Does anyone have the same problem or knows how i can fix it?
Kind Regards.
Paul