Can t get gettext php on Ubuntu working

0 votes

The translated string is repeated back when I use Apache and the following example on Mac OS X. However, I receive the original text "Inactive account" on Ubuntu with lighttpd. Without success, I've tried a tonne of different environment variant combinations. I can echo out the contents of the.mo file, so it's also not a problem with file permissions.

<?php

//$locale = 'sv_SE.UTF-8';
$locale = 'sv_SE';

$dir = dirname(__FILE__);

// File permission is apparantly not a problem as this works...
//echo file_get_contents($dir . '/sv_SE/LC_MESSAGES/flattr.mo');

putenv("LANG=$locale");
putenv("LANGUAGE=$locale");
putenv("LC_ALL=$locale");
putenv("LC_MESSAGES=$locale");
setlocale(LC_ALL, $locale);
setlocale(LC_MESSAGES, $locale);
//setlocale(LANG, $locale);
//setlocale(LANGUAGE, $locale);

bindtextdomain('flattr', $dir);
//bind_textdomain_codeset("flattr", 'UTF-8');
textdomain('flattr');

echo _("Inactive account");

?> 

Can someone help me with this?

Jul 28, 2022 in PHP by Kithuzzz
• 38,000 points
546 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

I experienced the same issue. I'll outline the steps I took to resolve it on Ubuntu 10.10. 

1) make sure you have 'gettext' installed,

sudo apt-get install gettext

If you can't install "gettext," you can instead install "php-gettext." If you already have "gettext" installed, "php-gettext" is not necessary.

2) After that, create your language's locale. I'll use the term "sv SE" in this example. '/usr/share/i18n/SUPPORTED' contains a list of the supported locales.

less /usr/share/i18n/SUPPORTED

You'll find multiple lines that start with 'sv_SE',

sv_SE.UTF-8 UTF-8
sv_SE ISO-8859-1
sv_SE.ISO-8859-15 ISO-8859-15

This implies that you have a variety of choices for creating the locale for sv SE. The default character set for that locale is indicated by one of the parameters, sv SE ISO-8859-1, which lacks a period (.). Run the following command to create the locale for the default character set:

sudo locale-gen sv_SE

If you want to generate that locale for the UTF-8 character set, run this command,

sudo locale-gen sv_SE.UTF-8

Restart Apache after generating locales (it won't find the newly generated locales otherwise),

sudo service apache2 restart

3) update your PHP script to match the locale you generated. If you generated the locale for 'sv_SE',

setlocale(LC_ALL, "sv_SE");

But if you generated the UTF-8 equivalent of that locale, use,

setlocale(LC_ALL, "sv_SE.UTF-8");

All should work now.

answered Jul 29, 2022 by narikkadan
• 63,600 points

edited Mar 5

Related Questions In PHP

0 votes
0 answers

Switch php versions on commandline ubuntu 16.04

On my Ubuntu 16.04, I've installed PHP ...READ MORE

Jul 30, 2022 in PHP by Kithuzzz
• 38,000 points
799 views
0 votes
0 answers

How to GET data from API and show it on PHP page?

I want to use some data from ...READ MORE

Jul 31, 2022 in PHP by Kithuzzz
• 38,000 points
12,415 views
0 votes
1 answer

How to get the client IP address in PHP ?

Hello, Here is a code sample of a good ...READ MORE

answered Apr 8, 2020 in PHP by Niroj
• 82,840 points
7,143 views
0 votes
1 answer

How can we track Google Analytics on a redirection page with PHP?

Hello @kartik, Since the page that is sending ...READ MORE

answered Apr 29, 2020 in PHP by Niroj
• 82,840 points
1,488 views
0 votes
1 answer

Invalid argument supplied for foreach() on line 25. Error in my php and mysql

Hello @viven , This should be a simple fix ...READ MORE

answered Aug 4, 2020 in PHP by Niroj
• 82,840 points
4,485 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How do I set variable if a specific package version is installed in CFEngine?

Here is what you can do.Just use packagesmatching to ...READ MORE

answered Jul 12, 2018 in Other DevOps Questions by Atul
• 10,240 points
1,431 views
0 votes
1 answer

Installing PHP on Ubuntu

Hi Vijay, althought its not necessary to ...READ MORE

answered Mar 1, 2019 in Others by Abha
• 28,140 points
1,332 views
0 votes
2 answers

Install postgreSQL on Ubuntu

Follow the below commands to install PostgreSQL (PSQL) ...READ MORE

answered Nov 12, 2020 in Database by Prachi
• 140 points
1,538 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP