I'm using NumberFormatter::formatCurrency to display formatted currency values, like this;
$value = 395;
$fmt = numfmt_create('en_GB', NumberFormatter::CURRENCY);
echo numfmt_format_currency($fmt, $value, 'gbp');
On my Windows dev box, and Centos UAT box, this outputs the desired £395.
But on the production Centos box, it outputs gbp395.
Any idea what is missing? I have checked intl extension is enabled.
Is there something wrong with my locale files perhaps? When I type
locale -a
in command line, I get a long list of locales, of which en_GB is one.
Maybe en_GB is not a valid locale on your system. Try en_GB.UTF-8 or en_GB.ISO-8559-1 f.e.
The NumberFormatter class is incorrect and the GBP needs to be in capital like so:'GBP'
Try this:
$value = 395;
$currencyFormat = new NumberFormatter('en_GB', NumberFormatter::CURRENCY);
echo $currencyFormat->formatCurrency($value, 'GBP');
This will render £395.00
Related
setlocale is taking a country and a language as parameters.
'money_format' is taking amount and other params.
But how can I tell a currency to PHP?
What if I want to use EUR in Australia?
like javascript can:
var formatter = new Intl.NumberFormat('en-AU', {
style: 'currency',
currency: 'EUR',
});
formatter.format(2500);
Chrome obviously understands that it is a foreign currency that need to be formatted to international currency format like so "EUR 2,500.00".
IE does not understand it, but it offers something "€2,500.00"
PHP (that is how I use it)
setlocale(LC_MONETARY, 'en-AU');
return money_format('%.2n', 2500);
gives "$2,500.00" (I know it is Ubuntu should be blamed)
It takes some default currency for Australia and I cannot find a way to change it. Is there a formatting library or something that I'm missing that can help?
Java script does not require installing locales individually, like Ubuntu does.
Maybe should we rely on Browsers in this case?
You can use the NumberFormatter in the intl extension instead.
$fmt = new NumberFormatter('en_AU.UTF8', NumberFormatter::CURRENCY);
print $fmt->formatCurrency(2500, 'EUR');
And it doesn't require to install every locale individually, since it ships with its own locale data.
If I use the money_format function, it prints the currency amount in the Hungarian format correctly:
$ php -a
Interactive mode enabled
php > setlocale(LC_MONETARY, 'hu_HU');
php > $number = 1234.5672;
php > echo money_format('%n', $number)."\n";
1.234,57 Ft
But if I try with the NumberFormatter class it returns the wrong format
$ php -a
Interactive mode enabled
php > $f = new NumberFormatter('hu_HU', NumberFormatter::CURRENCY);
php > $f->setAttribute($f::FRACTION_DIGITS, 2);
php > echo $f->formatCurrency('1234.5672', 'HUF')."\n";
1 234,57 Ft
I think it's a bug.
It is depend on Which operating System used. it will differ based on Windows and Linux.
Some time it doesn't work in windows
I'm trying to use money_format() to obviously add separators and a currency symbol to a number I have. I've seen plenty of examples where they work, but for some reason the currency symbol does not display.
$_price = '10995';
setlocale(LC_MONETARY, 'en_GB.UTF-8');
echo money_format('%n', $_price);
results in:
10995.00
I can do this and it nearly works, however as I understand, that's not the point?:
echo money_format('£%n', $_price);
results in:
£10995.00
I can see that this also formats the number close to how I want, without the .00:
echo '£'.number_format( $_price );
results in:
£10,995
From Sean's link in the above comment, I can see that the locale for en_GB was not created.
once created with sudo locale-gen en_GB.UTF-8 I had the currency symbol
Setting the locale information as:
$locale = 'it_IT';
$moneyFormat = '%n';
setlocale(LC_MONETARY, $locale.'.utf8');
$optionPrice = money_format($moneyFormat, floatval($option->optionPrice));
The price is shown as:
€ 23
instead
23 €
I've found the locale identifier p_sign_posn to set the position of the currency symbol but I don't know how to modify it and apparently it's set by default to 1 for the IT locale.
Anybody could enlightenment me to find a clever solution?
Thanks in advance.
EDIT: The locale for Italy is apparently installed on the system
vmamp#AMP30:~> locale -a | grep it
it_CH
it_CH.utf8
it_IT
it_IT.utf8
it_IT#euro
To use locale-aware function you have to have the requested locale installed on your computer. If the requested locale is not presented, the call to setlocale fails and nothing is changed.
For instance, I have es_ES locale installed, but no it_IT:
setlocale(LC_MONETARY, 'es_ES.utf8');
echo money_format($moneyFormat, floatval(12))
// ⇒ 12,00 €
setlocale(LC_MONETARY, 'en_US.utf8');
echo money_format($moneyFormat, floatval(12))
// ⇒ $12.00
setlocale(LC_MONETARY, 'it_IT.utf8');
echo money_format($moneyFormat, floatval(12))
// FAIL!!!
// ⇒ $12.00
As you can see, the latter call was ignored (setlocale returned 0.) So the problem you encountered is not with a position of eurosign; it’s likely a lack of it_IT l10n installed.
For italian currency mode you can do:
$money = substr(money_format('%.2n',$number), 4).' €';
given $number = 77500
you get
77.500,00 €
I've searched hours for a solution for this including in the documentation.
trying to use gettext for hebrew translations,
using PHP 5.3.1 and wamp,
it prints out "hello world" and not the Hebrew equivalent
$directory = '/locale';
$domain = 'messages';
$locale ="he_IL";
putenv("LANG=".$locale); //not needed for my tests, but people say it's useful for windows
setlocale( LC_ALL, $locale);
bindtextdomain($domain, $directory);
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');
echo _("hello world");
I use poedit to create the mo/po files, they are located on:
./locale/he_IL/LC_MESSAGES/messages.mo
the php file is at "./"
Why don't I get the hebrew text?
Ok solved,
Had to update to PHP version 5.3.5/5.3.10
and because I'm using windows I had to use this locale "Hebrew_Israel.1255"
instead of "he_IL" (that's how windows calls the hebrew locale anyway).
Of course I had to also rename the folders in the ./locale to "Hebrew_Israel.1255"
Now the system successfully chooses the locale
as pointed out by MarcB, PHP isn't magically going to translate the words into different language for you. but there are several different solutions out there to help you out. you can make use of google translator API to convert the text between different languages for example.
try {
$gt = new Gtranslate;
echo "Translating [Hello World] from English to German => ".$gt->english_to_german("hello world")."<br/>";
echo "Translating [Ciao mondo] Italian to English => ".$gt->it_to_en("Ciao mondo")."<br/>";
} catch (GTranslateException $ge) {
echo $ge->getMessage();
}
you can read more about google translator API here http://code.google.com/p/gtranslate-api-php/
You should check that "he_IL" locale is installed in your system. I don't know how to do it in Windows, but in Linux you can run "locale -a"(http://linux.about.com/library/cmd/blcmdl1_locale.htm) to see all locales installed.