problem with translating with gettext

Hi
i have this folder structure:
Locale
     en_US
         LC_MESSAGES

and the .po file in it
i use poedit, and it recognize my files and strings for translation

in my file i have this:
$language = "en_US";
putenv("LANG=".$language);
setlocale(LC_ALL, $language);

//set text domain
$domain = "messages";
bindtextdomain($domain, "Locale");
textdomain($domain);

echo __("this is one");
echo _e("what should i do?");



if (function_exists("gettext")) {
    echo "you got gettext";
}else{
    echo "No gettext";
}

Open in new window


i have gettext enables. those strings are for testing obviously.
but when i go to the page i get this:
Fatal error: Call to undefined function __()

 i don't see where can be the issue.
LVL 1
derridaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnoldCommented:
Line 10 of your post
0
Ray PaseurCommented:
Are you using a single underscore as the alias for gettext()?  It looks like a double in the question, but it should be a single underscore.

Is it installed correctly?  Can you verify with phpinfo()?
http://php.net/manual/en/gettext.installation.php
http://php.net/manual/en/book.gettext.php
0
derridaAuthor Commented:
hi ray
my phpinfo say: it is enabled
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

F PCommented:
The gettext alias is one _ and you have 2, __, written above. Could be why right there.
0
F PCommented:
In the future, wrap things in an if(function_exists('function_name'){ } if you get that error, and for classes use class_exists(). better error handling, and you can put it in a try/catch to throw an exception. Gives you much more information.
0
derridaAuthor Commented:
hey i have tried with one, two and, _e. none makes the translation, while poedit scan them. and that if else is just a quick check nothing more.
0
F PCommented:
Windows or Linux? Check that the dll exists in the extension directory if it's Windows, and with Linux you'll probably have a .ini with the .so extension addition in a separate directory, but make sure that it's there and the user running the webserver service has access to it.

Other than that, change your code like this and see if it errors...

echo __("this is one");

Open in new window


change to make it:

echo gettext("this is one");exit;/*

Open in new window


For a quick test.
0
derridaAuthor Commented:
hi
the dll file is in the ext folder, i am on windows. the extension is enabled on the ini file. i have tried your quick test and it is not translated.
any other idea? i can't seem to figure it out.
0
F PCommented:
It not translating is a different troubleshooting path than function not found. One problem before another, so is that error now gone with what you changed from my code/test?
0
derridaAuthor Commented:
yes. with gettext there is no error but it is not translating enev though the po file doas have a translation
0
F PCommented:
Do you use Mac? I've always used gettext with Poedit and it's a great validation utility of the Messages file with the ability to validate its contents...

https://poedit.net/

Otherwise can you post the file / contents please so I can reproduce it on my system?
0
derridaAuthor Commented:
i don't use mac. i have used and am using it not once. this is all the file have at the moment, wanted to start with a little test .
0
F PCommented:
Did you set the character content type in the po file, and send the same header in your HTML request?

<?php
//this:
setlocale( LC_MESSAGES, 'en_US')
//or this:
setlocale( LC_MESSAGES, 'en_US.utf8')
//or this:
setlocale( LC_MESSAGES, '')

//this:
putenv("LANG= en_US.utf8");
//or this:
putenv("LANGUAGE= en_US.utf8");

//this:
bindtextdomain('mydomain', dirname(__FILE__).'/locale');
//or this:
bindtextdomain("*", dirname(__FILE__).'/locale');
//or this:
bindtextdomain('*', dirname(__FILE__).'/locale');

//setting or not "bind_textdomain_codeset()":
bind_textdomain_codeset("mydomain", 'UTF-8');

http://php.net/manual/en/book.gettext.php
0
derridaAuthor Commented:
in the po file i have this :
"Language: en_US\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

i have tried all these options and the same result. only when i used LC_MESSAGES and not LC_ALL i got the message it is deprecated.
0
F PCommented:
OK, so I would first suggest that your locale within the po file of en_US be modified to en_US.utf8 as in the code example above and test - along with the language as well.
0
derridaAuthor Commented:
the same.
this is the start of the po file, maybe you will see there something?

msgid ""
msgstr ""
"Project-Id-Version: greentopsdb\n"
"POT-Creation-Date: 2015-06-28 22:05+0200\n"
"PO-Revision-Date: 2015-06-28 22:06+0200\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: en_US.utf8\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 1.7.6\n"
"X-Poedit-Basepath: .\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Poedit-SourceCharset: UTF-8\n"
"X-Poedit-KeywordsList: _e;__;gettext\n"
"X-Poedit-SearchPath-0: ../../..\n"
"X-Poedit-SearchPath-1: .\n"
"X-Poedit-SearchPathExcluded-0: ../../../node_modules\n"
0
F PCommented:
This pops out

"X-Poedit-KeywordsList: _e;__;gettext\n"

With the __

and I would put an absolute path on these two lines:

"X-Poedit-SearchPath-0: ../../..\n"
"X-Poedit-SearchPathExcluded-0: ../../../node_modules\n"
0
derridaAuthor Commented:
i removed the 2 __ and put one.
and there should be relative paths no?

paths
0
F PCommented:
Relative I'm not a fan of. When a script executes, you should hope the relative path is the same for everything to work, and it's always one of those guessing games in my mind.
0
Ray PaseurCommented:
Does this script work correctly?
<?php // demo/temp_derrida.php

/**
 * http://www.experts-exchange.com/questions/28693088/problem-with-translating-with-gettext.html
 */
error_reporting(E_ALL);

echo _('Hello World');

Open in new window

0
derridaAuthor Commented:
hi
nothing works. tries playing with paths, with utf8 and whatever i could think off. drive me crazy. any other idea?
0
F PCommented:
Check your bindtextdomain according to the path. You just have the folder name.

// Set language to German
putenv('LC_ALL=de_DE');
setlocale(LC_ALL, 'de_DE');

// Specify location of translation tables
bindtextdomain("myPHPApp", "./locale");

// Choose domain
textdomain("myPHPApp");

// Translation is looking for in ./locale/de_DE/LC_MESSAGES/myPHPApp.mo now

Your code just says 'Locale'

$language = "en_US";
putenv("LANG=".$language);
setlocale(LC_ALL, $language);

//set text domain
$domain = "messages";
bindtextdomain($domain, "Locale");
textdomain($domain);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
F PCommented:
Does this apply?

I just wanted to say that gettext won't work on WAMP Server 2.4 / 64 Bit, see the thread I posted here:

Title: Gettext doesn't work on WAMP 64 Bits
http://forum.wampserver.com/read.php?2,120770,120770#msg-120770

I haven't tested with only apache 64 Bit, so, I don't know if the issue is related to apache or WAMP. Anyway, to make it work with WAMP, install the 32 Bit version of WAMP and only do this:

define('LOCALE_DIR', '<root_dir_of_your_po_files>'); //ie: C:/wamp/www/your_app/locale
$locale = '<your_locale>'; //ie: es_CO
$domain = 'your_gettext_domain'; //ie: messages
putenv('LC_ALL=' . $locale);
bindtextdomain($domain, LOCALE_DIR);
textdomain($domain);
echo _('<your_string>'; // ie: Hello world

"setlocale" nor setting the LANG, LANGUAGE, and LC_MESSAGES environment variables seems to be necessary under windows. I got it working by setting "LC_ALL" only.
0
derridaAuthor Commented:
ok
this line made me check what is going on there, and i realized that according to what i have it should be the $language variable and not the $domain one.
it still didn't translated. out of frustration i restarted my apache, and then the translation works?
do i have to restart apache every time? this is bizzare. and idea?
0
Ray PaseurCommented:
do i have to restart apache every time? this is bizzare. and idea?
Well, that's why I asked you if the script I posted earlier works!  There is a method to debugging in which we try to isolate issues and eliminate causes.  First step would be to see if all of the default settings are sufficiently correct that a simple use of the _() function alias was correct.  Then we could go on to the other things.  It's usually better to know which steps were important and which were irrelevant, and that's why we don't just try whatever we can think of.

If you still have problems, please post another question, and good luck with your project, ~Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.