language.php

来自「国外免费开源的内容管理系统」· PHP 代码 · 共 774 行 · 第 1/2 页

PHP
774
字号
<?php
/**
* @version		$Id: language.php 10214 2008-04-19 08:59:04Z eddieajau $
* @package		Joomla.Framework
* @subpackage	Language* @copyright	Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/
// Check to ensure this file is within the rest of the frameworkdefined('JPATH_BASE') or die();
/**
 * Languages/translation handler class
 *
 * @package 	Joomla.Framework
 * @subpackage	Language * @since		1.5
 */
class JLanguage extends JObject
{
	/**
	 * Debug language, If true, highlights if string isn't found
	 *
	 * @var		boolean
	 * @access	protected	 * @since	1.5
	 */
	var $_debug 	= false;	/**	 * The default language	 *	 * The default language is used when a language file in the requested language does not exist.	 *	 * @var		string	 * @access	protected	 * @since	1.5	 */	var $_default	= 'en-GB';

	/**
	 * An array of orphaned text
	 *
	 * @var		array
	 * @access	protected	 * @since	1.5
	 */
	var $_orphans 	= array();

	/**
	 * Array holding the language metadata
	 *
	 * @var		array
	 * @access	protected	 * @since	1.5
	 */
	var $_metadata 	= null;

	/**
	 * The language to load
	 *
	 * @var		string
	 * @access	protected	 * @since	1.5
	 */
	var $_lang = null;	/**	 * List of language files that have been loaded	 *	 * @var		array of arrays	 * @access	public	 * @since	1.5	 */	var $_paths	= array();

	/**
	 * Transaltions
	 *
	 * @var		array
	 * @access	protected	 * @since	1.5
	 */
	var $_strings = null;	/**	 * An array of used text, used during debugging	 *	 * @var		array	 * @access	protected	 * @since	1.5	 */	var $_used		= array();

	/**
	* Constructor activating the default information of the language
	*
	* @access	protected
	*/
	function __construct($lang = null)
	{
		$this->_strings = array ();
		if ( $lang == null ) {
			$lang = $this->_default;
		}		$this->setLanguage($lang);

		$this->load();
	}

	/**
	 * Returns a reference to a language object
	 *
	 * This method must be invoked as:
	 * 		<pre>  $browser = &JLanguage::getInstance([$lang);</pre>
	 *
	 * @access	public
	 * @param	string $lang  The language to use.
	 * @return	JLanguage  The Language object.	 * @since	1.5
	 */
	function & getInstance($lang)
	{
		$instance = new JLanguage($lang);
		$reference = & $instance;
		return $reference;
	}

	/**
	* Translate function, mimics the php gettext (alias _) function
	*
	* @access	public
	* @param	string		$string 	The string to translate
	* @param	boolean	$jsSafe		Make the result javascript safe
	* @return	string	The translation of the string	* @since	1.5
	*/
	function _($string, $jsSafe = false)
	{
		//$key = str_replace( ' ', '_', strtoupper( trim( $string ) ) );echo '<br>'.$key;
		$key = strtoupper($string);
		$key = substr($key, 0, 1) == '_' ? substr($key, 1) : $key;

		if (isset ($this->_strings[$key]))
		{
			$string = $this->_debug ? "&bull;".$this->_strings[$key]."&bull;" : $this->_strings[$key];			// Store debug information			if ( $this->_debug )
			{				$caller = $this->_getCallerInfo();				if ( ! array_key_exists($key, $this->_used ) ) {					$this->_used[$key] = array();				}				$this->_used[$key][] = $caller;			}
		}
		else
		{
			if (defined($string))
			{
				$string = $this->_debug ? '!!'.constant($string).'!!' : constant($string);				// Store debug information				if ( $this->_debug )
				{					$caller = $this->_getCallerInfo();					if ( ! array_key_exists($key, $this->_used ) ) {						$this->_used[$key] = array();					}					$this->_used[$key][] = $caller;				}
			}
			else
			{
				if ($this->_debug)
				{					$string = '??'.$string.'??';					$caller	= $this->_getCallerInfo();					if ( ! array_key_exists($key, $this->_orphans ) ) {						$this->_orphans[$key] = array();					}
					$this->_orphans[$key][] = $caller;
				}
			}
		}

		if ($jsSafe) {
			$string = addslashes($string);
		}

		return $string;
	}

	/**
	 * Transliterate function
	 *
	 * This method processes a string and replaces all accented UTF-8 characters by unaccented
	 * ASCII-7 "equivalents"
	 *
	 * @access	public
	 * @param	string	$string 	The string to transliterate
	 * @return	string	The transliteration of the string
	 * @since	1.5
	 */
	function transliterate($string)
	{
		$string = htmlentities(utf8_decode($string));
		$string = preg_replace(
			array('/&szlig;/','/&(..)lig;/', '/&([aouAOU])uml;/','/&(.)[^;]*;/'),
			array('ss',"$1","$1".'e',"$1"),
			$string);

		return $string;
	}
	/**	 * Check if a language exists	 *	 * This is a simple, quick check for the directory that should contain language files for the given user.	 *	 * @access	public	 * @param	string $lang Language to check	 * @param	string $basePath Optional path to check	 * @return	boolean True if the language exists	 * @since	1.5	 */	function exists($lang, $basePath = JPATH_BASE)	{		static	$paths	= array();		// Return false if no language was specified		if ( ! $lang ) {			return false;		}		$path	= $basePath.DS.'language'.DS.$lang;		// Return previous check results if it exists		if ( isset($paths[$path]) )		{			return $paths[$path];		}		// Check if the language exists		jimport('joomla.filesystem.folder');		$paths[$path]	= JFolder::exists($path);		return $paths[$path];	}
	/**
	 * Loads a single language file and appends the results to the existing strings
	 *
	 * @access	public
	 * @param	string 	$extension 	The extension for which a language file should be loaded
	 * @param	string 	$basePath  	The basepath to use
	 * @param	string	$lang		The language to load, default null for the current language	 * @param	boolean $reload		Flag that will force a language to be reloaded if set to true	 * @return	boolean	True, if the file has successfully loaded.	 * @since	1.5
	 */
	function load( $extension = 'joomla', $basePath = JPATH_BASE, $lang = null, $reload = false )
	{
		if ( ! $lang ) {			$lang = $this->_lang;		}		$path = JLanguage::getLanguagePath( $basePath, $lang);

		$filename = ( $extension == 'joomla' ) ?  $lang : $lang . '.' . $extension ;
		$filename = $path.DS.$filename.'.ini';

		$result = false;
		if (isset( $this->_paths[$extension][$filename] ) && ! $reload )
		{			// Strings for this file have already been loaded
			$result = true;
		}
		else
		{
			// Load the language file
			$result = $this->_load( $filename, $extension );
			// Check if there was a problem with loading the file			if ( $result === false )
			{				// No strings, which probably means that the language file does not exist				$path		= JLanguage::getLanguagePath( $basePath, $this->_default);				$filename	= ( $extension == 'joomla' ) ?  $this->_default : $this->_default . '.' . $extension ;				$filename	= $path.DS.$filename.'.ini';				$result = $this->_load( $filename, $extension );			}
		}

		return $result;

	}

	/**
	* Loads a language file	*	* This method will not note the successful loading of a file - use load() instead
	*
	* @access	private
	* @param	string The name of the file	* @param	string The name of the extension
	* @return	boolean True if new strings have been added to the language	* @see		JLanguage::load()	* @since	1.5
	*/
	function _load( $filename, $extension = 'unknown' )
	{		$result	= false;
		if ($content = @file_get_contents( $filename ))
		{
			$registry	= new JRegistry();
			$registry->loadINI($content);
			$newStrings	= $registry->toArray( );			if ( is_array( $newStrings) )			{				$this->_strings = array_merge( $this->_strings, $newStrings);				$result = true;			}
		}
		// Record the result of loading the extension's file.		if ( ! isset($this->_paths[$extension])) {			$this->_paths[$extension] = array();		}		$this->_paths[$extension][$filename] = $result;

		return $result;
	}

	/**
	 * Get a matadata language property
	 *
	 * @access	public
	 * @param	string $property	The name of the property
	 * @param	mixed  $default	The default value
	 * @return	mixed The value of the property	 * @since	1.5
	 */
	function get($property, $default = null)
	{
		if (isset ($this->_metadata[$property])) {
			return $this->_metadata[$property];
		}
		return $default;
	}	/**	 * Determine who called JLanguage or JText	 *	 * @access	private	 * @return	array Caller information	 * @since	1.5	 */	function _getCallerInfo()	{			// Try to determine the source if none was provided		if ( ! function_exists('debug_backtrace') || extension_loaded('Zend Optimizer') ) {			return null;		}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?