1
votes

I am new to gettext.

Here is my setup: /Apache 2.2 PHP 5.3.6 Windows 7/

I have following code in the Apache/htdocs/test/index.php

<?php
    $language = 'de_DE'; 
    $translatefile = 'messages'; 
    setlocale(LC_ALL, $language);
    putenv("LANG=".$language); 
    bindtextdomain($translatefile, 'C:/locale'); 
    textdomain($translatefile); 

    echo gettext("Hello World!");
?>

I used PoEdit to generate the necessary translations under locale/de_DE/LC_MESSAGES/messsages.po & messages.mo The charset I used was UTF-8

When I visit http://localhost/test, the result is Hello World! when it should be Hall Welt!

As a test, I opened command prompt and navigated to the test folder. Then I typed in

php index.php 

The result that appeared in the console was

Hall Welt!

I am not sure why it is not working with Apache.

2

2 Answers

2
votes

The problem was not solved the traditional way. I had to use the php-gettext instead of the gettext(php_gettext.dll) comes default built into php.

Details:

1) Download php-gettext from here: https://launchpad.net/php-gettext/+download 2) Add the following files in the same folder as index.php: - gettext.inc - gettext.php - streams.php 3) This is the new index.php

  <?php
    error_reporting(E_ALL | E_STRICT);

    // define constants
    define('PROJECT_DIR', realpath('./'));
    define('LOCALE_DIR', 'C:/locale');
    define('DEFAULT_LOCALE', 'de_DE');

    require_once('gettext.inc');

    $supported_locales = array('en_US', 'sr_CS', 'de_CH');
    $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!");
    ?> 

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

  ;extension=php_gettext.dll

visit http://localhost/test and you will see Hall Welt!

0
votes

I had the same problem today on wampserver 2.2, apache 2.2 windows 7, 64 BITS. I unistalled it and installed 32 BITS. It works.