GCarop
GCarop

Reputation: 11

PO file Gettext not working. Not reading po file translation

I need to translate my web to french but it's not translating. I did a lot of changes on the code but nothing seems to work. Any idea?

<?php
    $langSite = 'fr';
    
    setlocale(LC_ALL, $langSite);
    putenv("LANGUAGE=".$langSite);
    
    $langDomain = 'fr';
    bindtextdomain($langDomain, 'lang');
    
    bind_textdomain_codeset($langDomain, 'UTF-8');
    
    textdomain($langDomain);

    echo _("Password"); // returning Password instead of Mot de passe
?>

My fr.po file found at langs/fr/LC_MESSAGES looks like this:

msgctxt "Password"
msgid "Password"
msgstr "Mot de passe"

Upvotes: 1

Views: 1033

Answers (1)

Alexander Dobernig
Alexander Dobernig

Reputation: 753

1.) You have to create a compiled version with ending .mo from the .po file and put this on the server.

This can be done e.g. with Poedit https://poedit.net/

POEDIT is VERY NICE - it scans the complete directory for all files and puts all translation strings into the .po file and offers a GUI for doing it with checks and more. The paid ( one-time) version is even better it can use web services to translate your strings if you translate from your language to foreign languages.

2.) Remark: Usually your domain is only e.g. 'messages' and the gettext then uses the correct directory for the desired language. The filenames are then messages.po and messages.mo

I had a hard time getting it started as I also used en.po until checking that the file name must be the same as the domain.

3.) here is the code in my includefile to configute GETTEXT on my server: (before i have some checks if the language came from SESSION or GET but that is a different story.)

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

// Set the text domain as "messages"
$domain = "messages";
$domain_path= $_SERVER['DOCUMENT_ROOT'] ."/locale";
bindtextdomain($domain, $domain_path);
bind_textdomain_codeset($domain, "UTF-8" );
textdomain($domain);

The values up there reflect my path structure:

this is

/locale/
        /{language}/
                    LC_MESSAGES/
                                messages.po
                                messages.mo

4. you write

bindtextdomain($langDomain, 'lang');

My fr.po file found at langs /fr/LC_MESSAGES looks like this:

The second parameter for binddextdomain must be the same as the path but you have either a typo or another error because you have once lang and the other time langs ?? This must be the same!

Upvotes: 1

Related Questions