Ignore:
Timestamp:
Oct 9, 2007, 3:43:29 AM (17 years ago)
Author:
rvelices
Message:
  • PWG_CHARSET, DB_CHARSET and DB_COLLATE... utf-8 ready
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/functions.inc.php

    r2126 r2127  
    477477 * @returns array
    478478 */
    479 function get_languages()
    480 {
     479function get_languages($target_charset = null)
     480{
     481  if ( empty($target_charset) )
     482  {
     483    $target_charset = get_pwg_charset();
     484  }
     485  $target_charset = strtolower($target_charset);
     486
    481487  $dir = opendir(PHPWG_ROOT_PATH.'language');
    482488  $languages = array();
     
    488494    {
    489495      list($language_name) = @file($path.'/iso.txt');
    490       $languages[$file] = $language_name;
     496
     497      $langdef = explode('.',$file);
     498      if (count($langdef)>1) // (langCode,encoding)
     499      {
     500        $langdef[1] = strtolower($langdef[1]);
     501
     502        if (
     503          $target_charset==$langdef[1]
     504         or
     505          ($target_charset=='utf-8' and $langdef[1]=='iso-8859-1')
     506         or
     507          ($target_charset=='iso-8859-1' and
     508          in_array( substr($langdef[0],2), array('en','fr','de','es','it','nl')))
     509        )
     510        {
     511          $language_name = convert_charset($language_name,
     512              $langdef[1], $target_charset);
     513          $languages[ $langdef[0] ] = $language_name;
     514        }
     515        else
     516          continue; // the language encoding is not compatible with our charset
     517      }
     518      else
     519      { // probably english that is the same in all ISO-xxx and UTF-8
     520        $languages[$file] = $language_name;
     521      }
    491522    }
    492523  }
     
    14301461function get_pwg_charset()
    14311462{
    1432   //TEMP CODE
    1433   global $lang_info;return $lang_info['charset'];
     1463  defined('PWG_CHARSET') or die('load_language PWG_CHARSET undefined');
     1464  return PWG_CHARSET;
    14341465}
    14351466
     
    14501481 */
    14511482function load_language($filename, $dirname = '', $language = '',
    1452     $return_content=false)
    1453 {
    1454   //TEMP CODE
    1455   if (!$return_content) $filename.='.php';
    1456   $f = get_language_filepath($filename, $dirname, $language);
    1457   if ($f === false)
    1458     return false;
    1459   if ($return_content)
    1460     return @file_get_contents($f);
    1461   global $lang, $lang_info;
    1462   @include($f);
    1463   return true;
    1464 }
    1465 
     1483    $return_content=false, $target_charset=null)
     1484{
     1485  global $user;
     1486
     1487  if (!$return_content)
     1488  {
     1489    $filename .= '.php'; //MAYBE to do .. load .po and .mo localization files
     1490  }
     1491  if (empty($dirname))
     1492  {
     1493    $dirname = PHPWG_ROOT_PATH;
     1494  }
     1495  $dirname .= 'language/';
     1496
     1497  $languages = array();
     1498  if ( !empty($language) )
     1499  {
     1500    $languages[] = $language;
     1501  }
     1502
     1503  if ( !empty($user['language']) )
     1504  {
     1505    $languages[] = $user['language'];
     1506  }
     1507  $languages[] = PHPWG_DEFAULT_LANGUAGE;
     1508  $languages = array_unique($languages);
     1509
     1510  if ( empty($target_charset) )
     1511  {
     1512    $target_charset = get_pwg_charset();
     1513  }
     1514  $target_charset = strtolower($target_charset);
     1515  $source_charset = '';
     1516  $source_file    = '';
     1517  foreach ($languages as $language)
     1518  {
     1519    $dir = $dirname.$language;
     1520
     1521    // exact charset match - no conversion required
     1522    $f = $dir.'.'.$target_charset.'/'.$filename;
     1523    if (file_exists($f))
     1524    {
     1525      $source_file = $f;
     1526      break;
     1527    }
     1528
     1529    // universal language (like Eng) no conversion required
     1530    $f = $dir.'/'.$filename;
     1531    if (file_exists($f))
     1532    {
     1533      $source_file = $f;
     1534      break;
     1535    }
     1536
     1537    if ($target_charset=='utf-8')
     1538    { // we accept conversion from ISO-8859-1 to UTF-8
     1539      $f = $dir.'.iso-8859-1/'.$filename;
     1540      if (file_exists($f))
     1541      {
     1542        $source_charset = 'iso-8859-1';
     1543        $source_file = $f;
     1544        break;
     1545      }
     1546    }
     1547
     1548    if ($target_charset=='iso-8859-1' and
     1549      in_array( substr($language,2), array('en','fr','de','es','it','nl') )
     1550      )
     1551    { // we accept conversion from UTF-8 to ISO-8859-1 for backward compatibility ONLY
     1552      $f = $dir.'.utf-8/'.$filename;
     1553      if (file_exists($f))
     1554      {
     1555        $source_charset = 'utf-8';
     1556        $source_file = $f;
     1557        break;
     1558      }
     1559    }
     1560  }
     1561
     1562  if ( !empty($source_file) )
     1563  {
     1564    if (!$return_content)
     1565    {
     1566      @include($source_file);
     1567      $load_lang = @$lang;
     1568      $load_lang_info = @$lang_info;
     1569
     1570      global $lang, $lang_info;
     1571      if ( !isset($lang) ) $lang=array();
     1572      if ( !isset($lang_info) ) $lang_info=array();
     1573
     1574      if ( !empty($source_charset) and $source_charset!=$target_charset)
     1575      {
     1576        if ( is_array($load_lang) )
     1577        {
     1578          foreach ($load_lang as $k => $v)
     1579          {
     1580            if ( is_array($v) )
     1581            {
     1582              $func = create_function('$v', 'return convert_charset($v, "'.$source_charset.'","'.$target_charset.'");' );
     1583              $lang[$k] = array_map($func, $v);
     1584            }
     1585            else
     1586              $lang[$k] = convert_charset($v, $source_charset, $target_charset);
     1587          }
     1588        }
     1589        if ( is_array($load_lang_info) )
     1590        {
     1591          foreach ($load_lang_info as $k => $v)
     1592          {
     1593            $lang_info[$k] = convert_charset($v, $source_charset, $target_charset);
     1594          }
     1595        }
     1596      }
     1597      else
     1598      {
     1599        $lang = array_merge( $lang, $load_lang );
     1600        $lang_info = array_merge( $lang_info, $load_lang_info );
     1601      }
     1602      return true;
     1603    }
     1604    else
     1605    {
     1606      $content = @file_get_contents($source_file);
     1607      if ( !empty($source_charset) and $source_charset!=$target_charset)
     1608      {
     1609        $content = convert_charset($content, $source_charset, $target_charset);
     1610      }
     1611      return $content;
     1612    }
     1613  }
     1614  return false;
     1615}
     1616
     1617/**
     1618 * converts a string from a character set to another character set
     1619 * @param string str the string to be converted
     1620 * @param string source_charset the character set in which the string is encoded
     1621 * @param string dest_charset the destination character set
     1622 */
     1623function convert_charset($str, $source_charset, $dest_charset)
     1624{
     1625  if ($source_charset==$dest_charset)
     1626    return $str;
     1627  if ($source_charset=='iso-8859-1' and $dest_charset=='utf-8')
     1628  {
     1629    return utf8_encode($str);
     1630  }
     1631  if ($source_charset=='utf-8' and $dest_charset=='iso-8859-1')
     1632  {
     1633    return utf8_decode($str);
     1634  }
     1635  if (function_exists('iconv'))
     1636  {
     1637    return iconv($source_charset, $dest_charset, $str);
     1638  }
     1639  if (function_exists('mb_convert_encoding'))
     1640  {
     1641    return mb_convert_encoding( $str, $dest_charset, $source_charset );
     1642  }
     1643  return $str; //???
     1644}
    14661645?>
Note: See TracChangeset for help on using the changeset viewer.