A PHP language switcher with templates
Dave Smith, Trinity College Dublin Web Design Office, 2012
The example page is set up to detect English, Gaeilge and Russian and switch between them.
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
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', ), );
languageSwitcher2AppTCD(array( 'siteRoot' => string, 'ianaSubtags' => array (string, string, ...), 'homeImageURL' => string, 'template' => number, ));
array ('en', 'ga', 'ru')
English is the default languagearray ('en', 'ga')
is given then the app will only look for English and Gaeilge pages, for example index.php, bob.php, index.ru.php would be detected as English, and index.ga.php, bob.ga.php will be detected as Gaeilgearray( 'html' => string, 'lang' => string, 'subtag' => string, }
<?php if(!@include $_SERVER['DOCUMENT_ROOT'].'/tms/m/h'.$languageSwitcherTCD['ext'].'.php') include $_SERVER['DOCUMENT_ROOT'].'/tms/m/h.php'; ?>
, which attempts to include the current language include file, which may be the default language, and if the current language include file does not exist the default language include file is included$languageSwitcherTCD['i18n']['language'][$languageSwitcherTCD['lang']]
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.