1

I am having a strange problem with gettext on a server I am forced to use to redesign a website. The customer is used to having Network Solutions as provider so currently it's not an option to change providers. Anyway the problem is the following:

  • I thought it might not have gettext installed so I checked phpinfo and it is enabled.

    gettext GetText Support enabled

  • I also checked using

    if (!function_exists("gettext")) { echo "gettext is not installed\n"; } else { echo "gettext is supported\n"; }

The output says gettext is supported

I then thought perhaps the server is having issues with the native gettext so I tried using this one https://launchpad.net/php-gettext/. I uploaded it to the server and it's not working either.

The configuration in my header is

putenv("LC_ALL=".$locale.".utf8");
putenv("LANGUAGE=".$locale."utf8");
setlocale(LC_ALL, $locale.".utf8");
bindtextdomain($domain, "./locale");
bind_textdomain_codeset($domain, "UTF-8");
textdomain($domain);`

The $locale and $domain server are assigned before the functions.

The same code with gettext worked fine on my local apache server.

What could be the issue? As the site is not really that big I am considering using a database storage for the translations although I am not really into the idea but I am out of ideas why this is happening.

I have my locale file correctly located in ./locale/language_code/LC_MESSAGES/file.po and file.mo when I echo the return of the bindtextdomain it shows the full path to the locale.

Any suggestions?

Mihail Minkov
  • 2,463
  • 2
  • 24
  • 41
  • What is the output of `var_dump(file_exists('./locale/language_code/LC_MESSAGES/file.mo'));` and `var_dump(fileperms('./locale/language_code/LC_MESSAGES/file.mo'));`? Note, the .po file is not used by gettext. – Mike Jan 02 '14 at 22:55
  • The output is: `bool(true)` and `int(33204)` – Mihail Minkov Jan 02 '14 at 23:00
  • I do know only the .mo file is used I just wanted to make it clear everything is in place :) – Mihail Minkov Jan 02 '14 at 23:01
  • Are any errors being displayed? Have you checked the error log file? What happens? Is the default text showing up instead of the translated text, or nothing at all? Maybe try [Zend Translate](http://framework.zend.com/manual/2.0/en/modules/zend.i18n.translating.html) and see if that gets you anywhere. – Mike Jan 02 '14 at 23:10
  • There's no error_log, I think the hosting company is kind of crappy. The default text shows instead of the translation. Doesn't Zend Translate require the FW? – Mihail Minkov Jan 02 '14 at 23:20
  • The only dependency for Zend Translate is [intl](http://php.net/intl) if it falls back to the default locale. You can see if this is loaded by doing `var_dump(extension_loaded('intl'));`. See also [this page](http://framework.zend.com/manual/2.0/en/modules/zend.i18n.translating.html). – Mike Jan 02 '14 at 23:37

2 Answers2

1

You're confusing locales with translation files. Locales are useful regional- and languaged-based constants for formatting text, dates, etc. appropriately for the end user. PHP's gettext functions will use the locale to seek the correct translation look-up files, but that's the only property they share.

When you use setlocale, you do not supply a file path, you supply a locale identifier. These quasi-constants vary from system to system (and may be absent), which may ultimately be part of your issue with Network Solutions, but PHP's examples are useful.

You can then use bindtextdomain to specify the translation directory path, and then call textdomain to specify the domain to be used in the current scope.

SitePoint has an article called "Localizing PHP Applications "The Right Way"" that may be helpful to you.

Jacob Budin
  • 9,753
  • 4
  • 32
  • 35
  • Hello @jacob-budin I had already checked the SitePoint article and my setlocale, bindtextdomain and textdomain functions are configured correctly. As I mentioned previously it's working on my localhost and the same configuration works on a differente hosting provider. What I do find interesting in your comment is the part of the constants. Any recommendation on how to check if they're being passed correctly or if they are absent in the hosting? – Mihail Minkov Jan 02 '14 at 23:24
  • @mihail_ov I was concerned because your question states "my locale file correctly located" and go on to mention a `.po` file. Did you check what `setlocale` is returning? It should return `false` if the locale doesn't exist. I do not know a way to get a list of locales (on shared Web hosting), but there are usually only ~5 or so variations of any given region and language. – Jacob Budin Jan 02 '14 at 23:44
  • it's not returning anything, should it do that. I assigned it to a variable and apparently it's returning false. I am handling es_MX for mexican spanish, I don't think they shouldn't have it defined. What might the problem be then? – Mihail Minkov Jan 02 '14 at 23:51
  • `setlocale` needs to return `true`, or nothing you have will work. Check if `es_MX.UTF-8` works. If not, contact Network Solutions and ask them for a list of installed locales on your system. You might need to rejigger what you have to something more common (e.g. `es_ES.UTF-8`, which is Spanish [International]). – Jacob Budin Jan 02 '14 at 23:58
  • If you have command line access you can execute `locale -a` to find out which locales are installed. – Mike Jan 03 '14 at 00:01
  • @JacobBudin I tried changing to es_ES.UTF-8 but it still doesn't work. @Mike I don't have shell access, I tried to use `exec("locale -a")` but what I get is `Warning: exec() has been disabled for security reasons in /.../server/index.php on line 103` – Mihail Minkov Jan 03 '14 at 00:06
  • @mihail_ov You really need to contact Network Solutions. They may not have any locales additional installed. Many Linux OSs do not include them as part of the default installation. I hope I've been helpful. – Jacob Budin Jan 03 '14 at 00:08
  • I'll try contacting them and I'll let you guys know what happened. Thanks! – Mihail Minkov Jan 03 '14 at 00:09
  • Do you consider it's wise to use database for fixed text translations if this doesn't work in the end? – Mihail Minkov Jan 03 '14 at 00:19
  • You can also try `echo file_get_contents('/usr/share/i18n/SUPPORTED');` for Debian-based systems. For other distros, you will have to look up where they store their list of supported locales. – Mike Jan 03 '14 at 02:44
  • Hey @Mike I tried this last one and it showed a bunch of locales, probably around 100 or more and between them there was the one I actually need: `es_MX.UTF-8 UTF-8 es_MX ISO-8859-1` both in UTF-8 and ISO-8859-1. What could be the problem I contacted Network Solutions but they told me I must call for support which is kind of lame in my opinion, but... I still haven't called though. – Mihail Minkov Jan 04 '14 at 18:37
  • Hey @JacobBudin I contacted them and apparently they don't support locales on shared hosting, I posted it below as answer to the thread. Thanks for your help. – Mihail Minkov Jan 16 '14 at 17:01
0

After contacting Network Solutions via chat they told me that using their shared hosting you cannot use locales. There's also the problem with specific .htaccess syntax especially for their server which is a pain in the a**. I would definitely not recommend their hosting for shared servers. Anyway, thanks to @jacob-budin and @mike for helping.

Mihail Minkov
  • 2,463
  • 2
  • 24
  • 41