init_gettext.php 1.27 KB
<?php
	
	/*
	 * First find the browser encoding and try to create a valid locale
	 * from one of the supported browser encodings. Start with the most
	 * preferred encoding.
	 * If no country code is found use the same as for language.
	 * !!! FIXME: Is this code correct for IE too??? !!!
	 */
	$langs = explode (",", $_SERVER[HTTP_ACCEPT_LANGUAGE]);
	foreach ($langs as $lang)
	{
		preg_match("/^([a-z]{2})-*([a-z|A-Z]{2})*(;q=* *)*([0-9].[0-9])*$/",
			$lang, $lng_pref);

		$la = $lng_pref[1];
		$co = strtoupper ($lng_pref[2]);
		$pr = ($lng_pref[4] == "") ? 1.0 : $lng_pref[4];

		if ($co == "")
		{
			switch(true) {
			case $la === "en":
				$co = "GB";
				break;
			case $la === "ja":
				$co = "JP";
				break;
			default:  
				/* if not set most of the time i assume co = strtoupper (la) */
				$co = strtoupper ($la);
			}
		}

		if ($co != "US")
			$la_specs[$la."_".$co]=$pr;
		else
			$la_specs["POSIX"]=$pr;
	}
	array_multisort ($la_specs, SORT_DESC, SORT_NUMERIC);

	/*
	 * Set locale and initialize gettext catalogs
	 */
	foreach ($la_specs as $key => $value)
		if ("" != ($locale=setlocale (LC_MESSAGES, $key))) break;

	setlocale (LC_TIME, $locale);
	bindtextdomain ("messages", OEDL_DIR . 'locale');
	bind_textdomain_codeset ("messages", "UTF-8");
	textdomain ("messages");

?>