Skip to content

Commit 13681eb

Browse files
enhance currency formatting with effective locale handling and update settings migration
1 parent b5223f1 commit 13681eb

4 files changed

Lines changed: 43 additions & 18 deletions

File tree

app/Helpers/CurrencyHelper.php

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,57 @@ private function convertForDisplay($amount)
1111
return $amount / 1000;
1212
}
1313

14-
public function formatForDisplay($amount, $decimals = 2, $locale = null, $ignoreOverride = false)
14+
/**
15+
* Gets the effective locale to use for formatting, considering global overrides.
16+
*
17+
* @param string|null $locale The requested locale
18+
* @param bool $ignoreOverride Whether to ignore the global override setting
19+
* @return string The effective locale to use
20+
*/
21+
private function getEffectiveLocale($locale = null, $ignoreOverride = false)
1522
{
16-
$locale = $locale ?: str_replace('_', '-', app()->getLocale());
23+
$effectiveLocale = $locale ?: str_replace('_', '-', app()->getLocale());
1724

1825
if (!$ignoreOverride) {
1926
$override = resolve(\App\Settings\GeneralSettings::class)->currency_format_override ?? null;
20-
if ($override) {
21-
$locale = $override;
27+
if ($override && $override !== '') {
28+
$effectiveLocale = $override;
2229
}
2330
}
2431

32+
return $effectiveLocale;
33+
}
34+
35+
/**
36+
* Formats a currency amount for display.
37+
*
38+
* @param mixed $amount The amount to format.
39+
* @param int $decimals Number of decimal places to use.
40+
* @param string|null $locale The locale to use for formatting (defaults to current application locale).
41+
* @param bool $ignoreOverride When true, bypasses the global currency format override setting.
42+
* @return string The formatted currency string.
43+
*/
44+
public function formatForDisplay($amount, $decimals = 2, $locale = null, $ignoreOverride = false)
45+
{
46+
$locale = $this->getEffectiveLocale($locale, $ignoreOverride);
47+
2548
$display = $this->convertForDisplay($amount);
2649

50+
// Bulgarian ('bg') locale: For numbers <= 9999, use comma as decimal separator and no thousands separator.
51+
// This follows common Bulgarian formatting conventions for small numbers, as per CLDR and local usage.
52+
// source: https://forum.opencart.com/viewtopic.php?t=144907
2753
if ($locale === 'bg' && $display <= 9999) {
2854
return number_format($display, $decimals, ',', '');
2955
}
3056

31-
if ($locale === 'es' && $display < 10000) {
57+
// Spanish ('es') locale: For numbers <= 9999, use comma as decimal separator and no thousands separator.
58+
// This matches Spanish formatting standards for small numbers, as seen in CLDR and government guidelines.
59+
if ($locale === 'es' && $display <= 9999) {
3260
return number_format($display, $decimals, ',', '');
3361
}
3462

63+
// Polish ('pl') locale: For numbers <= 9999, use comma as decimal separator and no thousands separator.
64+
// This reflects Polish conventions for small numbers, according to CLDR and local financial documents.
3565
if ($locale === 'pl' && $display <= 9999) {
3666
return number_format($display, $decimals, ',', '');
3767
}
@@ -54,13 +84,7 @@ public function prepareForDatabase($amount)
5484

5585
public function formatToCurrency(int $amount, $currency_code, $locale = null,)
5686
{
57-
$locale = $locale ?: str_replace('_', '-', app()->getLocale());
58-
59-
// overriding users locale with global override
60-
$override = resolve(\App\Settings\GeneralSettings::class)->currency_format_override ?? null;
61-
if ($override) {
62-
$locale = $override;
63-
}
87+
$locale = $this->getEffectiveLocale($locale, false);
6488

6589
$formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
6690

app/Http/Controllers/PreferencesController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public function index(Request $request)
2424
$generalSettings = $this->generalSettings;
2525

2626
$currencyOverrideAlert = null;
27-
if ($generalSettings->currency_format_override) {
27+
// Only show alert when an override is actively set (non-empty string)
28+
if ($generalSettings->currency_format_override && $generalSettings->currency_format_override !== '') {
2829
$currencyOverrideAlert = __('Global currency format override is enabled. All currency and number displays use :locale formatting. Your language preference does not affect currency formatting.', ['locale' => $generalSettings->currency_format_override]);
2930
}
3031

app/Settings/GeneralSettings.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
namespace App\Settings;
44

55
use Spatie\LaravelSettings\Settings;
6-
6+
use App\Helpers\CurrencyHelper;
77
class GeneralSettings extends Settings
88
{
99
public bool $store_enabled = false;
1010
public ?float $sales_tax = null;
1111
public string $credits_display_name = 'Credits';
12-
public ?string $currency_format_override = 'en';
12+
public ?string $currency_format_override = null;
1313
public ?string $recaptcha_version = null;
1414
public ?string $recaptcha_site_key = null;
1515
public ?string $recaptcha_secret_key = null;
@@ -70,11 +70,11 @@ public static function getCurrencyFormatOptions()
7070
{
7171
$options = [];
7272
$locales = config('app.available_locales');
73-
$helper = new \App\Helpers\CurrencyHelper();
73+
$helper = app(CurrencyHelper::class);
7474

7575
foreach ($locales as $locale) {
7676
// Format a sample amount (1234.56 in database units = 1234560)
77-
$sample = $helper->formatForDisplay(1234560, 2, $locale);
77+
$sample = $helper->formatForDisplay(1234560, 2, $locale, true);
7878
$options[$locale] = "$locale: $sample";
7979
}
8080

database/settings/2025_10_29_102720_add_currency_format_override_to_general_settings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
{
77
public function up(): void
88
{
9-
$this->migrator->add('general.currency_format_override', 'en');
9+
$this->migrator->add('general.currency_format_override', '');
1010
}
1111
};

0 commit comments

Comments
 (0)