Silver Light
Silver Light

Reputation: 45932

PHP: does gettext require LC_MESSAGES dirs?

To translate my PHP app I use compiled in gettext module. Here is a directory tree of translations made according to docs:

locale/
  cs_CZ/
    LC_MESSAGES/
       messages.po
       messages.mo
  de_DE/
    LC_MESSAGES/
       messages.po
       messages.mo
  fr_FR/
    LC_MESSAGES/
       messages.po
       messages.mo

Question: is it possible to get rid of LC_MESSAGES catalog? Will PHP be able to find translations if I use this structure?

locale/
  cs_CZ/
     messages.po
     messages.mo
  de_DE/
     messages.po
     messages.mo
  fr_FR/
     messages.po
     messages.mo

My PHP that switches translations:

<?php
    setlocale(LC_ALL, 'fr_FR.UTF-8');
    bindtextdomain("messages", "locale");
    bind_textdomain_codeset("messages", 'UTF-8');
    textdomain("messages");
?>

Thank you in advance.

Upvotes: 5

Views: 3386

Answers (4)

igorsantos07
igorsantos07

Reputation: 4686

Answering the question in the title: it does require.

Why?
To my current understanding, LC_MESSAGES is one of the many localization categories you could use with gettext. You can change to another category using dcgettext(), for instance.

However, that's a bit confusing since gettext is supposed to be mainly used for text translation, and it might be weird to use it to localize dates, money, numbers or charsets.

Upvotes: 0

defro
defro

Reputation: 56

If you really want to do that, you can use this composer package : gettext/gettext

So you can have the folder organization as you desire or even something like that :

locales/
   cs_CZ.mo
   cs_CZ.po
   de_DE.mo
   de_DE.po
   fr_FR.mo
   fr_FR.po

Upvotes: 0

takeshin
takeshin

Reputation: 50658

I'm afraid LC_MESSAGES is a requirement.

Correct me if I'm wrong, but I think it has something to do with the gettext cache.

Upvotes: 2

mario
mario

Reputation: 145482

The only feasible workaround is creating a symlink LC_MESSAGES -> . in each language subdirectory. (But this complicates PHP application installation. FTP seldomly can create symlinks.)

Upvotes: 4

Related Questions