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

[VUFIND-1618] Eliminate setlocale() call from Bootstrapper. #3526

Merged
merged 4 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 2 additions & 13 deletions module/VuFind/src/VuFind/Bootstrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,23 +144,12 @@ protected function initSystemStatus(): void
}

/**
* Initializes locale and timezone values
* Initializes timezone value
*
* @return void
*/
protected function initLocaleAndTimeZone(): void
protected function initTimeZone(): void
{
// Try to set the locale to UTF-8, but fail back to the exact string from
// the config file if this doesn't work -- different systems may vary in
// their behavior here.
setlocale(
LC_ALL,
[
"{$this->config->Site->locale}.UTF8",
"{$this->config->Site->locale}.UTF-8",
$this->config->Site->locale,
]
);
date_default_timezone_set($this->config->Site->timezone);
}

Expand Down
12 changes: 6 additions & 6 deletions module/VuFind/src/VuFind/Service/CurrencyFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,19 @@ class CurrencyFormatter
* Constructor
*
* @param string $defaultCurrency Default currency format (ISO 4217) to use (null
* for default from locale)
* for default from system locale)
* @param string $locale Locale to use for number formatting (null for
* default system locale)
*/
public function __construct($defaultCurrency = null)
public function __construct($defaultCurrency = null, $locale = null)
{
// Initialize number formatter:
$locale = setlocale(LC_MONETARY, 0);
$locale ??= setlocale(LC_MONETARY, 0);
$this->formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);

// Initialize default currency:
if (null === $defaultCurrency) {
$localeInfo = localeconv();
$defaultCurrency = isset($localeInfo['int_curr_symbol'])
? trim($localeInfo['int_curr_symbol']) : '';
$defaultCurrency = trim($this->formatter->getTextAttribute(NumberFormatter::CURRENCY_CODE) ?: '');
}
$this->defaultCurrency = empty($defaultCurrency) ? 'USD' : $defaultCurrency;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public function __invoke(
}
$config = $container->get(\VuFind\Config\PluginManager::class)
->get('config');
return new $requestedName($config->Site->defaultCurrency ?? null);
return new $requestedName(
$config->Site->defaultCurrency ?? null,
$config->Site->locale ?? null
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,17 @@ public function testFormatting()
{
// test default settings
$cc = new \VuFind\Service\CurrencyFormatter();
$this->assertEquals('$3.00', $cc->convertToDisplayFormat(3));
$this->assertEquals('€3.00', $cc->convertToDisplayFormat(3, 'EUR'));
$this->assertEquals('$3,000.00', $cc->convertToDisplayFormat(3000));
$this->assertEquals('€3,000.00', $cc->convertToDisplayFormat(3000, 'EUR'));

// test override default currency
$cc = new \VuFind\Service\CurrencyFormatter('EUR');
$this->assertEquals('€3.00', $cc->convertToDisplayFormat(3));
$this->assertEquals('$3.00', $cc->convertToDisplayFormat(3, 'USD'));
$this->assertEquals('€3,000.00', $cc->convertToDisplayFormat(3000));
$this->assertEquals('$3,000.00', $cc->convertToDisplayFormat(3000, 'USD'));

// test override default locale
$cc = new \VuFind\Service\CurrencyFormatter(null, 'de_DE');
$this->assertEquals("3.000,00\u{a0}€", $cc->convertToDisplayFormat(3000));
$this->assertEquals("3.000,00\u{a0}\$", $cc->convertToDisplayFormat(3000, 'USD'));
}
}