Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alpine and locales problem #219

Closed
kusmierz opened this issue Apr 11, 2016 · 7 comments
Closed

Alpine and locales problem #219

kusmierz opened this issue Apr 11, 2016 · 7 comments

Comments

@kusmierz
Copy link

kusmierz commented Apr 11, 2016

Hello,

I'm using php:X.X-[fpm-]alpine image and can't use localeconv() function.

$ docker run --rm -it php:7.0-alpine php -r 'var_dump(localeconv());'
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"]=>
  int(127)
  ["frac_digits"]=>
  int(127)
  ["p_cs_precedes"]=>
  int(127)
  ["p_sep_by_space"]=>
  int(127)
  ["n_cs_precedes"]=>
  int(127)
  ["n_sep_by_space"]=>
  int(127)
  ["p_sign_posn"]=>
  int(127)
  ["n_sign_posn"]=>
  int(127)
  ["grouping"]=>
  array(0) {
  }
  ["mon_grouping"]=>
  array(0) {
  }
}
$ docker run --rm -it php:7.0-alpine php -r 'setlocale(LC_ALL, "de_DE"); var_dump(localeconv());'

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"]=>
  int(127)
  ["frac_digits"]=>
  int(127)
  ["p_cs_precedes"]=>
  int(127)
  ["p_sep_by_space"]=>
  int(127)
  ["n_cs_precedes"]=>
  int(127)
  ["n_sep_by_space"]=>
  int(127)
  ["p_sign_posn"]=>
  int(127)
  ["n_sign_posn"]=>
  int(127)
  ["grouping"]=>
  array(0) {
  }
  ["mon_grouping"]=>
  array(0) {
  }
}

There is no locale -a, locale-gen nor localedef, even directory /usr/share/locale/...

How could I make it work?

@shouze
Copy link
Contributor

shouze commented May 25, 2016

Hi @kusmierz you need to make your own Dockerfile that inherits from the alpine one and install libicu and php intl extension.

I think you can close this issue too ;)

@kusmierz
Copy link
Author

kusmierz commented May 25, 2016

# apk add icu icu-libs icu-dev
(1/3) Installing icu-libs (56.1-r0)
(2/3) Installing icu (56.1-r0)
(3/3) Installing icu-dev (56.1-r0)
Executing busybox-1.24.1-r7.trigger
OK: 249 MiB in 48 packages

# docker-php-ext-install intl
+ cd /usr/src/php/ext/intl
+ phpize
Configuring for:
PHP Api Version:         20151012
Zend Module Api No:      20151012
Zend Extension Api No:   320151012
+ ./configure
(...)
Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-non-zts-20151012/
find . -name \*.gcno -o -name \*.gcda | xargs rm -f
find . -name \*.lo -o -name \*.o | xargs rm -f
find . -name \*.la -o -name \*.a | xargs rm -f 
find . -name \*.so | xargs rm -f
find . -name .libs -a -type d|xargs rm -rf
rm -f libphp.la       modules/* libs/*

# php -m | grep intl
intl

doesn't help either.

See also gliderlabs/docker-alpine#144

@shouze
Copy link
Contributor

shouze commented May 25, 2016

@kusmierz yes... you're right in fact... this is a bit disappointing, thanks for pointing that issue... I'm looking for a solution too as I will need it.

@shouze
Copy link
Contributor

shouze commented May 25, 2016

@kusmierz BTW localeconv has nothing related to intl in fact... I've been misled.

Why do you need localconv in fact? If you have to deal with number or currency formating, languages detection & various other l10n subjects intl it the right tool for your app:

php > $fmt = numfmt_create( 'en_US', NumberFormatter::CURRENCY);
php > echo numfmt_format_currency($fmt, 1234567.891234567890000, "EUR")."\n";
€1,234,567.89
php > $fmt = numfmt_create( 'fr_FR', NumberFormatter::CURRENCY);
php > echo numfmt_format_currency($fmt, 1234567.891234567890000, "EUR")."\n";
1 234 567,89 €

Your app can run independently of the system configured locale. Intl is more powerful as you can inject locales as parameters.

@kusmierz
Copy link
Author

kusmierz commented May 27, 2016

This is not entirely true. What I was trying to do is to use zend-i18n component (CurrencyFormat Helper in fact). Internally it uses intl extension - mainly NumberFormatter class which is equivalent to numfmt_* functions + Locale class. So, your example is correct, and the ZF2 component works good (with intl extension), but this is not what I needed.

I'm using localeconv() method to determine inter alia currency code to pass it as a default currency code to the CurrencyFormat Helper (basically something like $fmt->formatCurrency(1234567.891234567890000, localeconv()['int_curr_symbol'])). But I can't use localeconv() (as you can see above), nor other method to find currency code for given locale. And this is the problem and why I need locales.

So in short, I'd like to find currency code based on locale, which localeconv() function returns as int_curr_symbol key.

@shouze
Copy link
Contributor

shouze commented May 27, 2016

So in short, I'd like to find currency code based on locale.

Here again you can do that with php+intl:

echo (new NumberFormatter("de_DE", NumberFormatter::CURRENCY))->getSymbol(NumberFormatter::INTL_CURRENCY_SYMBOL);
// will output EUR
echo (new NumberFormatter("en_US", NumberFormatter::CURRENCY))->getSymbol(NumberFormatter::INTL_CURRENCY_SYMBOL);
// will output USD

This mainly avoid you to localegen every existing locale on your Linux distribution. It's a bad idea in general to use system generated locales, intl & icu lib usage is recommended for all i18n & l10n needs.

So I guess this time you can close this issue ;)

@kusmierz
Copy link
Author

Hm, you're right. Thanks for the help and patience :)

Closing now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants