Language Switcher App

A PHP language switcher with templates
Dave Smith, Trinity College Dublin Web Design Office, 2012

Example

The example page is set up to detect English, Gaeilge and Russian and switch between them.

Add the switcher

1. Add this HTML to every page where the language switcher needs to be displayed

2. Add this code to the top of every page the language switcher will be on, or at least before the switcher HTML

3. Style it, the following is a starting point for template 4

Add more languages to the switcher

Look for $i18n in /www.tcd.ie/uns/language-switcher/2/app.php and add the new language to the array. Make sure the new language has an entry for language, home and ianasubtag_language_name.

For example adding Russian would go from:

$i18n = array (
    'ianasubtag_language_name' => array (
        'en' => 'English',
        'ga' => 'Gaeilge',
    ),
    'home' => array (
        'en' => 'Home',
        'ga' => 'Baile',
    ),
    'language' => array (
        'en' => 'Language',
        'ga' => 'Teagmháil',
    ),
);

to:

$i18n = array (
    'ianasubtag_language_name' => array (
        'en' => 'English',
        'ga' => 'Gaeilge',
        'ru' => 'Ruski',
    ),
    'home' => array (
        'en' => 'Home',
        'ga' => 'Baile',
        'ru' => 'Homeski',
    ),
    'language' => array (
        'en' => 'Language',
        'ga' => 'Teagmháil',
        'ru' => 'Langski',
    ),
);

App Options

languageSwitcher2AppTCD(array(
	'siteRoot' => string,
	'ianaSubtags' => array (string, string, ...),
	'homeImageURL' => string,
	'template' => number,
));

App Output

array(
	'html' => string,
	'lang' => string,
	'subtag' => string,
}

How the app detects the language

First tell the app what languages to search for, for example 'ianaSubtags' => array ('en', 'ga',), which tells the app the default language is English and to detect Gaeilge. See IANA Subtag Registry for a complete list or subtags and their corresponding language.

Then the app checks the filename of the loaded page and determines the language, for example index.php or bob.php would be detected as English (default language) and index.ga.php and bob.ga.php would be detected as Gaeilge.