Getting the list of (other) languages

* N=0/1
* KEY=id/code/name/custom (name -> translated_name), (default: id)
* DIR=asc/desc (default: asc)
* link_empty_to = str (default: empty, works in conjunction with skip_missing=0 and allows using custom links for the languages that do not have translations for the current element. {%lang} can be used as placeholder for the language code)

Notes:

The skip_missing parameter tells the function how to treat languages with no translations.

The combination of orderby and order allows creating the drop-down language switcher or language switchers where languages always display in the same position.

The custom order can be defined in the WordPress admin under WPML » Languages » Language Switcher Options

The function returns an array with entries per language. For example, for a WordPress site running English, French and Italian, it will return this:

Each language has its own array of parameters, which your theme function can use to build any language selector.

id: Internal reference id

active: This is the currently active language (exactly one language is active)

native_name: The native name of the language (never translated)

translated_name: The name of the language translated to the currently active language

country_flag_url: The URL to a PNG image with the country flag

url: The link to the translation in that language

missing: 1 if the translation for that element is missing, 0 if it it exists.

Handling missing translations

Some pages may not be translated to all languages. You can tell icl_get_languages what to return for languages with no translations.

If ‘skip_missing=1′, these languages will not appear in the output. If ‘skip_missing=0′, all the languages will appear and languages with missing translations will link back to the home page in that language.

How to use in your theme functions

You can build your own language switchers in whatever way you choose. Here, we’ll show two popular uses.

The example PHP functions we’re showing here should go in to your functions.php file (in the theme folder) – not added to the plugin.

This post is also available in…

Supposing you want to add a line, at the bottom (or top) of each post, saying in which other languages it’s available. This function should only return existing translated posts and if no translation is available, it shouldn’t output anything.

Get the list of languages from WPML – $languages = icl_get_languages(‘skip_missing=1′);

Check that there’s more than one language for this post – if(1 < count($languages))

Create the output, skipping the currently displayed language – if(!$l[‘active’])

Notes:

The message goes through gettext. This is very important, so that this message, which you’ll append to posts in different languages will appear in the correct language: __(‘This post is also available in: ‘);

The language names used are the translated language names. This would guarantee that the entire sentence is written in the correct language: $l[‘translated_name’]

The result is this:

Message about post available in other languages

To include this message in posts, add a call to icl_post_languages() from single.php.

List of language names and flags for the footer

Even if you have a language selector at the top of the page, it’s a good idea to add a list of language names and flags to the footer. Many people immediately scroll down the the bottom of the page, to get a better idea of what’s ahead, so placing a prominent language switcher there is likely to help your foreign visitors.

The icl_disp_language() function is created by WPML. What it does is check if the two arguments (native_language_name, translated_language_name) are different. If so, it returns them both, otherwise, it returns them just once.

We should also add some CSS to style this languages list. This CSS will center the languages list in your footer and format it a bit:

Replacing the country flags and changing language names

WPML includes a GUI for editing language information. Go to WPML->Languages and click on Edit languages. You will be able to change the language names, edit their locale values and choose different flags.