百科狗-知识改变命运!
--

bind_textdomain_codeset() - gettext库(多语言支持)

梵高1年前 (2023-11-21)阅读数 13#技术干货
文章标签返回值

bind_textdomain_codeset()

(PHP 4 >= 4.2.0, PHP 5, PHP 7)

bind_textdomain_codeset() - gettext库(多语言支持)

Specify the character encoding in which the messages from the DOMAIN message catalog will be returned

说明

bind_textdomain_codeset(string $domain,string $codeset): string

With bind_textdomain_codeset(), you can set in which encoding will be messages from$domainreturned by gettext() and similar functions.

参数

$domain

The domain

$codeset

The code set

返回值

A string on success.

First the the url of the gettext manual changed:
http://www.gnu.org/software/gettext/manual/
Secondly, lets explain a little bit what this fonction does.
By default, gettext will use the LC_CTYPE of the language you choose (for example fr_FR).
This LC_CTYPE is extracted from your locales.alias file in your configuration dir (Should be /etc/locales.alias).
By default, the encoding is frequently iso-8859-1.
So if you want to make your site utf-8 aware, you need to bind your domain with the right encoding.
Here is a sample:

As quoted in other notes, the translations path should be like
/translations_path
  /de_DE/
    /LC_MESSAGES
  /fr_FR/
    /LC_MESSAGES
  ...
Your translation goes in the LC_MESSAGES dirs ... Hopes this helps :)
So, by using this function and by setting the LANGUAGE variable, you should be able to have a functioning i18n system that is not dependent upon a system's locale installation. Here is a sample from a method I created for a language class. Look for it on http://wiki.creativecommons.org/Developer soon:
  function Init ()
  {
    // set the LANGUAGE environmental variable
    // This one for some reason makes a difference FU@#$%^&*!CK
    // and when combined with bind_textdomain_codeset allows one
    // to set locale independent of server locale setup!!!
    if ( false == putenv("LANGUAGE=" . $this->_language ) )
      CCDebug::Log(sprintf("Could not set the ENV variable LANGUAGE = %s",
                 $this->_language));
    // set the LANG environmental variable
    if ( false == putenv("LANG=" . $this->_language ) )
      CCDebug::Log(sprintf("Could not set the ENV variable LANG = %s",
                 $this->_language));
    // if locales are not installed in locale folder, they will not
    // get set! This is usually in /usr/lib/locale
    // Also, the backup language should always be the default language
    // because of this...see the NOTE in the class description
    // Try first what we want but with the .utf8, which is what the locale
    // setting on most systems want (and is most compatible
    // Then just try the standard lang encoding asked for, and then if
    // all else fails, just try the default language
    // LC_ALL is said to be used, but it has nasty usage in some languages
    // in swapping commas and periods! Thus try LC_MESSAGE if on one of
    // those systems.
    // It is supposedly not defined on WINDOWS, so am including it here
    // for possible uncommenting if a problem is shown
    //
    // if (!defined('LC_MESSAGES')) define('LC_MESSAGES', 6);
    // yes, setlocale is case-sensitive...arg
    $locale_set = setlocale(LC_ALL, $this->_language . ".utf8",
                    $this->_language . ".UTF8",
                    $this->_language . ".utf-8",
                    $this->_language . ".UTF-8",
                    $this->_language,
                    CC_LANG);
    // if we don't get the setting we want, make sure to complain!
    if ( ( $locale_set != $this->_language && CC_LANG == $locale_set) ||
       empty($locale_set) )
    {
      CCDebug::Log(
        sprintf("Tried: setlocale to '%s', but could only set to '%s'.",            $this->_language, $locale_set) );
    }
    $bindtextdomain_set = bindtextdomain($this->_domain,
                 CC_LANG_LOCALE . "/" . $this->_locale_pref );
    if ( empty($bindtextdomain_set) )
      CCDebug::Log(
      sprintf("Tried: bindtextdomain, '%s', to directory, '%s', " .
          "but received '%s'",
          $this->_domain, CC_LANG_LOCALE . "/" . $this->_locale_pref,
          $bindtextdomain_set) );
    bind_textdomain_codeset($this->_domain, "UTF-8");
    $textdomain_set = textdomain($this->_domain);
    if ( empty($textdomain_set) )
      CCDebug::Log(sprintf("Tried: set textdomain to '%s', but got '%s'",
                 $this->_domain, $textdomain_set));
  } // end of method Init ()
I had problems with German "umlauts" when using gettext. So, this is how it can be resolved:
I've put these lines itom my PHP code:
$domain = "messages";
bindtextdomain($domain, "path_to_messages_dir");
bind_textdomain_codeset($domain, 'ISO-8859-15');
textdomain($domain);
It works!

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)