joomla_test/libraries/joomla/language/text.php

334 lines
9.4 KiB
PHP
Raw Permalink Normal View History

2020-01-02 22:20:31 +07:00
<?php
/**
* @package Joomla.Platform
* @subpackage Language
*
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
defined('JPATH_PLATFORM') or die;
/**
* Text handling class.
*
* @package Joomla.Platform
* @subpackage Language
* @since 11.1
*/
class JText
{
/**
* javascript strings
*
* @var array
* @since 11.1
*/
protected static $strings = array();
/**
* Translates a string into the current language.
*
* Examples:
* <script>alert(Joomla.JText._('<?php echo JText::_("JDEFAULT", array("script"=>true));?>'));</script>
* will generate an alert message containing 'Default'
* <?php echo JText::_("JDEFAULT");?> it will generate a 'Default' string
*
* @param string $string The string to translate.
* @param mixed $jsSafe Boolean: Make the result javascript safe.
* @param boolean $interpretBackSlashes To interpret backslashes (\\=\, \n=carriage return, \t=tabulation)
* @param boolean $script To indicate that the string will be push in the javascript language store
*
* @return string The translated string or the key is $script is true
*
* @since 11.1
*/
public static function _($string, $jsSafe = false, $interpretBackSlashes = true, $script = false)
{
$lang = JFactory::getLanguage();
if (is_array($jsSafe))
{
if (array_key_exists('interpretBackSlashes', $jsSafe))
{
$interpretBackSlashes = (boolean) $jsSafe['interpretBackSlashes'];
}
if (array_key_exists('script', $jsSafe))
{
$script = (boolean) $jsSafe['script'];
}
if (array_key_exists('jsSafe', $jsSafe))
{
$jsSafe = (boolean) $jsSafe['jsSafe'];
}
else
{
$jsSafe = false;
}
}
if (!(strpos($string, ',') === false))
{
$test = substr($string, strpos($string, ','));
if (strtoupper($test) === $test)
{
$strs = explode(',', $string);
foreach ($strs as $i => $str)
{
$strs[$i] = $lang->_($str, $jsSafe, $interpretBackSlashes);
if ($script)
{
self::$strings[$str] = $strs[$i];
}
}
$str = array_shift($strs);
$str = vsprintf($str, $strs);
return $str;
}
}
if ($script)
{
self::$strings[$string] = $lang->_($string, $jsSafe, $interpretBackSlashes);
return $string;
}
else
{
return $lang->_($string, $jsSafe, $interpretBackSlashes);
}
}
/**
* Translates a string into the current language.
*
* Examples:
* <?php echo JText::alt("JALL","language");?> it will generate a 'All' string in English but a "Toutes" string in French
* <?php echo JText::alt("JALL","module");?> it will generate a 'All' string in English but a "Tous" string in French
*
* @param string $string The string to translate.
* @param string $alt The alternate option for global string
* @param mixed $jsSafe Boolean: Make the result javascript safe.
* @param boolean $interpretBackSlashes To interpret backslashes (\\=\, \n=carriage return, \t=tabulation)
* @param boolean $script To indicate that the string will be pushed in the javascript language store
*
* @return string The translated string or the key if $script is true
*
* @since 11.1
*/
public static function alt($string, $alt, $jsSafe = false, $interpretBackSlashes = true, $script = false)
{
$lang = JFactory::getLanguage();
if ($lang->hasKey($string . '_' . $alt))
{
return self::_($string . '_' . $alt, $jsSafe, $interpretBackSlashes);
}
else
{
return self::_($string, $jsSafe, $interpretBackSlashes);
}
}
/**
* Like JText::sprintf but tries to pluralise the string.
*
* Note that this method can take a mixed number of arguments as for the sprintf function.
*
* The last argument can take an array of options:
*
* array('jsSafe'=>boolean, 'interpretBackSlashes'=>boolean, 'script'=>boolean)
*
* where:
*
* jsSafe is a boolean to generate a javascript safe strings.
* interpretBackSlashes is a boolean to interpret backslashes \\->\, \n->new line, \t->tabulation.
* script is a boolean to indicate that the string will be push in the javascript language store.
*
* Examples:
* <script>alert(Joomla.JText._('<?php echo JText::plural("COM_PLUGINS_N_ITEMS_UNPUBLISHED", 1, array("script"=>true));?>'));</script>
* will generate an alert message containing '1 plugin successfully disabled'
* <?php echo JText::plural("COM_PLUGINS_N_ITEMS_UNPUBLISHED", 1);?> it will generate a '1 plugin successfully disabled' string
*
* @param string $string The format string.
* @param integer $n The number of items
*
* @return string The translated strings or the key if 'script' is true in the array of options
*
* @since 11.1
*/
public static function plural($string, $n)
{
$lang = JFactory::getLanguage();
$args = func_get_args();
$count = count($args);
if ($count > 1)
{
// Try the key from the language plural potential suffixes
$found = false;
$suffixes = $lang->getPluralSuffixes((int) $n);
array_unshift($suffixes, (int) $n);
foreach ($suffixes as $suffix)
{
$key = $string . '_' . $suffix;
if ($lang->hasKey($key))
{
$found = true;
break;
}
}
if (!$found)
{
// Not found so revert to the original.
$key = $string;
}
if (is_array($args[$count - 1]))
{
$args[0] = $lang->_(
$key, array_key_exists('jsSafe', $args[$count - 1]) ? $args[$count - 1]['jsSafe'] : false,
array_key_exists('interpretBackSlashes', $args[$count - 1]) ? $args[$count - 1]['interpretBackSlashes'] : true
);
if (array_key_exists('script', $args[$count - 1]) && $args[$count - 1]['script'])
{
self::$strings[$key] = call_user_func_array('sprintf', $args);
return $key;
}
}
else
{
$args[0] = $lang->_($key);
}
return call_user_func_array('sprintf', $args);
}
elseif ($count > 0)
{
// Default to the normal sprintf handling.
$args[0] = $lang->_($string);
return call_user_func_array('sprintf', $args);
}
return '';
}
/**
* Passes a string thru a sprintf.
*
* Note that this method can take a mixed number of arguments as for the sprintf function.
*
* The last argument can take an array of options:
*
* array('jsSafe'=>boolean, 'interpretBackSlashes'=>boolean, 'script'=>boolean)
*
* where:
*
* jsSafe is a boolean to generate a javascript safe strings.
* interpretBackSlashes is a boolean to interpret backslashes \\->\, \n->new line, \t->tabulation.
* script is a boolean to indicate that the string will be push in the javascript language store.
*
* @param string $string The format string.
*
* @return string The translated strings or the key if 'script' is true in the array of options.
*
* @since 11.1
*/
public static function sprintf($string)
{
$lang = JFactory::getLanguage();
$args = func_get_args();
$count = count($args);
if ($count > 0)
{
if (is_array($args[$count - 1]))
{
$args[0] = $lang->_(
$string, array_key_exists('jsSafe', $args[$count - 1]) ? $args[$count - 1]['jsSafe'] : false,
array_key_exists('interpretBackSlashes', $args[$count - 1]) ? $args[$count - 1]['interpretBackSlashes'] : true
);
if (array_key_exists('script', $args[$count - 1]) && $args[$count - 1]['script'])
{
self::$strings[$string] = call_user_func_array('sprintf', $args);
return $string;
}
}
else
{
$args[0] = $lang->_($string);
}
return call_user_func_array('sprintf', $args);
}
return '';
}
/**
* Passes a string thru an printf.
*
* Note that this method can take a mixed number of arguments as for the sprintf function.
*
* @param format $string The format string.
*
* @return mixed
*
* @since 11.1
*/
public static function printf($string)
{
$lang = JFactory::getLanguage();
$args = func_get_args();
$count = count($args);
if ($count > 0)
{
if (is_array($args[$count - 1]))
{
$args[0] = $lang->_(
$string, array_key_exists('jsSafe', $args[$count - 1]) ? $args[$count - 1]['jsSafe'] : false,
array_key_exists('interpretBackSlashes', $args[$count - 1]) ? $args[$count - 1]['interpretBackSlashes'] : true
);
}
else
{
$args[0] = $lang->_($string);
}
return call_user_func_array('printf', $args);
}
return '';
}
/**
* Translate a string into the current language and stores it in the JavaScript language store.
*
* @param string $string The JText key.
* @param boolean $jsSafe Ensure the output is JavaScript safe.
* @param boolean $interpretBackSlashes Interpret \t and \n.
*
* @return string
*
* @since 11.1
*/
public static function script($string = null, $jsSafe = false, $interpretBackSlashes = true)
{
if (is_array($jsSafe))
{
if (array_key_exists('interpretBackSlashes', $jsSafe))
{
$interpretBackSlashes = (boolean) $jsSafe['interpretBackSlashes'];
}
if (array_key_exists('jsSafe', $jsSafe))
{
$jsSafe = (boolean) $jsSafe['jsSafe'];
}
else
{
$jsSafe = false;
}
}
// Add the string to the array if not null.
if ($string !== null)
{
// Normalize the key and translate the string.
self::$strings[strtoupper($string)] = JFactory::getLanguage()->_($string, $jsSafe, $interpretBackSlashes);
}
return self::$strings;
}
}