We have moved to Github. Please open tickets there.

Opened 4 years ago

Closed 4 years ago

#1006 closed Enhancement (invalid)

Need changes to core languages lib

Reported by: gabrielinux Owned by:
Priority: normal Milestone: Elgg 2.0
Component: Core Version: 1.5
Severity: minor Keywords: languages, translation
Cc: ben, cash, gabrielinux@… Difficulty:

Description

In order to create a language auto detection plug-in, I need to have a plugin hook for get_language(). Either that, or the core language.php file needs to be modified as follows:

/

  • Gets the current language in use by the system or user.
  • [Marcus Povey 20090216: Not sure why this func is necessary.] *
  • @return string The language code (eg "en") */

function get_language() {


global $CONFIG;


$user = get_loggedin_user();
$language = false;


if (($user) && ($user->language))

$language = $user->language;


BEGIN OakPages modification: detect browser language
attempt detection of browser language if user is not logged in
$browser_lang = substr($_SERVERHTTP_ACCEPT_LANGUAGE?, 0, 2);
if ((!$language) && (isset($browser_lang)))

$language = $browser_lang;

END OakPages modification: detect browser language

if ((!$language) && ($CONFIG->language))

$language = $CONFIG->language;


if ($language) {

return $language;

}
return false;


}

Attachments (1)

languages.php (7.7 KB) - added by gabrielinux 4 years ago.
Changes made to languages.php by Gabriel Monge-Franco

Download all attachments as: .zip

Change History (4)

comment:1 Changed 4 years ago by gabrielinux

  • Cc gabrielinux@… added
  • Keywords languages, translation added; languages removed

Never mind -- actually, I found a way to select between a site-default language and an autodetected language. I created a plug-in for this (http://community.elgg.org/pg/plugins/gabrielinux/read/99711/automagic_translation). However, to use this plug-in, the core engine/lib/languages.php file needs to be changed. The reason behind this is that Elgg 1.5 loads plug-ins after it has already loaded the login page, index page and other core components. By the time this plug-in is loaded, those pages have already been created in the site's default language, so they can no longer be translated. But don't worry -- the changes made are not specific to my plug-in, in the hope that they can make it into the next Elgg release.

So, with that said, could we change the following functions in engine/lib/languages.php? These changes also happen to fix some other bugs reported on trac.

get_current_language():

	/**
	 * Detect the current language being used by the current site or logged in user.
	 *
	 */
	function get_current_language()
	{
		global $CONFIG;
		
		$language = get_language();
			
		if (!$language)
			$language = 'en';
			
		return $language;
	}

get_language():

	/**
	 * Gets the current language in use by the system or user.
	 * 
	 * [Marcus Povey 20090216: Not sure why this func is necessary.]
	 *
	 * @return string The language code (eg "en")
	 */
		function get_language() {
			
			global $CONFIG;
		
			$user = get_loggedin_user();
			$language = false;
			$browserlang = false;
			
			// Priority 1: User-configured language (if logged in)
			if (($user) && ($user->language))
				$language = $user->language;
			
			// Priority 2: Browser language (if autodetection enabled)
			if ((!$language) && (isset($CONFIG->detectlanguage)) && ($CONFIG->detectlanguage==true)) {
				$browserlang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
				if ($browserlang) { // We were able to autodetect the browser language
					if ((!isset($CONFIG->language)) || (is_null($CONFIG->language)) || (isset($CONFIG->language) && ($CONFIG->language!=$browserlang))) {
						// Language was autodetected; set default site language
						$CONFIG->language = $language = $browserlang;
					}
				}
			}
			
			// Priority 3: Administrator-configured language (if set)
			if ((!$language) && (isset($CONFIG->language)) && ($CONFIG->language))
				$language = $CONFIG->language;
				
			if ($language) {
				return $language;
			}		
			return false;
			
		}

elgg_echo():

	/**
	 * Given a message shortcode, returns an appropriately translated full-text string 
	 *
	 * @param string $message_key The short message code
	 * @param string $language Optionally, the standard language code (defaults to the site default, then English)
	 * @return string Either the translated string, or the original English string, or an empty string
	 */
		function elgg_echo($message_key, $language = "") {
			
			global $CONFIG;
				
			static $CURRENT_LANGUAGE;
			if ((!$CURRENT_LANGUAGE) && (!$language)) 
				$CURRENT_LANGUAGE = $language = get_language();
			else 
				$language = $CURRENT_LANGUAGE;

			if (isset($CONFIG->translations[$language][$message_key])) {
				return $CONFIG->translations[$language][$message_key];
			} else if (isset($CONFIG->translations["en"][$message_key])) {
				return $CONFIG->translations["en"][$message_key];
			}
				
			return $message_key;
			
		}

Changed 4 years ago by gabrielinux

Changes made to languages.php by Gabriel Monge-Franco

comment:2 Changed 4 years ago by gabrielinux

  • Summary changed from Need to hook for get_language() function to Need changes to core languages lib

comment:3 Changed 4 years ago by coldtrick

  • Resolution set to invalid
  • Status changed from new to closed

no need for any changes in languages.php. The behaviour can be done by acting on the 'plugins_boot' system event. I did just that in my plugin http://community.elgg.org/pg/plugins/jdalsem/read/213021/language-selector

so i vote for closing this ticket

Note: See TracTickets for help on using tickets.