stlawson
By:
stlawson

Locale not working on my Ubuntu DigitalOcean Droplet

September 13, 2013 3.8k views
I developed a PHP shopping cart for a website that has world wide sales. Because of it's international traffic, I'm using setlocale() and localeconv() to display 'USD' next to all prices whenever the locale is not en_US. This works great when my code is hosted at places like Hostgator, Westhost, even GoDaddy, but it's not working on my DigitalOcean "Droplet". Here's what I get from localeconv() from Hostgator: $locale_info => Array (18) ( | ['decimal_point'] = String(1) "." | ['thousands_sep'] = String(0) "" | ['int_curr_symbol'] = String(4) "USD " | ['currency_symbol'] = String(1) "$" | ['mon_decimal_point'] = String(1) "." | ['mon_thousands_sep'] = String(1) "," | ['positive_sign'] = String(0) "" | ['negative_sign'] = String(1) "-" | ['int_frac_digits'] = Integer(1) 2 | ['frac_digits'] = Integer(1) 2 | ['p_cs_precedes'] = Integer(1) 1 | ['p_sep_by_space'] = Integer(1) 0 | ['n_cs_precedes'] = Integer(1) 1 | ['n_sep_by_space'] = Integer(1) 0 | ['p_sign_posn'] = Integer(1) 1 | ['n_sign_posn'] = Integer(1) 1 | ['grouping'] => Array (0) | ( | ) | ['mon_grouping'] => Array (2) | ( | | ['0'] = Integer(1) 3 | | ['1'] = Integer(1) 3 | ) ) And here's what it looks like on Disital Ocean: $locale_info => Array (18) ( | ['decimal_point'] = String(1) "." | ['thousands_sep'] = String(0) "" | ['int_curr_symbol'] = String(0) "" | ['currency_symbol'] = String(0) "" | ['mon_decimal_point'] = String(0) "" | ['mon_thousands_sep'] = String(0) "" | ['positive_sign'] = String(0) "" | ['negative_sign'] = String(0) "" | ['int_frac_digits'] = Integer(3) 127 | ['frac_digits'] = Integer(3) 127 | ['p_cs_precedes'] = Integer(3) 127 | ['p_sep_by_space'] = Integer(3) 127 | ['n_cs_precedes'] = Integer(3) 127 | ['n_sep_by_space'] = Integer(3) 127 | ['p_sign_posn'] = Integer(3) 127 | ['n_sign_posn'] = Integer(3) 127 | ['grouping'] => Array (0) | ( | ) | ['mon_grouping'] => Array (0) | ( | ) ) I'm guessing there is something that I need to install/setup. Anybody know what? I'm running LAMP on Ubuntu 12.04
4 Answers
Follow up:

From an SSH commandline, "locale" produces:

LANG=en_US.UTF-8
LANGUAGE=en_US:en
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
I changed:

setlocale(LC_MONETARY, 'en_US');

to:

setlocale(LC_MONETARY, 'en_US.UTF-8');

And my shopping cart is no longer displaying 'USD' after the prices (which is what I designed it to do). Now I just have to find a way to test it with a non-US locale to see if the 'USD' suffixes return.
Yeah, now, on my DigitalOcean Droplet, localconv() returns:

$locale_info => Array (18)
(
| ['decimal_point'] = String(1) "."
| ['thousands_sep'] = String(0) ""
| ['int_curr_symbol'] = String(4) "USD "
| ['currency_symbol'] = String(1) "$"
| ['mon_decimal_point'] = String(1) "."
| ['mon_thousands_sep'] = String(1) ","
| ['positive_sign'] = String(0) ""
| ['negative_sign'] = String(1) "-"
| ['int_frac_digits'] = Integer(1) 2
| ['frac_digits'] = Integer(1) 2
| ['p_cs_precedes'] = Integer(1) 1
| ['p_sep_by_space'] = Integer(1) 0
| ['n_cs_precedes'] = Integer(1) 1
| ['n_sep_by_space'] = Integer(1) 0
| ['p_sign_posn'] = Integer(1) 1
| ['n_sign_posn'] = Integer(1) 1
| ['grouping'] => Array (0)
| (
| )
| ['mon_grouping'] => Array (2)
| (
| | ['0'] = Integer(1) 3
| | ['1'] = Integer(1) 3
| )
)

I don't know if the following contributed to this success, but I also did the following:

sudo apt-get install language-pack-en-base

sudo dpkg-reconfigure locales

and then rebooted.
Have another answer? Share your knowledge.