Language Switcher WordPress Plugin
This is the fourth page describing the Language Switcher plugin for WordPress. If you didn't start on the first page, you might want to go back and start there.
Also... As of April 2010, this plugin is no longer being maintained. Please see note on the top of the plugin home page.
Testing and Troubleshooting
Once you have followed the steps on the other pages of this site to install and modify WordPress, Plugins, and your Theme, you will need to do some testing before your site is ready for public release. Here is the process I would suggest:
- Delete the dummy articles, categories, comments, and blogroll links that come with your original installation of WordPress.
- Make some tagged, multilingual content, as described in the translation section of the site. Write one short multilingual article, using the special language tags (including the title). Also, make one multilingual category name for your post and another for links, and add a multilingual link to your blogroll. Add a multilingual tag, too.
- Visit your site's home page. Verify that everything on the screen appears in your default language.
- Click on a language link or flag to change to a different language, and verify that everything on the screen appears in that language. Also verify that if you click on links, the language is maintained (you will need to have cookies enabled to ensure that the language is maintained when you close your browser window).
- Repeat the previous two steps for a category archive page, a date archive page, a search results page, and a single article page.
- Repeat the previous three steps for any additional languages you are using.
- You might also want to verify that after selecting a language, the WordPress adminstration panels appear in that language. In WordPress 2.5 or later, with Language Switcher 1.16 or later, you can switch languages by clicking on a language link on the right side of the administration page header.
- If you find any text that is not appearing in the right language,
here are some things you
can do to try to make it right:
- If the text that is not working correctly came from the content you entered (article text, titles, blogroll, categories, etc.), check to see that you are using the language tags correctly in your content.
- If none of the static text seems to be changing for you, your MO files are probably either missing, have the wrong names, or are in the wrong places. See the Translating section of this site. Or, it could be this bug in WordPress's "gettext" functionality, which has to do with whether your web site is running on a computer with a 64-bit processor, or a 32-bit processor. (The link shows how to fix this problem.)
- If you find that dates are not being translated correctly (i.e. the days of the week or names of months are not translating), those are in the main WordPress .mo file. See the Translating section of this site.
- If some of the text is changing but some isn't, you probably need to internationalize or filter more of your plugins, or translate more terms. See Fixing plugins and themes and Translating sections of this site for more information.
- If you find that some things seem to be "sticky" in one of your languages
(meaning that when you change languages, they still have links that lead to
articles back in the previous language), you could have a "cache" problem,
where WordPress is keeping some of its output around
and re-using it, rather than changing it when you change languages.
Check to see if you have this line in your wp-config.php file, and if so,
remove it:
define('ENABLE_CACHE', true);
- If your admin interface is in the wrong language,
here are some things to consider:
- If you switch to a particular language on the visitor-facing part of your site, the admin interface will switch to that language too. So, use the sidebar links you put into your theme to switch to the language you want to use in the admin interface.
- In WordPress 2.5 or later, with Language Switcher 1.16 or later, you can also switch languages inside the admin interface, by using either a link in the administration page header, or the drop-down list of quick links in the administration page header, depending on your WordPress version.
- If your administration interface is staying in English, then you probably need to add translations to the main WordPress language files, or change the names of the main WordPress language files. The files must be named ll.mo, where ll matches the two-letter language code you entered in the Language Switcher settings page, and the language files must be in directory wp-content/languages in your WordPress installation (wp-includes/languages may also work). See Translating section of this site for more information.
Frequently Asked Questions (and answers)
- How can I use different flag images?
Put the image files you want to use into the sub-directory "langswitch_flags" of wp-content/plugins, in your WordPress installation. Then change your Language Switcher Options to associate languages with the desired flag image files. - I am thinking about converting an existing site to being multilingual.
Can I prepare my translations of old posts off-line before activating the plugin?
My recommendation would be that you go ahead and install/activate the Language Switcher, and then fill in the translations for past articles as you have time. Language Switcher works just fine with posts that have no language tags -- it just allows them to be viewed like they were before you installed and activated the Language Switcher (i.e. the existing posts will be visible for all language viewers). The other alternative would be to translate the articles into text documents off-line, and then when you are ready to turn the switch, turn on Language Switcher and put in all the translations. But that would mean a delay, and an inability to write new posts with translations while you are working on the old posts. Another thing you could do is to prepare a section something like this, in all of your site's languages:[lang_en]I'm working on translating my past posts. For now, this post is only available in English.[/lang_en] [lang_es]Estoy traduciendo blah blah blah [/lang_es] [lang_it]...[/lang_it]
If you copy/paste something like this at the top of all of your past articles, site viewers will see a description of what is going on, in their chosen language, followed by the untranslated text of the post. The thing to avoid is to have Language Switcher turned off, but have posts with language tags in them. They will not look good on the screen! - I have lots of articles already in my site, and it would take
me a very long time to put tags or the above text into all of my articles. Is there
some way to do this automatically?
Yes, you can run a database query using PHPMyAdmin. Here's how:- Log into PHPMyAdmin, and select the database you are using for your site.
- Back up your database! If this query doesn't work, you can restore your backup.
- Find the name of the "posts" table. This is usually something like "wp_posts", but you might have a different prefix on your table.
- Click on the "SQL" tab/link.
- Type in the following query:
UPDATE wp_posts SET post_content=CONCAT( "BEFORE_TEXT", CONCAT( post_content, "AFTER_TEXT" )) WHERE post_type='post'
Replace wp_posts with the correct post table name for your database, if it isn't called wp_posts. You will also need to replace BEFORE_TEXT and AFTER_TEXT with the text you want to add to each of your posts. For instance, you might want to put "[lang_en]" in as the Before text, and "[/lang_en]" in as the After text, to mark all previous posts you wrote as being in English. - Submit the query.
- You might want to repeat this query using post_title instead of post_content, to update the titles in the same way (Note: make sure to change post_content to post_title twice in the query!). Also, if your site has static pages, you can update them by substituting post_type='page' for post_type='post' at the end of the query.
- Test and make sure it worked. If not, restore your database backup.
- Does Language Switcher translate the Custom Fields
in my site?
Yes and no. As of version 1.16 of the Language Switcher, if you use the "the_meta" function in your theme, you can use language tags in both the keys and values of your custom fields, and Language Switcher will display the correct language. However, if you are using the "get_post_meta" function in your theme to display only certain "meta" information, only the values will get translated, and you will have to do a little more work. For example, if you have a post meta field called "fav" that you have created in the Custom Fields section of the post editing screen, you would normally display it in your theme using something like this:<?php echo get_post_meta($post->ID, "fav", true); ?>
To have Language Switcher choose the correct language, change this part of your theme to look like this:<?php echo apply_filters('the_title', get_post_meta($post->ID, "fav", true)); ?>
See the Using Custom Fields article on the WordPress Codex site for more information about custom fields. - I installed and activated the plugin, but some of the text on
my screen is not being translated. Why?
See the Testing and Troubleshooting section above. - I installed the Language Switcher, and now my admin interface
is only in English. How could that happen?
See the Testing and Troubleshooting section above. - When I switch to a different language, my site's layout is
completely broken or very different. How could that happen?
Here are some possible reasons that your site could look different in different languages:- Your theme is doing something different in the CSS for different languages. For instance, you may have a language-specific CSS stylesheet, or a right-to-left language stylesheet that is being loaded for only some languages.
- The HTML structure of a particular post or page that is being viewed is different in different languages. Which is to say, when you wrote your post in the different languages, you put different HTML inside the post sections for different languages. Usually this would not affect the overall layout of your site, but if you had an HTML error inside the post section for one particular language, it could break the layout for the whole page. You can test for HTML errors on any web page by visiting validator.w3.org
- I am using the built-in WordPress Widgets for my sidebar,
but the widget titles are not being translated. What can I do?
You can put Language Switcher language tags into the titles you enter on the Widget option screens. If you are using the latest versions of WordPress and Language Switcher, and installed the WordPress fixes files below, built-in WordPress widgets should then be working properly. If your widget comes from a plugin, you might have to modify the plugin file so that the title text is "filtered". See the Fixing Themes and Plugins section of this site to learn how to add filters to PHP files. - What happens with comments on a post?
Basically, comments are run through the Language Switcher just the same as the text in your posts. So, assuming the commenter doesn't put language tags into their comment, it will be as if they put [lang_all] tags around it, and it will appear in all languages. - Another plugin I am using has some special URLs, and it
stops working when I activate the Language Switcher. How can I fix this?
The Language Switcher adds suffixes to your site's URLs to indicate languages. If you have permalinks turned on in your site, the suffixes look something like "/langswitch_lang/de" to switch to German (code "de"). If you don't have permalinks, the suffixes look like this instead: "?langswitch_lang=de".
Unfortunately, while the core of WordPress doesn't care, and properly-written plugins also should not care, some plugins use special URL detection that cannot deal with the first type of URL suffix. If this is happening in your site, you can force Language Siwtcher to use "?"-style URL suffixes. To do this, if you are using the latest version of Language Switcher, visit the Language Switcher options page (under Settings / Language Switcher or Options / Languages Switcher in your WordPress admin screens, depending on what version of WordPress you are running). Check the box that says "Check this box to force Language Switcher URL suffixes to use ? or &, if you are having trouble with permalink-style URL suffixes". Then save your changes, and hopefully your other plugin will go back to working. - I think the URLs generated by Language Switcher are ugly. Can I do anything
about them?
Yes. Visit the Language Switcher options page (under Settings / Language Switcher or Options / Languages Switcher in your WordPress admin screens, depending on what version of WordPress you are running). There is a setting near the bottom of the screen for "URL slug for Language Switcher". By default, Language Switcher adds suffixes like "/langswitch_lang/de" or "?langswitch_lang=de" to your URLs. You can change the "langswitch_lang" part to something different by typing it in the box and saving the options. But be careful -- don't use something that WordPress could confuse with another WordPress URL! For instance, "cat", "tag", "s", "p", numbers, and any text that is part of your custom permalink structure would all be bad ideas. - For search-engine purposes, or because I am using a caching
plugin, I want every URL in my site to
have a language suffix, so that I can be sure that a particular URL
always points to a particular language. Can I do that?
Yes. Visit the Language Switcher options page (under Settings / Language Switcher or Options / Languages Switcher in your WordPress admin screens, depending on what version of WordPress you are running). There is a setting near the bottom of the screen that tells Language Switcher to add a language suffix to all of your URLs. - In some browsers, my site keeps reverting to English as I
navigate. Why?
Probably the reason is that you are not allowing cookies. Language Switcher uses cookies to keep track of your language preference as you navigate the site, so if you turn off cookies, it will not remember your language preference. You can get around this by making every URL have the language suffix (see question above), so that each time a site visitor clicks on a link, the URL rather than a cookie will be used to determine the language. - The language part of the URL always seems to appear at the
end. Can I change the order of the URL?
No, not unless you rewrite the Language Switcher to do that. - I don't think all of the languages are being indexed by
blog search engines. Why?
You many need to submit multiple RSS feeds to the blog search engines, one for each language you want to be indexed, as if they were separate blogs. The reason for that is that the blog search engines usually index based on your RSS feeds, and each feed has only one language included. Switch to one of your other languages, and you should be able to figure out what the feed URL is for that language. - The language links on my home page, generated by the Language Switcher, are incorrect. I am hosting on Windows/IIS. What can I do?
At least some Windows/IIS hosting platforms appear to be incompatible with the Language Switcher. At this time, there are no plans to modify the Language Switcher to be more compatible with IIS, sorry! You will probably either need to find a Linux-based hosting company, or a different language plugin. - When I turn on Language Switcher, something strange happens with the
times and dates on my posts. How can I fix it?
See the Fixing Plugins and Themes section of this site. - I don't think the default language setting is working. Why?
The Language Switcher goes through the following steps to see what language to use when displaying your site to a visitor:- Checks the language setting of the visitor's web browser.
- Checks for a cookie - this cookie is set by the Language Switcher after the first time through these steps, and if the cookie exists, it overrides the browser setting.
- Checks for a language in the URL, and if there is one, overrides cookies and browser settings (i.e. if the person has clicked on a link that changes the language).
- If none of the above gives a valid language (i.e. a language that you have entered in the Language Switcher settings page as one of the languages used in your site ), Language Switcher uses your site's default language from the settings page.
- What if there is no official 2-letter code for my language, or if I
want to use two different locales whose languages have the same 2-letter code?
The Language Switcher actually doesn't care whether you use "official" 2-letter language codes or not. So, just make up a 2-letter code for your language, and use it consistently in the Language Switcher options and when creating posts, categories, etc., and it should work just fine. The only thing that will not work with this method is that the automatic browser-detection of language will not work -- a user will have to click on a link to switch to your invented language code's language. - I am using a static page as my home page, and when I do an empty (no search
term) search (which gives me the home page) and then switch to a non-default language,
a list of posts is displayed instead. What can I do?
Some themes, when they are putting the search form on the screen, give the search button's HTML tag a "name" attribute. You need to remove this name, or replace it with an "id" attribute (you may also need to update your theme's style.css file). So, for instance, if your theme's search button line looks like this:<input type="submit" name="sbutton" value="<?php _e('Search'); ?>" />
you need to replace it with this:<input type="submit" id="sbutton" value="<?php _e('Search'); ?>" />
This should fix the problem, although you may still see a problem that if a site viewer switches to a non-default language, does a search, and doesn't accept a cookie that stores their language preference, they may get switched back to the default language. They would just have to click on their language in the sidebar again, if that is the case.
If you are interested, here is a bit of background. When you do a search for a term, the suffix "?s=term" is added to the URL (without the quotes), and WordPress knows to search for posts containing the term. If you don't enter a term, "?s=" is added to the URL, but since there is no search term, WordPress ignores it and goes to the home page. However, if you then add a language link to the URL, the extra URL suffix for the language would confuse WordPress and it would give you a list of posts, which is fine if that is normally what is on your home page, but not so good if you are using a static home page. So, Language Switcher cleans out empty "?s=" URL suffixes when making language links, so your viewers will still see the home page. Now, if your button also has a name, you'll see "?s=&sbutton=Search" on the URL, which again confuses WordPress, and since Language Switcher doesn't know the name of your particular search button, it doesn't know it should clean out the empty search suffix. Taking the name out of the search button fixes the problem. - I can program a little in PHP, and I want to know if there is a way I can do
something in PHP that detects what language the site viewer has chosen in the Language
Switcher.
Yes, you can. There is a global variable in the Language Switcher called $langswitch_lang_pref, which gets set to the 2-letter language code currently in effect. So you can do something like this:global $langswitch_lang_pref; if( $langswitch_lang_pref == 'ru' ) { // do something if the language is Russian } else { // do something different if the language is not Russian } - Is there an easier way to insert the language tags when editing a
post?
If you want to, you can add editing toolbar buttons to the editing screen. To do this, you will need to find the file "quicktags.js" in the wp-includes/js directory of your WordPress installation. Edit this file using a text editor, find the area where the buttons are defined, and add new buttons similar to this:edButtons[edButtons.length] = new edButton( 'ed_en', 'lang_en', '[lang_en]', '[/lang_en]', 'e' );
Thanks to user Christina for that suggestion. More recently, JP wrote to suggest a modification:t.Buttons[t.Buttons.length] = new edButton(name+'_es','Lang_ES','[lang_es]','[/lang_es]','z'); t.Buttons[t.Buttons.length] = new edButton(name+'_en','Lang_EN','[lang_en]','[/lang_en]','e'); edButtons[edButtons.length] = new edButton( 'ed_es','lang_es','[lang_es]','[/lang_es]','z'); edButtons[edButtons.length] = new edButton( 'ed_en','lang_en','[lang_en]','[/lang_en]','e');
Try both... I haven't tested either solution. - What versions of WordPress is this plugin compatible with?
Language Switcher works with version 2.1.2 and above of WordPress, if you have the WordPress fixes installed (as detailed in the Installing WordPress section). The latest version of WordPress that has been tested with this plugin is 2.9 Beta 1 (pre-release version of WordPress 2.9). - What has changed from version to version of Language Switcher?
Check the Updates section of the Introduction page. - How can I find out if there is a new version of the
plugin?
That's unlikely... Check out the note at the top of the plugin home page. - What additional features have been requested or considered?
- Add a template tag to make better page links for nextpage within a post.
- Make a way to escape tags to show them in a blog post, if you wanted to make a blog post about how to use the plugin. This could for instance be to add something like [notlang_es] in the post, and then translate that into [lang_es] in the post output.
- Make the flags work if the plugin is installed in a plugin sub-directory.
- Option to force a default language rather than using browser setting.
