Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/i18n/language.php

Documentation is available at language.php

  1. <?php
  2. /**
  3. @version        $Id: language.php 6762 2007-03-03 08:37:41Z tcp $
  4. @package        Joomla.Framework
  5. @subpackage    I18N
  6. @copyright    Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
  7. @license        GNU/GPL, see LICENSE.php
  8. *  Joomla! is free software. This version may have been modified pursuant
  9. *  to the GNU General Public License, and as distributed it includes or
  10. *  is derivative of works licensed under the GNU General Public License or
  11. *  other free or open source software licenses.
  12. *  See COPYRIGHT.php for copyright notices and details.
  13. */
  14.  
  15. // Check to ensure this file is within the rest of the framework
  16. defined('JPATH_BASE'or die();
  17.  
  18. /**
  19.  * Text  handling class
  20.  *
  21.  * @static
  22.  * @package     Joomla.Framework
  23.  * @subpackage    I18N
  24.  * @since        1.5
  25.  */
  26. class JText
  27. {
  28.     /**
  29.      * Translates a string into the current language
  30.      *
  31.      * @access public
  32.      * @param string $string The string to translate
  33.      * @param boolean    $jsSafe        Make the result javascript safe
  34.      *
  35.      */
  36.     function _($string$jsSafe false)
  37.     {
  38.         $lang =JFactory::getLanguage();
  39.         return $lang->_($string$jsSafe);
  40.     }
  41.  
  42.     /**
  43.      * Passes a string thru an sprintf
  44.      *
  45.      * @access public
  46.      * @param format The format string
  47.      * @param mixed Mixed number of arguments for the sprintf function
  48.      */
  49.     function sprintf($string)
  50.     {
  51.         $lang =JFactory::getLanguage();
  52.         $args func_get_args();
  53.         if (count($args0{
  54.             $args[0$lang->_($args[0]);
  55.             return call_user_func_array('sprintf'$args);
  56.         }
  57.         return '';
  58.     }
  59.     /**
  60.      * Passes a string thru an printf
  61.      *
  62.      * @access public
  63.      * @param format The format string
  64.      * @param mixed Mixed number of arguments for the sprintf function
  65.      */
  66.     function printf($string)
  67.     {
  68.         $lang =JFactory::getLanguage();
  69.         $args func_get_args();
  70.         if (count($args0{
  71.             $args[0$lang->_($args[0]);
  72.             return call_user_func_array('printf'$args);
  73.         }
  74.         return '';
  75.     }
  76.  
  77. }
  78.  
  79. /**
  80.  * Languages/translation handler class
  81.  *
  82.  * @package     Joomla.Framework
  83.  * @subpackage    I18N
  84.  * @since        1.5
  85.  */
  86. class JLanguage extends JObject
  87. {
  88.     /**
  89.      * Debug language, If true, highlights if string isn't found
  90.      *
  91.      * @var boolean 
  92.      * @access protected
  93.      */
  94.     var $_debug     = false;
  95.     
  96.     /**
  97.      * The default language
  98.      * 
  99.      * The default language is used when a language file in the requested language does not exist.
  100.      * 
  101.      * @var string 
  102.      * @access protected
  103.      */
  104.     var $_default    = 'en-GB';
  105.  
  106.     /**
  107.      * An array of orphaned text
  108.      *
  109.      * @var array 
  110.      * @access protected
  111.      */
  112.     var $_orphans     = array();
  113.  
  114.     /**
  115.      * Array holding the language metadata
  116.      *
  117.      * @var array 
  118.      * @access protected
  119.      */
  120.     var $_metadata     = null;
  121.  
  122.     /**
  123.      * Identifying string of the language
  124.      *
  125.      * @var string 
  126.      * @access protected
  127.      */
  128.     var $_identifyer = null;
  129.  
  130.     /**
  131.      * The language to load
  132.      *
  133.      * @var string 
  134.      * @access protected
  135.      */
  136.     var $_lang = null;
  137.  
  138.     /**
  139.      * Transaltions
  140.      *
  141.      * @var array 
  142.      * @access protected
  143.      */
  144.     var $_strings = null;
  145.  
  146.     /**
  147.     * Constructor activating the default information of the language
  148.     *
  149.     * @access protected
  150.     */
  151.     function __construct($lang null)
  152.     {
  153.         $this->_strings = array ();
  154.  
  155.         if $lang == null {
  156.             $lang $this->_default;
  157.         }
  158.  
  159.         $this->setLanguage($lang);
  160.  
  161.         $this->load();
  162.     }
  163.  
  164.     /**
  165.      * Returns a reference to a language object
  166.      *
  167.      * This method must be invoked as:
  168.      *         <pre>  $browser = &JLanguage::getInstance([$lang);</pre>
  169.      *
  170.      * @access public
  171.      * @param string $lang  The language to use.
  172.      * @return JLanguage  The Language object.
  173.      */
  174.     function getInstance($lang)
  175.     {
  176.         $instance new JLanguage($lang);
  177.         $reference $instance;
  178.         return $reference;
  179.     }
  180.  
  181.     /**
  182.     * Translator function, mimics the php gettext (alias _) function
  183.     *
  184.     * @access public
  185.     * @param string        $string     The string to translate
  186.     * @param boolean    $jsSafe        Make the result javascript safe
  187.     * @return string    The translation of the string
  188.     */
  189.     function _($string$jsSafe false)
  190.     {
  191.         //$key = str_replace( ' ', '_', strtoupper( trim( $string ) ) );echo '<br>'.$key;
  192.         $key strtoupper($string);
  193.         $key substr($key01== '_' substr($key1$key;
  194.         if (isset ($this->_strings[$key])) {
  195.             $string $this->_debug ? "&bull;".$this->_strings[$key]."&bull;" $this->_strings[$key];
  196.         else {
  197.             if (defined($string)) {
  198.                 $string $this->_debug ? "!!".constant($string)."!!" constant($string);
  199.             else {
  200.                 if ($this->_debug)
  201.                 {
  202.                     $this->_orphans[$string;
  203.                     $string "??".$string."??";
  204.                 }
  205.             }
  206.         }
  207.         if ($jsSafe{
  208.             $string addslashes($string);
  209.         }
  210.         return $string;
  211.     }
  212.     
  213.     /**
  214.      * Check if a language exists
  215.      * 
  216.      * This is a simple, quick check for the directory that should contain language files for the given user.
  217.      * 
  218.      * @access    public
  219.      * @param    string $lang Language to check
  220.      * @return    boolean True if the language exists
  221.      * @since    1.5
  222.      */
  223.     function exists($lang)
  224.     {
  225.         static    $languages    array();
  226.         
  227.         // Return false if no language was specified
  228.                 if $lang {
  229.             return false;
  230.         }
  231.         
  232.         // Return previous check results if it exists
  233.                 if isset($languages[$lang]) )
  234.         {
  235.             return $languages[$lang];
  236.         }
  237.         
  238.         // Check if the language exists
  239.                 jimport('joomla.filesystem.folder');
  240.         
  241.         $dir    JLanguage::getLanguagePathJPATH_BASE$lang );
  242.         
  243.         $languages[$lang]    JFolder::exists($dir);
  244.  
  245.         return $languages[$lang];
  246.     }
  247.     
  248.     /**
  249.      * Loads a single language file and appends the results to the existing strings
  250.      *
  251.      * @access public
  252.      * @param string     $prefix     The prefix
  253.      * @param string     $basePath      The basepath to use
  254.      * @return boolean    True, if the file has successfully loaded.
  255.      */
  256.     function load$prefix ''$basePath JPATH_BASE )
  257.     {
  258.         static $paths;
  259.  
  260.         if (!isset($paths))
  261.         {
  262.             $paths array();
  263.         }
  264.  
  265.         $path JLanguage::getLanguagePath$basePath$this->_lang);
  266.  
  267.         $filename empty$prefix ?  $this->_lang $this->_lang '.' $prefix ;
  268.         $filename $path.DS.$filename.'.ini';
  269.  
  270.         $result false;
  271.         if (isset$paths[$filename))
  272.         {
  273.             // Strings for this file have already been loaded
  274.             $result true;
  275.         }
  276.         else
  277.         {
  278.             // Load the language file
  279.             $newStrings $this->_load$filename );
  280.  
  281.             // Check if there was a problem with loading the file
  282.             if $newStrings === false {
  283.                 // No strings, which probably means that the language file does not exist
  284.                 $path        JLanguage::getLanguagePath$basePath$this->_default);
  285.                 $filename    empty$prefix ?  $this->_default $this->_default '.' $prefix ;
  286.                 $filename    $path.DS.$filename.'.ini';
  287.  
  288.                 $newStrings $this->_load$filename );
  289.             }
  290.             
  291.             // Merge the new strings into the strings array
  292.             if is_array($newStrings) ) {
  293.                 $this->_strings array_merge$this->_strings$newStrings);
  294.                 $paths[$filenametrue;
  295.                 $result true;
  296.             else {
  297.                 // Do something ??? 
  298.             }
  299.         }
  300.  
  301.         return $result;
  302.  
  303.     }
  304.  
  305.     /**
  306.     * Loads a language file and returns the parsed values
  307.     *
  308.     * @access private
  309.     * @param string The name of the file
  310.     * @return mixed Array of parsed values if successful, boolean False if failed
  311.     */
  312.     function _load$filename )
  313.     {
  314.         if ($content @file_get_contents$filename )) {
  315.  
  316.             $this->_identifyer basename$filename'.ini' );
  317.  
  318.             $registry new JRegistry();
  319.             $registry->loadINI($content);
  320.             return $registry->toArray);
  321.         }
  322.  
  323.         return false;
  324.     }
  325.  
  326.     /**
  327.      * Get a matadata language property
  328.      *
  329.      * @access public
  330.      * @param string $property    The name of the property
  331.      * @param mixed  $default    The default value
  332.      * @return mixed The value of the property
  333.      */
  334.     function get($property$default null)
  335.     {
  336.         if (isset ($this->_metadata[$property])) {
  337.             return $this->_metadata[$property];
  338.         }
  339.         return $default;
  340.     }
  341.  
  342.     /**
  343.     * Getter for Name
  344.     *
  345.     * @access public
  346.     * @param string  $value     An optional value
  347.     * @return string Official name element of the language
  348.     */
  349.     function getName($value null{
  350.         return $this->_metadata['name'];
  351.     }
  352.  
  353.     /**
  354.     * Getter for PDF Font Name
  355.     *
  356.     * @access public
  357.     * @return string name of pdf font to be used
  358.     */
  359.     function getPdfFontName({
  360.         return $this->_metadata['pdffontname'];
  361.     }
  362.  
  363.     /**
  364.     * Getter for Windows locale code page
  365.     *
  366.     * @access public
  367.     * @return string windows locale encoding
  368.     */
  369.     function getWinCP({
  370.         return $this->_metadata['wincodepage'];
  371.     }
  372.  
  373.     /**
  374.     * Getter for backward compatible language name
  375.     *
  376.     * @access public
  377.     * @return string backward compatible name
  378.     */
  379.     function getBackwardLang({
  380.         return $this->_metadata['backwardlang'];
  381.     }
  382.  
  383.     /**
  384.     * Get for the language tag (as defined in RFC 3066)
  385.     *
  386.     * @access public
  387.     * @return string The language tag
  388.     */
  389.     function getTag({
  390.         return $this->_metadata['tag'];
  391.     }
  392.  
  393.     /**
  394.     * Get locale property
  395.     *
  396.     * @access public
  397.     * @return string The locale property
  398.     */
  399.     function getLocale()
  400.     {
  401.         $locales explode(','$this->_metadata['locale']);
  402.  
  403.         for($i 0$i count($locales)$i++ {
  404.             $locale $locales[$i];
  405.             $locale trim($locale);
  406.             $locales[$i$locale;
  407.         }
  408.  
  409.         //return implode(',', $locales);
  410.         return $locales;
  411.     }
  412.  
  413.     /**
  414.     * Get the RTL property
  415.     *
  416.     * @access public
  417.     * @return boolean True is it an RTL language
  418.     */
  419.     function isRTL($value null{
  420.         return $this->_metadata['rtl'];
  421.     }
  422.  
  423.     /**
  424.     * Set the Debug property
  425.     *
  426.     * @access public
  427.     */
  428.     function setDebug($debug{
  429.         $this->_debug $debug;
  430.     }
  431.  
  432.     /**
  433.     * Get the Debug property
  434.     *
  435.     * @access public
  436.     * @return boolean True is in debug mode
  437.     */
  438.     function getDebug({
  439.         return $this->_debug;
  440.     }
  441.     
  442.     /**
  443.      * Get the default language code
  444.      * 
  445.      * @access    public
  446.      * @return    string Language code
  447.      */
  448.     function getDefault({
  449.         return $this->_default;
  450.     }
  451.     
  452.     /**
  453.      * Set the default language code
  454.      * 
  455.      * @access public
  456.      */
  457.     function setDefault($lang{
  458.         $this->_default    $lang;
  459.     }
  460.  
  461.     /**
  462.     * Get the list of orphaned strings if being tracked
  463.     *
  464.     * @access public
  465.     * @return boolean True is in debug mode
  466.     */
  467.     function getOrphans({
  468.         return $this->_orphans;
  469.     }
  470.  
  471.     /**
  472.      * Determines is a key exists
  473.      *
  474.      * @access public
  475.      * @param key $key    The key to check
  476.      * @return boolean True, if the key exists
  477.      */
  478.     function hasKey($key{
  479.         return isset ($this->_strings[strtoupper($key)]);
  480.     }
  481.  
  482.     /**
  483.      * Returns a associative array holding the metadata
  484.      *
  485.      * @access public
  486.      * @param string    The name of the language
  487.      * @return mixed    If $lang exists return key/value pair with the language metadata,
  488.      *                   otherwise return NULL
  489.      */
  490.  
  491.     function getMetadata($lang)
  492.     {
  493.         $path JLanguage::getLanguagePath(JPATH_BASE$lang);
  494.         $file $lang.'.xml';
  495.  
  496.         $result null;
  497.         if(is_file($path.DS.$file)) {
  498.             $result JLanguage::_parseXMLLanguageFile($path.DS.$file);
  499.         }
  500.  
  501.         return $result;
  502.     }
  503.  
  504.     /**
  505.      * Returns a list of known languages for an area
  506.      *
  507.      * @access public
  508.      * @param string    $basePath     The basepath to use
  509.      * @return array    key/value pair with the language file and real name
  510.      */
  511.     function getKnownLanguages($basePath JPATH_BASE)
  512.     {
  513.         $dir JLanguage::getLanguagePath($basePath);
  514.         $knownLanguages JLanguage::_parseLanguageFiles($dir);
  515.  
  516.         return $knownLanguages;
  517.     }
  518.  
  519.     /**
  520.      * Get the path to a language
  521.      *
  522.      * @access public
  523.      * @param string $basePath  The basepath to use
  524.      * @param string $language    The language tag
  525.      * @return string    language related path or null
  526.      */
  527.     function getLanguagePath($basePath JPATH_BASE$language null )
  528.     {
  529.         $dir $basePath.DS.'language';
  530.         if (isset ($language)) {
  531.             $dir .= DS.$language;
  532.         }
  533.         return $dir;
  534.     }
  535.  
  536.     /**
  537.      * Set the language attributes to the given language
  538.      *
  539.      * Once called, the language still needs to be loaded using JLanguage::load()
  540.      *
  541.      * @access    public
  542.      * @param    string    $lang    Language code
  543.      */
  544.     function setLanguage($lang)
  545.     {
  546.         $this->_lang        $lang;
  547.         $this->_metadata    $this->getMetadata($this->_lang);
  548.  
  549.         //set locale based on the language tag
  550.         //TODO : add function to display locale setting in configuration
  551.         $locale setlocale(LC_ALL$this->getLocale());
  552.     }
  553.  
  554.     /**
  555.      * Searches for language directories within a certain base dir
  556.      *
  557.      * @access public
  558.      * @param string     $dir     directory of files
  559.      * @return array    Array holding the found languages as filename => real name pairs
  560.      */
  561.     function _parseLanguageFiles($dir null)
  562.     {
  563.         jimport('joomla.filesystem.folder');
  564.  
  565.         $languages array ();
  566.  
  567.         $subdirs JFolder::folders($dir);
  568.         foreach ($subdirs as $path{
  569.             $langs JLanguage::_parseXMLLanguageFiles($dir.DS.$path);
  570.             $languages array_merge($languages$langs);
  571.         }
  572.  
  573.         return $languages;
  574.     }
  575.  
  576.     /**
  577.      * Parses INI type of files for language information
  578.      *
  579.      * @access public
  580.      * @param string    $dir     Directory of files
  581.      * @return array    Array holding the found languages as filename => real name pairs
  582.      */
  583.     function _parseINILanguageFiles($dir null)
  584.     {
  585.         if ($dir == null)
  586.             return null;
  587.  
  588.         $languages array ();
  589.         jimport('joomla.filesystem.folder');
  590.         $files JFolder::files($dir'^([_A-Za-z]*)\.ini$');
  591.         foreach ($files as $file{
  592.             if ($content file_get_contents($dir.DS.$file)) {
  593.                 $langContent JParameter::parse($contentfalsetrue);
  594.                 $lang str_replace('.ini'''$file);
  595.                 $name $lang;
  596.                 if (isset ($langContent['__NAME'])) {
  597.                     $name $langContent['__NAME'];
  598.                 }
  599.  
  600.                 $languages[$lang$name;
  601.             }
  602.         }
  603.         return $languages;
  604.     }
  605.  
  606.     /**
  607.      * Parses XML files for language information
  608.      *
  609.      * @access public
  610.      * @param string    $dir     Directory of files
  611.      * @return array    Array holding the found languages as filename => metadata array
  612.      */
  613.     function _parseXMLLanguageFiles($dir null)
  614.     {
  615.         if ($dir == null{
  616.             return null;
  617.         }
  618.  
  619.         $languages array ();
  620.         jimport('joomla.filesystem.folder');
  621.         $files JFolder::files($dir'^([-_A-Za-z]*)\.xml$');
  622.         foreach ($files as $file{
  623.             if ($content file_get_contents($dir.DS.$file)) {
  624.                 if ($metadata JLanguage::_parseXMLLanguageFile($dir.DS.$file)) {
  625.                     $lang str_replace('.xml'''$file);
  626.                     $languages[$lang$metadata;
  627.                 }
  628.             }
  629.         }
  630.         return $languages;
  631.     }
  632.  
  633.     /**
  634.      * Parse XML file for language information
  635.      *
  636.      * @access public
  637.      * @param string    $path     Path to the xml files
  638.      * @return array    Array holding the found metadata as a key => value pair
  639.      */
  640.     function _parseXMLLanguageFile($path)
  641.     {
  642.         jimport('joomla.factory');
  643.         $xml JFactory::getXMLParser('Simple');
  644.         if (!$xml->loadFile($path)) {
  645.             return null;
  646.         }
  647.  
  648.         // Check that it's am metadata file
  649.         if ($xml->document->name(!= 'metafile'{
  650.             return null;
  651.         }
  652.  
  653.         $metadata array ();
  654.  
  655.         //if ($xml->document->attributes('type') == 'language') {
  656.  
  657.             foreach ($xml->document->metadata[0]->children(as $child{
  658.                 $metadata[$child->name()$child->data();
  659.             }
  660.         //}
  661.         return $metadata;
  662.     }
  663. }
  664.  
  665. /**
  666.  * @package     Joomla.Framework
  667.  * @subpackage        I18N
  668.  * @static
  669.  * @since 1.5
  670.  */
  671. {
  672.     /**
  673.      * Builds a list of the system languages which can be used in a select option
  674.      *
  675.      * @access public
  676.      * @param string    Client key for the area
  677.      * @param string    Base path to use
  678.      * @param array    An array of arrays ( text, value, selected )
  679.      */
  680.     function createLanguageList($actualLanguage$basePath JPATH_BASE{
  681.  
  682.         $list array ();
  683.  
  684.         // cache activation
  685.         $cache JFactory::getCache('JLanguage');
  686.         $langs $cache->call('JLanguage::getKnownLanguages'$basePath);
  687.  
  688.         foreach ($langs as $lang => $metadata{
  689.             $option array ();
  690.  
  691.             $option['text'$metadata['name'];
  692.             $option['value'$lang;
  693.             if ($lang == $actualLanguage{
  694.                 $option['selected''selected="selected"';
  695.             }
  696.             $list[$option;
  697.         }
  698.  
  699.         return $list;
  700.     }
  701.  
  702.     /**
  703.       * Tries to detect the language
  704.       *
  705.       * @access public
  706.       */
  707.     function detectLanguage()
  708.     {
  709.         if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
  710.         {
  711.             $systemLangs    JLanguage::getKnownLanguages();
  712.             $browserLangs    explode','$_SERVER['HTTP_ACCEPT_LANGUAGE');
  713.  
  714.             foreach ($browserLangs as $browserLang)
  715.             {
  716.                 // slice out the part before ; on first step, the part before - on second, place into array
  717.                 $browserLang substr$browserLang0strcspn$browserLang';' ) );
  718.                 $primary_browserLang substr$browserLang0);
  719.  
  720.                 foreach($systemLangs as $systemLang => $metadata)
  721.                 {
  722.                     if($primary_browserLang == substr$metadata['tag']0)) {
  723.                         return $systemLang;
  724.                     }
  725.                 }
  726.             }
  727.         }
  728.  
  729.         return 'en-GB';
  730.     }
  731.  
  732. }
  733. ?>

Documentation generated on Mon, 05 Mar 2007 21:09:13 +0000 by phpDocumentor 1.3.1