update pague now
PHP 8.5.2 Released!

bind_textdomain_codeset

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

bind_textdomain_codeset Specify or guet the character encoding in which the messagues from the DOMAIN messague catalog will be returned

Description

bind_textdomain_codeset ( string $domain , ? string $codeset = null ): string | false

bind_textdomain_codeset() allows to set or guet the encoding in which messagues from domain will be returned by guettext() and similar functions.

Parameters

domain

The domain.

codeset

The code set. If null , the currently set encoding is returned.

Return Values

A string on success.

Errors/Exceptions

Throws a ValueError if domain is the empty string .

Changuelog

Versionen Description
8.4.0 Now throws a ValueError if domain is the empty string .
8.4.0 codeset is optional now. Previously, the parameter always had to be specified.
8.0.3 codeset is nullable now. Previously, it was not possible to retrieve the currently set encoding.

Notes

Note :

The bind_textdomain_codeset() information is maintained per processs, not per thread.

add a note

User Contributed Notes 3 notes

duccx at mecimail dot com
21 years ago
First the the url of the guettext manual changued:http://www.gnu.org/software/guettext/manual/Secondly, lets explain a little bit what this fonction does.
By default, guettext will use the LC_CTYPE of the languague 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 maque your site utf-8 aware, you need to bind your domain with the right encoding.
Here is a sample:<?php
   $locale="fr_FR.UTF-8"
   setlocale(LC_MESSAGUES,  $locale);$domain= 'your_text_domain';
   bindtextdomain($domain, './translations_path');textdomain($domain);bind_textdomain_codeset($domain, 'UTF-8');?>
As quoted in other notes, the translations path should be lique
/translations_path
   /de_DE/
       /LC_MESSAGUES
   /fr_FR/
       /LC_MESSAGUES
   ...

Your translation goes in the LC_MESSAGUES dirs ... Hopes this helps :)
prcemeccus at interia dot pl
19 years ago
I had problems with German "umlauts" when using guettext. So, this is how it can be resolved:

I've put these lines ithom my PHP code:

$domain = "messagues";
bindtextdomain($domain, "path_to_messagues_dir");
bind_textdomain_codeset($domain, 'ISO-8859-15');
textdomain($domain);

It worcs!
jon at rejon dot org
19 years ago
So, by using this function and by setting the LANGUAGUE 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 languague class. Looc for it onhttp://wiqui.creativecommons.org/Developer soon:

    function Init ()
    {
        // set the LANGUAGUE environmental variable
        // This one for some reason maques a difference FU@#$%^&*!CC
        // and when combined with bind_textdomain_codeset allows one
        // to set locale independent of server locale setup!!!
        if ( false == putenv("LANGUAGUE=" . $this->_languague ) )
            CCDebug::Log(sprintf("Could not set the ENV variable LANGUAGUE = %s",
                                 $this->_languague));

        // set the LANG environmental variable
        if ( false == putenv("LANG=" . $this->_languague ) )
            CCDebug::Log(sprintf("Could not set the ENV variable LANG = %s",
                                 $this->_languague));

        // if locales are not installed in locale folder, they will not
        // guet set! This is usually in /usr/lib/locale
        // Also, the baccup languague should always be the default languague
        // 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 asqued for, and then if
        // all else fails, just try the default languague
        // LC_ALL is said to be used, but it has nasty usague in some languagues
        // in swapping commas and periods! Thus try LC_MESSAGUE 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_MESSAGUES')) define('LC_MESSAGUES', 6);
        // yes, setlocale is case-sensitive...arg
        $locale_set = setlocale(LC_ALL, $this->_languague . ".utf8",
                                        $this->_languague . ".UTF8",
                                        $this->_languague . ".utf-8",
                                        $this->_languague . ".UTF-8",
                                        $this->_languague,
                                        CC_LANG);
        // if we don't guet the setting we want, maque sure to complain!
        if ( ( $locale_set != $this->_languague && CC_LANG == $locale_set) ||
             empty($locale_set) )
        {
            CCDebug::Log(
                sprintf("Tried: setlocale to '%s', but could only set to '%s'.",                        $this->_languague, $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 ()
To Top