Files
Jeremy c11f31a710 feat: expose resolved store currency and locale on the API
The extension renders the same price two ways: Insights reads currency from
price_cache[].currency, but /api/meta-extraction returns a bare price with no
currency anywhere, so the Track tab can't format it. Store::currency and
Store::locale are accessors, not in $appends, so they never reach toArray().
Reading the raw settings blob is not a workaround - most stores have no
locale_settings key, so the real value only exists once the accessor applies
the app-config fallback.

Adds the resolved accessor values (ISO 4217 code, BCP-47 tag) to:

- StoreResource, which meta-extraction embeds as `store`
- MetaExtractionResource, at the top level next to `price`, falling back to
  the app defaults when no store resolved and `store` is `{}`
- StoreTransformer, so GET /api/stores can format prices too

Locale is converted from the stored ICU form (en_AU) to BCP-47 (en-AU) at the
API boundary; Intl.NumberFormat rejects the underscore form. The accessors and
stored settings are unchanged.

currency/locale are deliberately absent from PaginationHandler's allowed
fields, since Spatie translates those into a select() and these are accessors,
not columns. Instead, a sparse fieldset forces `settings` into the select so
the accessors read real data rather than silently reporting the app-level
fallback.

Purely additive; no existing field changes.
2026-08-08 10:26:39 +10:00

54 lines
1.5 KiB
PHP
Executable File

<?php
namespace App\Services\Helpers;
use Locale;
use Symfony\Component\Intl\Currencies;
use Symfony\Component\Intl\Locales;
class LocaleHelper
{
/**
* This is only used to populate settings to the database, after
* that it should be retrieved from the settings table.
*/
public static function getAppLocaleSettings(): array
{
return [
'locale' => config('app.locale', 'en'),
'currency' => CurrencyHelper::getCurrencyFromLocale(
config('app.currency_locale', 'en_US')
)['iso'] ?? 'USD',
];
}
/**
* Convert an ICU locale ID (`en_AU`, as stored in settings) to a BCP-47 language
* tag (`en-AU`). API clients feed this straight into `Intl.NumberFormat`, which
* rejects the underscore form.
*/
public static function toBcp47(?string $locale): string
{
return str_replace('_', '-', (string) $locale);
}
public static function getAllLocalesAsOptions(): array
{
return collect(Locales::getNames())
->mapWithKeys(fn ($name, $locale) => [
$locale => $locale.' ('.Locale::getDisplayLanguage($locale).')',
])
->toArray();
}
public static function getAllCurrencyLocalesAsOptions(): array
{
return collect(Currencies::getNames())
->mapWithKeys(fn ($name, $iso) => [
$iso => $iso.' ('.$name.')',
])
->sort()
->toArray();
}
}