0

At work we have decided to translate our site into 3 additional languages and I have been looking into how to do this. I've spent the better part of the day searching google and stackoverflow, trying every different solution but I can't make it work.

I'm starting to fear that it is impossible to do in Windows 7 Home edition (My development machine).

// C:\Apache24\htdocs\locale\en_GB\LC_MESSAGES\messages.mo
// C:\Apache24\htdocs\locale\en_GB\LC_MESSAGES\messages.po
msgid ""
msgstr ""
"Project-Id-Version: iArbete 1.0.0\n"
"Report-Msgid-Bugs-To: ******** \n"
"Last-Translator: ********\n"
"Language: en_GB\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

msgid "iArbete"
msgstr "workIng"


// C:\Apache24\htdocs\index.php
if(isset($_GET["locale"]))
{
    $locale = $_GET["locale"];
}
else
{
    $locale = 'en_GB';
}

putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
echo getenv("LC_ALL");

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

echo _("iArbete");

echo _("iArbete"); Should translate to "workIng" but it does not. Or am I not getting the concept?

--- More information --------------------------------------------------------

I've made some changes and recompiled the mo file and this is how it looks now.

// C:\Apache24\htdocs\locale\en_GB\LC_MESSAGES\messages.mo
// C:\Apache24\htdocs\locale\en_GB\LC_MESSAGES\messages.po
msgid ""
msgstr ""
"Project-Id-Version: iArbete 1.0.0\n"
"Report-Msgid-Bugs-To: ******** \n"
"Last-Translator: ********\n"
"Language: en_GB\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

msgid "workIng"
msgstr "iArbete"

msgid "Menu"
msgstr "Meny"


// C:\Apache24\htdocs\index.php
if(isset($_GET["locale"]))
{
    $locale = $_GET["locale"];
}
else
{
    $locale = 'sv_SE';
    $locale = 'en_GB';
//  $locale = 'ar_AE';
}

putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
//  echo getenv("LC_ALL");

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

echo _("workIng");
echo _("Menu");

msgid "workIng" is translated to "iArbete" msgid "Menu" is not translated to "Meny"

What is going on? It feels so weird that only the first word get translated. So I rearranged the words in the po and recompiled but still "workIng" is the only word that gets translated??

I've uploades my files to google drive here if anyone would like to have a look.

Cirshiss
  • 81
  • 9
  • 1
    What's your question? – Jamhead Feb 18 '17 at 01:08
  • why does it not work? – Cirshiss Feb 18 '17 at 01:39
  • If you do a `var_dump(__DIR__)` just before the `bindtextdomain`, does it show the directory which contains the `locale` subdirectory? What’s the full path to your `messages.mo` file? – lxg Feb 19 '17 at 12:25
  • Yes it does. `var_dump(__DIR__)` = string(18) "C:\Apache24\htdocs" `C:\Apache24\htdocs\locale\en_GB\LC_MESSAGES\messages.mo` `C:\Apache24\htdocs\locale\en_GB\LC_MESSAGES\messages.po` – Cirshiss Feb 19 '17 at 12:32
  • I noticed that the mo file is not encoded to `utf-8` but `ANSI`, that might be normal, I don't know. I tried an online compiler and that to left me with an `ANSI` mo file. These lines are a part of the headers in the po file. `"Content-Type: text/plain; charset=UTF-8\n"` `"Content-Transfer-Encoding: 8bit\n"` Can that have something to do with my problem? – Cirshiss Feb 19 '17 at 13:25
  • Well, well.. Transfered the files to the computer at work and everything works flawlessly. *sigh* It would be nice to figure out what is wrong with my machine at home but I'm not going to waste more energy on this problem since it does not hinder me from using `_("gettext")` and continue the development. Thanks for trying anyway. – Cirshiss Feb 22 '17 at 16:49

1 Answers1

0

You'll need to convert your PO file into a MO file using msgfmt:

The letters PO in ‘.po’ files means Portable Object, to distinguish it from ‘.mo’ files, where MO stands for Machine Object. [...]

PO files are meant to be read and edited by humans, and associate each original, translatable string of a given package with its translation in a particular target language. [...]

MO files are meant to be read by programs, and are binary in nature. [...]

Once the PO file is complete and dependable, the ‘msgfmt’ program is used for turning the PO file into a machine-oriented format, which may yield efficient retrieval of translations by the programs of the package, whenever needed at runtime (see MO Files). See msgfmt Invocation, for more information about all modes of execution for the ‘msgfmt’ program.

— The GNU gettext manual, sections 1.4 and 1.5

deltab
  • 2,498
  • 23
  • 28
  • 1
    I have done this. It doesn't matter. An since I can't show whats in it, since it's compiled, I posted the source of my message.po – Cirshiss Feb 18 '17 at 10:23
  • I'm thinking it might be something with cache. But I clean out cache whenever I make a change, so I don't know, anymore... Perhaps some other file. But also when I change something in the po file I delete and recomplie the mo file. – Cirshiss Feb 18 '17 at 10:34