0

I've been trying to use this gettext functionality to add localization functionality to my website. Please keep in mind I'm not a webdev and this field is rather unknow to me. After following several tutorials I have installed the extension and have enabled it in the php.ini. I'm also using PoEdit for creating the .po files. My php code looks like this :

<?php
$language = "en_US";
putenv("LC_ALL={$locale}");

$domain = 'messages';
bindtextdomain($domain, './locale'); 
bind_textdomain_codeset($domain, 'UTF-8'); 
textdomain($domain); 
?>

Then I call it like this :

<p class="welcome"><?=_('welcome');?></p>

As my understanding goes "_" is an alieas for the gettext method. Both dont seem to work. The messages .po file looks like this :

msgid ""
msgstr ""
"Project-Id-Version: \n"
"POT-Creation-Date: 2017-04-23 11:27+0300\n"
"PO-Revision-Date: 2017-04-23 11:36+0300\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 2.0.1\n"
"X-Poedit-Basepath: ../../..\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"Last-Translator: \n"
"Language: en\n"
"X-Poedit-SearchPath-0: index.php\n"

#: index.php:63
msgid "welcome"
msgstr "Welcome"

#: index.php:64
msgid "to-leadersplay"
msgstr "to Leadersplay"

My directory structure looks like this :

enter image description here

NO matter what I do the strings that are being displayed seem to be the keys and not the translations from the file. In my current example I get welcome but I expect to get Welcome (with a capital W)

Uri Popov
  • 2,127
  • 2
  • 25
  • 43

2 Answers2

0

I think the problem is that

$language = "en_US";
putenv("LC_ALL={$locale}");

should be

$locale = "en_US";
putenv("LC_ALL={$locale}");

instead.

Because after this change the snippet works just fine for me.

akond
  • 15,865
  • 4
  • 35
  • 55
  • 1
    This was a mistake on my part from copying the code. I actually tried it like that. Testing the code on my hosting provider seems to work so I just connected my IDE to there and I'm going to be publishing the files there. Cant seem to get it to work on localhost. – Uri Popov Apr 23 '17 at 10:42
  • Make sure you put proper directory to `bindtextdomain` then. – akond Apr 23 '17 at 11:50
0

see my answer @ Gettext example not working on Windows 7 - XAMPP

In Windows gettext looks at the 'LANG' variable which must be set before Apache starts,

set LANG=nl_NL
xampp-control
Tim
  • 11
  • 1