PHP gettext on Windows

前端 未结 6 1518
你的背包
你的背包 2020-12-13 22:03

There are some tutorials out there for gettext (with Poedit)... unfortunately, it\'s mostly for a UNIX environment. And even more unfortunate is that I am running my WAMP se

相关标签:
6条回答
  • 2020-12-13 22:38

    I had the same problem, and wasted almost a day or so on it. Finally, I found a very simple solution, namely to uninstall WAMP Server (version 2.1), and install a newer version (WampServer 2.1e - 32 bits). It's strange, but it solved the problem completely.

    Here is the sample code that worked for me:

    <?php
    
        $locale = "deu_DEU";
    
        putenv("LC_ALL=$locale");
        setlocale(LC_ALL, $locale);
    
        bindtextdomain("greetings", "./locale");
        textdomain("greetings");
    
        echo _("Hello World");
    
    ?>
    

    Hope it helps.

    0 讨论(0)
  • 2020-12-13 22:41

    It was because I didn't have the locales installed.

    0 讨论(0)
  • 2020-12-13 22:44

    I didn't made a full investigation on this but I'm sure that one of your mistakes is that you used invalid locale codes - even on Windows the locales codes are the same - these are gettext locales and they are cross platform.

    Please try to use just "de" as locale code, it should work. Also be sure that your PHP has gettext extension installed and activated (check with phpinfo).

    0 讨论(0)
  • 2020-12-13 22:44

    This is the solution that worked for me. This works on the latest wampserver. (source: http://www.extradrm.com/blog/?p=1035 )

    1) Download php-gettext from here: https://launchpad.net/php-gettext/+download and unzip it

    2) Add the following files found in the package root in the same folder as test-language.php : - gettext.inc - gettext.php - streams.php

    3) Open your php.ini and comment out wampserver php_gettext.dll:

    ;extension=php_gettext.dll

    4) This is the new test file test-language.php

    <?php
    error_reporting(E_ALL | E_STRICT);
    
    // define constants
    define('PROJECT_DIR', realpath('./'));
    define('LOCALE_DIR', 'C:/wamp/www/test/locale');
    define('DEFAULT_LOCALE', 'es_ES');
    
    require_once('gettext.inc');
    
    $supported_locales = array('en_US', 'sr_CS', 'de_CH','es_ES');
    $encoding = 'UTF-8';
    
    $locale = (isset($_GET['lang']))? $_GET['lang'] : DEFAULT_LOCALE;
    
    //var_dump($locale);die();
    
    // gettext setup
    T_setlocale(LC_MESSAGES, $locale);
    // Set the text domain as 'messages'
    $domain = 'messages';
    bindtextdomain($domain, LOCALE_DIR);
    // bind_textdomain_codeset is supported only in PHP 4.2.0+
    if (function_exists('bind_textdomain_codeset'))
    bind_textdomain_codeset($domain, $encoding);
    textdomain($domain);
    
    echo gettext("HELLO_WORLD");
    ?>
    

    After all of this you must create locale folder, en_US ( or other language ) folder, LC_MESSAGES folder and then put the messages.po file.

    0 讨论(0)
  • 2020-12-13 22:51

    Might be too late but I was having similar issues until I enabled "short open tag" in my WAMP php settings

    0 讨论(0)
  • 2020-12-13 22:59

    I ran into problems while working with gettext on my local machine, and after some search i found this page which solved my problem: http://www.kipras.com/blog/getting-gettext-to-work-in-apache-on-windows/96

    I am quoting from the web page:

    On Linux servers (or any other servers apart windows), the way to do it is this:

    setlocale(LC_MESSAGES, “en_US”);
    

    The correct way to set locality on windows is this:

    putenv(“LC_ALL=en_US”);
    
    0 讨论(0)
提交回复
热议问题