I use two seperate tutorials and have followed them carefully (this and this) but i still cant get gettext to translate anything. I'm on a hosted linux webserver with PHP 8.2.1 and gettext is enabled in the phpinfo.
I can not figure out what is wrong. Or is gettext() just not a good solution for multi-langual sites?
The po-file is this
msgid ""
msgstr ""
"Project-Id-Version: \n"
"POT-Creation-Date: 2014-03-22 14:45+0100\n"
"PO-Revision-Date: 2023-01-28 12:39+0100\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: en_US\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Poedit-KeywordsList: _;gettext;gettext_noop\n"
"X-Poedit-Basepath: .\n"
"X-Poedit-SourceCharset: UTF-8\n"
"X-Generator: Poedit 3.2.2\n"
# Test 1
msgid "SCHOOL"
msgstr "School"
PHP looks like this:
$language = "en_US.utf8";
putenv("LANG=".$language);
setlocale(LC_ALL, $language);
$domain = "messages";
bindtextdomain($domain, "Locale");
textdomain($domain);
if (!function_exists("gettext")){echo "gettext is not installed<br>";}
echo gettext("SCHOOL") . " (Works if small letters)<br>";
echo "<br><br>";
echo "Current locale: " . setlocale(LC_ALL, 0);
echo "<br><br>";
var_dump(system('locale -a'));
Output like this:
SCHOOL (Works if small letters)
Current locale: en_US.utf8
C C.UTF-8 en_AG en_AG.utf8 en_AU.utf8 en_BW.utf8 en_CA.utf8 en_DK.utf8 en_GB.utf8 en_HK.utf8 en_IE.utf8 en_IL en_IL.utf8 en_IN en_IN.utf8 en_NG en_NG.utf8 en_NZ.utf8 en_PH.utf8 en_SG.utf8 en_US.utf8 en_ZA.utf8 en_ZM en_ZM.utf8 en_ZW.utf8 POSIX sv_SE.utf8 string(10) "sv_SE.utf8"
I used PoEdit to create the message.po file and put it in en_US/LC_MESSAGES/message.po
Greatful for hints on how to get it to work. Have tried and searched the web for days.
It's strange it does not work, it is a basic tutorial i follow.
Translations are read from .mo files, not .po files. The .po files are just the source files for the binary .mo files.
The tutorials you linked explain how to create .mo files from .po files.
Related
I'm trying to make a website localizable in multiple language. I'm using XAMPP to test translations. The problem is that the string "Text not translated" does not change and I have already tried multiple solutions. I already modified the string into php.ini which enables/disables the gettext function. Here is my test website project:
htdocs/Traduzioni/Locale/it-IT/LC_MESSAGES/
Under LC_MESSAGES there are 2 files named:
- messages.po;
- messages.mo
<?php
$language = "it-it";
putenv("LANG=".$language);
setlocale(LC_ALL, $language);
$domain = "messages";
bindtextdomain($domain, "C:\xampp\htdocs\Traduzioni\Locale");
textdomain($domain);
echo gettext("Text not translated");
?>
^ This is the content of the index.php file contained into Traduzioni folder.
msgid ""
msgstr ""
"Project-Id-Version: \n"
"POT-Creation-Date: \n"
"PO-Revision-Date: \n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=ISO-8859-1\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: it_IT\n"
"X-Generator: Poedit 2.0.7\n"
# Test1
msgid "Text not translated"
msgstr "Testo tradotto"
^ And this is the content of the messages.po file under the it-it folder.
I already tried to restart Apache and tried the function_exists function to make sure that the package is installed correcly.
I'm running this server on Windows (10). Thanks in advance :)
I am trying to implement gettext() for building a multi-lingual PHP web app.
Here's what I did:
Installed GNU gettext package in C:\Program Files
Installed POedit
Checked my php.ini file, and gettext was enabled
extension=php_gettext.dll
Checked if gettext() is installed using function_exists()
Set folder structure : test-project/locale/en_US/LC_MESSAGES/
Created the translation files using POedit
Checked translation files have been generated properly, i.e., messages.po and messages.mo
I was following this tutorial: https://blog.udemy.com/php-gettext/
Here's my .po file:
msgid ""
msgstr ""
"Project-Id-Version: \n"
"POT-Creation-Date: 2017-06-09 15:45+0530\n"
"PO-Revision-Date: 2017-06-09 17:07+0530\n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 2.0.2\n"
"X-Poedit-Basepath: .\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"Language: en_US\n"
# Test1
msgid "Hello"
msgstr "Hallo"
Here's my .php file
$language = "en_US";
putenv("LANG=".$language);
setlocale(LC_ALL, $language);
//Set the text domain as messages
$domain = "messages";
bindtextdomain($domain, "Locale");
textdomain($domain);
echo gettext("Hello");
Any kind of help would be appreciable.
Ok, Let go right to the code,
PHP Code:
$locale = "Dutch_Netherlands.1252";
echo PHP_VERSION . "<br />";
echo (putenv("LC_ALL=$locale") ? "Success" : "Failure"). "<br />";
echo setlocale(LC_ALL, $locale) . "<br />";
echo bindtextdomain("messages", "C:\\Hello") . "<br />";
echo textdomain("messages") . "<br />";
echo gettext("Hello") . "<br />";
Directory structure is:
C:\Hello\Dutch_Netherlands.1252\LC_MESSAGES\messages.mo
C:\Hello\Dutch_Netherlands.1252\LC_MESSAGES\messages.po
messages.po contents:
msgid ""
msgstr ""
"Project-Id-Version: messages\n"
"POT-Creation-Date: 2014-12-14 13:27+0330\n"
"PO-Revision-Date: 2014-12-14 15:41+0330\n"
"Last-Translator: xxxxxxxxxxxx\n"
"Language-Team: \n"
"Language: nl_NL\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=windows-1252\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 1.7.1\n"
"X-Poedit-Basepath: .\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
#: ..\..\..\xxxxxx:531
msgid "Hello"
msgstr "LOADED"
Output:
5.5.12
Success
Dutch_Netherlands.1252
C:\Hello
messages
Hello
Process Monitor results:
httpd.exe QueryDirectory C:\Hello SUCCESS
httpd.exe CreateFile C:\Hello\en_US\LC_MESSAGES\messages.mo PATH NOT FOUND: Desired Access: Generic Read, Disposition: Open, Options: Synchronous IO Non-Alert, Non-Directory File, Attributes: N, ShareMode: Read, Write, AllocationSize: n/a
httpd.exe CreateFile C:\Hello\en\LC_MESSAGES\messages.mo PATH NOT FOUND: Desired Access: Generic Read, Disposition: Open, Options: Synchronous IO Non-Alert, Non-Directory File, Attributes: N, ShareMode: Read, Write, AllocationSize: n/a
I cant see any reason for the above code to not work. So here I am to ask you if you see anything I dont. Everything goes smooth and yet Apache wants to read the English version.
Windows 7 SP1 x64
Based on my tests and searches,
It is a common problem with Apache on x64 versions of Windows,
I am not sure if this bug is in PHP, GetText library or Apache, but it is a serious bug.
For now using 32bit version of Wamp can solve the problem. I found no workaround for VS.Php's default webservice.
I'm trying to get gettext to work in a localserver, but it's just not working. It's not giving me any kind of error, it just returns my string in spanish instead of translating it to english.
This is index.php
<html>
<?php
$locale = "en_US";
putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
bindtextdomain("messages", "./locale");
bind_textdomain_codeset("messages", 'UTF-8');
textdomain("messages");
?>
<?= _("Volver al Panel") ?>
</htlm>
This is messages.po, located in locale/en_US/LC_MESSAGES/
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL#ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2014-10-01 11:31-0300\n"
"PO-Revision-Date: 2014-10-01 11:38-0300\n"
"Last-Translator: FULL NAME <EMAIL#ADDRESS>\n"
"Language-Team: LANGUAGE <LL#li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 1.5.4\n"
#: index.php:10
msgid "Volver al Panel"
msgstr "Back to Panel"
Of course, i also create the messages.mo with "sudo msgfmt messages.po" without a problem.
The thing is it should return "Back to Panel" instead of "Volver al Panel", am i wrong?
I figured out what the problem was and could fix it.
I used the command locale -a and realized I didn't have a locale named en_US installed, but I did have an en_US.utf8 locale, so i switched them both and it worked.
I am using gettext _('someString') to do i18n, and it is working fine in my windows dev XAMPP platform;
However, when I upload to a Linux CentOS hosting, my program display the translated in non utf-8 string.
For example, in Chinese version, it displays �Τ� instead of 用戶 (browser encoding is UTF8)
I can see the label is 用戶, if I change the browser encoding to BIG5.
here is my messages.po file (ANSI as UTF-8):
msgid ""
msgstr ""
"Project-Id-Version: ERP\n"
"POT-Creation-Date: 2014-04-25 14:50+0800\n"
"PO-Revision-Date: 2014-04-28 09:50+0800\n"
"Language-Team: \n"
"Language: zh_HK\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 1.5.7\n"
"X-Poedit-KeywordsList: _;gettext;gettext_noop\n"
"X-Poedit-Basepath: .\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
#: C:\xampp\test.php:34
msgid "User"
msgstr "用戶"
thank you so much
I finally find the solution. In Linux, the locale folder must be zh_HK.ut8 (I was using zh_HK), because the linux has 3 similar locales: zh_HK zh_HK.big5hkscs zh_HK.utf8
http://php.net/manual/en/book.gettext.php#91187