Add support for British English and allow the user to set a locale.

This commit is contained in:
James Cole
2020-04-19 06:51:40 +02:00
parent aa786eaaf3
commit c398aa2b69
17 changed files with 78 additions and 42 deletions

View File

@@ -154,14 +154,14 @@ class MonthReportGenerator implements ReportGeneratorInterface
$journals[$index]['invoice_date'] = $journalRepository->getMetaDateById($journal['transaction_journal_id'], 'invoice_date'); $journals[$index]['invoice_date'] = $journalRepository->getMetaDateById($journal['transaction_journal_id'], 'invoice_date');
} }
$locale = app('steam')->getLocale();
$return = [ $return = [
'journals' => $journals, 'journals' => $journals,
'currency' => $currency, 'currency' => $currency,
'exists' => count($journals) > 0, 'exists' => count($journals) > 0,
'end' => $this->end->formatLocalized((string) trans('config.month_and_day')), 'end' => $this->end->formatLocalized((string) trans('config.month_and_day', [], $locale)),
'endBalance' => app('steam')->balance($account, $this->end), 'endBalance' => app('steam')->balance($account, $this->end),
'dayBefore' => $date->formatLocalized((string) trans('config.month_and_day')), 'dayBefore' => $date->formatLocalized((string) trans('config.month_and_day', [], $locale)),
'dayBeforeBalance' => $dayBeforeBalance, 'dayBeforeBalance' => $dayBeforeBalance,
]; ];

View File

@@ -571,6 +571,7 @@ class AccountController extends Controller
*/ */
private function periodByCurrency(Carbon $start, Carbon $end, Account $account, TransactionCurrency $currency): array private function periodByCurrency(Carbon $start, Carbon $end, Account $account, TransactionCurrency $currency): array
{ {
$locale = app('steam')->getLocale();
$step = $this->calculateStep($start, $end); $step = $this->calculateStep($start, $end);
$result = [ $result = [
'label' => sprintf('%s (%s)', $account->name, $currency->symbol), 'label' => sprintf('%s (%s)', $account->name, $currency->symbol),
@@ -582,7 +583,7 @@ class AccountController extends Controller
switch ($step) { switch ($step) {
case '1D': case '1D':
// per day the entire period, balance for every day. // per day the entire period, balance for every day.
$format = (string) trans('config.month_and_day'); $format = (string) trans('config.month_and_day', [], $locale);
$range = app('steam')->balanceInRange($account, $start, $end, $currency); $range = app('steam')->balanceInRange($account, $start, $end, $currency);
$previous = array_values($range)[0]; $previous = array_values($range)[0];
while ($end >= $current) { while ($end >= $current) {

View File

@@ -111,6 +111,7 @@ class BillController extends Controller
if ($cache->has()) { if ($cache->has()) {
return response()->json($cache->get()); // @codeCoverageIgnore return response()->json($cache->get()); // @codeCoverageIgnore
} }
$locale = app('steam')->getLocale();
/** @var GroupCollectorInterface $collector */ /** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class); $collector = app(GroupCollectorInterface::class);
@@ -131,7 +132,7 @@ class BillController extends Controller
]; ];
foreach ($journals as $journal) { foreach ($journals as $journal) {
$date = $journal['date']->formatLocalized((string) trans('config.month_and_day')); $date = $journal['date']->formatLocalized((string) trans('config.month_and_day', [], $locale));
$chartData[0]['entries'][$date] = $bill->amount_min; // minimum amount of bill $chartData[0]['entries'][$date] = $bill->amount_min; // minimum amount of bill
$chartData[1]['entries'][$date] = $bill->amount_max; // maximum amount of bill $chartData[1]['entries'][$date] = $bill->amount_max; // maximum amount of bill

View File

@@ -179,14 +179,14 @@ class BudgetController extends Controller
if ($cache->has()) { if ($cache->has()) {
return response()->json($cache->get()); // @codeCoverageIgnore return response()->json($cache->get()); // @codeCoverageIgnore
} }
$locale = app('steam')->getLocale();
$entries = []; $entries = [];
$amount = $budgetLimit->amount; $amount = $budgetLimit->amount;
$budgetCollection = new Collection([$budget]); $budgetCollection = new Collection([$budget]);
while ($start <= $end) { while ($start <= $end) {
$spent = $this->opsRepository->spentInPeriod($budgetCollection, new Collection, $start, $start); $spent = $this->opsRepository->spentInPeriod($budgetCollection, new Collection, $start, $start);
$amount = bcadd($amount, $spent); $amount = bcadd($amount, $spent);
$format = $start->formatLocalized((string)trans('config.month_and_day')); $format = $start->formatLocalized((string)trans('config.month_and_day', [], $locale));
$entries[$format] = $amount; $entries[$format] = $amount;
$start->addDay(); $start->addDay();

View File

@@ -76,6 +76,7 @@ class PiggyBankController extends Controller
} }
$set = $repository->getEvents($piggyBank); $set = $repository->getEvents($piggyBank);
$set = $set->reverse(); $set = $set->reverse();
$locale =app('steam')->getLocale();
// get first event or start date of piggy bank or today // get first event or start date of piggy bank or today
$startDate = $piggyBank->start_date ?? new Carbon; $startDate = $piggyBank->start_date ?? new Carbon;
@@ -99,7 +100,7 @@ class PiggyBankController extends Controller
} }
); );
$currentSum = $filtered->sum('amount'); $currentSum = $filtered->sum('amount');
$label = $oldest->formatLocalized((string) trans('config.month_and_day')); $label = $oldest->formatLocalized((string) trans('config.month_and_day', [], $locale));
$chartData[$label] = $currentSum; $chartData[$label] = $currentSum;
$oldest = app('navigation')->addPeriod($oldest, $step, 0); $oldest = app('navigation')->addPeriod($oldest, $step, 0);
} }
@@ -110,7 +111,7 @@ class PiggyBankController extends Controller
} }
); );
$finalSum = $finalFiltered->sum('amount'); $finalSum = $finalFiltered->sum('amount');
$finalLabel = $today->formatLocalized((string) trans('config.month_and_day')); $finalLabel = $today->formatLocalized((string) trans('config.month_and_day', [], $locale));
$chartData[$finalLabel] = $finalSum; $chartData[$finalLabel] = $finalSum;
$data = $this->generator->singleSet($piggyBank->name, $chartData); $data = $this->generator->singleSet($piggyBank->name, $chartData);

View File

@@ -79,6 +79,7 @@ class ReportController extends Controller
if ($cache->has()) { if ($cache->has()) {
return response()->json($cache->get()); // @codeCoverageIgnore return response()->json($cache->get()); // @codeCoverageIgnore
} }
$locale = app('steam')->getLocale();
$current = clone $start; $current = clone $start;
$chartData = []; $chartData = [];
/** @var NetWorthInterface $helper */ /** @var NetWorthInterface $helper */
@@ -110,7 +111,7 @@ class ReportController extends Controller
/** @var array $netWorthItem */ /** @var array $netWorthItem */
foreach ($result as $netWorthItem) { foreach ($result as $netWorthItem) {
$currencyId = $netWorthItem['currency']->id; $currencyId = $netWorthItem['currency']->id;
$label = $current->formatLocalized((string) trans('config.month_and_day')); $label = $current->formatLocalized((string) trans('config.month_and_day', [], $locale));
if (!isset($chartData[$currencyId])) { if (!isset($chartData[$currencyId])) {
$chartData[$currencyId] = [ $chartData[$currencyId] = [
'label' => 'Net worth in ' . $netWorthItem['currency']->name, 'label' => 'Net worth in ' . $netWorthItem['currency']->name,

View File

@@ -85,15 +85,16 @@ class Controller extends BaseController
$this->middleware( $this->middleware(
function ($request, $next) { function ($request, $next) {
$locale = app('steam')->getLocale();
// translations for specific strings: // translations for specific strings:
$this->monthFormat = (string) trans('config.month'); $this->monthFormat = (string) trans('config.month', [], $locale);
$this->monthAndDayFormat = (string) trans('config.month_and_day'); $this->monthAndDayFormat = (string) trans('config.month_and_day', [], $locale);
$this->dateTimeFormat = (string) trans('config.date_time'); $this->dateTimeFormat = (string) trans('config.date_time', [], $locale);
// get shown-intro-preference: // get shown-intro-preference:
if (auth()->check()) { if (auth()->check()) {
$language = app('steam')->getLanguage(); $language = app('steam')->getLanguage();
$locale = app('steam')->getLanguage(); $locale = app('steam')->getLocale();
$page = $this->getPageName(); $page = $this->getPageName();
$shownDemo = $this->hasSeenDemo(); $shownDemo = $this->hasSeenDemo();
app('view')->share('language', $language); app('view')->share('language', $language);

View File

@@ -151,6 +151,7 @@ class RecurrenceController extends Controller
$today = Carbon::now()->startOfDay(); $today = Carbon::now()->startOfDay();
$date = Carbon::createFromFormat('Y-m-d', $string)->startOfDay(); $date = Carbon::createFromFormat('Y-m-d', $string)->startOfDay();
$preSelected = (string) $request->get('pre_select'); $preSelected = (string) $request->get('pre_select');
$locale = app('steam')->getLocale();
Log::debug(sprintf('date = %s, today = %s. date > today? %s', $date->toAtomString(), $today->toAtomString(), var_export($date > $today, true))); Log::debug(sprintf('date = %s, today = %s. date > today? %s', $date->toAtomString(), $today->toAtomString(), var_export($date > $today, true)));
Log::debug(sprintf('past = true? %s', var_export('true' === (string) $request->get('past'), true))); Log::debug(sprintf('past = true? %s', var_export('true' === (string) $request->get('past'), true)));
@@ -163,7 +164,7 @@ class RecurrenceController extends Controller
$dayOfWeek = (string) trans(sprintf('config.dow_%s', $date->dayOfWeekIso)); $dayOfWeek = (string) trans(sprintf('config.dow_%s', $date->dayOfWeekIso));
$ndom = sprintf('ndom,%s,%s', $date->weekOfMonth, $date->dayOfWeekIso); $ndom = sprintf('ndom,%s,%s', $date->weekOfMonth, $date->dayOfWeekIso);
$yearly = sprintf('yearly,%s', $date->format('Y-m-d')); $yearly = sprintf('yearly,%s', $date->format('Y-m-d'));
$yearlyDate = $date->formatLocalized((string) trans('config.month_and_day_no_year')); $yearlyDate = $date->formatLocalized((string) trans('config.month_and_day_no_year', [], $locale));
$result = [ $result = [
'daily' => ['label' => (string) trans('firefly.recurring_daily'), 'selected' => 0 === strpos($preSelected, 'daily')], 'daily' => ['label' => (string) trans('firefly.recurring_daily'), 'selected' => 0 === strpos($preSelected, 'daily')],
$weekly => ['label' => (string) trans('firefly.recurring_weekly', ['weekday' => $dayOfWeek]), $weekly => ['label' => (string) trans('firefly.recurring_weekly', ['weekday' => $dayOfWeek]),

View File

@@ -31,6 +31,7 @@ use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Routing\Redirector; use Illuminate\Routing\Redirector;
use Illuminate\View\View; use Illuminate\View\View;
use JsonException;
/** /**
* Class PreferencesController. * Class PreferencesController.
@@ -90,8 +91,9 @@ class PreferencesController extends Controller
$viewRange = $viewRangePref->data; $viewRange = $viewRangePref->data;
$frontPageAccounts = app('preferences')->get('frontPageAccounts', $accountIds); $frontPageAccounts = app('preferences')->get('frontPageAccounts', $accountIds);
$language = app('preferences')->get('language', config('firefly.default_language', 'en_US'))->data; $language = app('steam')->getLanguage();
$languages = config('firefly.languages'); $languages = config('firefly.languages');
$locale = app('preferences')->get('locale', config('firefly.default_locale', 'equal'))->data;
$listPageSize = app('preferences')->get('listPageSize', 50)->data; $listPageSize = app('preferences')->get('listPageSize', 50)->data;
$customFiscalYear = app('preferences')->get('customFiscalYear', 0)->data; $customFiscalYear = app('preferences')->get('customFiscalYear', 0)->data;
$fiscalYearStartStr = app('preferences')->get('fiscalYearStart', '01-01')->data; $fiscalYearStartStr = app('preferences')->get('fiscalYearStart', '01-01')->data;
@@ -100,6 +102,15 @@ class PreferencesController extends Controller
ksort($languages); ksort($languages);
// list of locales also has "equal" which makes it equal to whatever the language is.
try {
$locales = json_decode(file_get_contents(resource_path(sprintf('lang/%s/locales.json', $language))), true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
Log::error($e->getMessage());
$locales = [];
}
$locales = ['equal' => (string) trans('firefly.equal_to_language')] + $locales;
// an important fallback is that the frontPageAccount array gets refilled automatically // an important fallback is that the frontPageAccount array gets refilled automatically
// when it turns up empty. // when it turns up empty.
if (0 === count($frontPageAccounts->data)) { if (0 === count($frontPageAccounts->data)) {
@@ -113,6 +124,8 @@ class PreferencesController extends Controller
'groupedAccounts', 'groupedAccounts',
'frontPageAccounts', 'frontPageAccounts',
'languages', 'languages',
'locales',
'locale',
'tjOptionalFields', 'tjOptionalFields',
'viewRange', 'viewRange',
'customFiscalYear', 'customFiscalYear',
@@ -172,6 +185,11 @@ class PreferencesController extends Controller
session()->flash('info', 'All translations are supplied by volunteers. There might be errors and mistakes. I appreciate your feedback.'); session()->flash('info', 'All translations are supplied by volunteers. There might be errors and mistakes. I appreciate your feedback.');
} }
// same for locale:
/** @var Preference $currentLocale */
$locale = $request->get('locale');
app('preferences')->set('locale', $locale);
// optional fields for transactions: // optional fields for transactions:
$setOptions = $request->get('tj'); $setOptions = $request->get('tj');
$optionalTj = [ $optionalTj = [

View File

@@ -76,15 +76,14 @@ class Range
{ {
// get locale preference: // get locale preference:
$language = app('steam')->getLanguage(); $language = app('steam')->getLanguage();
//$locale = $this->getLocale(); $locale = app('steam')->getLocale();
App::setLocale($language); App::setLocale($language);
Carbon::setLocale(substr($language, 0, 2)); Carbon::setLocale(substr($locale, 0, 2));
$locale = explode(',', (string) trans('config.locale')); $localeArray = app('steam')->getLocaleArray($locale);
$locale = array_map('trim', $locale);
setlocale(LC_TIME, $locale); setlocale(LC_TIME, $localeArray);
$moneyResult = setlocale(LC_MONETARY, $locale); $moneyResult = setlocale(LC_MONETARY, $localeArray);
// send error to view if could not set money format // send error to view if could not set money format
if (false === $moneyResult) { if (false === $moneyResult) {
@@ -92,12 +91,12 @@ class Range
} }
// save some formats: // save some formats:
$monthAndDayFormat = (string) trans('config.month_and_day'); $monthAndDayFormat = (string) trans('config.month_and_day', [], $locale);
$dateTimeFormat = (string) trans('config.date_time'); $dateTimeFormat = (string) trans('config.date_time', [], $locale);
$defaultCurrency = app('amount')->getDefaultCurrency(); $defaultCurrency = app('amount')->getDefaultCurrency();
// also format for moment JS: // also format for moment JS:
$madMomentJS = (string) trans('config.month_and_day_moment_js'); $madMomentJS = (string) trans('config.month_and_day_moment_js', [], $locale);
app('view')->share('madMomentJS', $madMomentJS); app('view')->share('madMomentJS', $madMomentJS);
app('view')->share('monthAndDayFormat', $monthAndDayFormat); app('view')->share('monthAndDayFormat', $monthAndDayFormat);

View File

@@ -83,7 +83,7 @@ class AccountMeta extends Model
*/ */
public function getDataAttribute($value) public function getDataAttribute($value)
{ {
return json_decode($value); return json_decode($value, true, 512, JSON_THROW_ON_ERROR);
} }
/** /**

View File

@@ -78,13 +78,6 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @property-read \Illuminate\Database\Eloquent\Collection|Location[] $locations * @property-read \Illuminate\Database\Eloquent\Collection|Location[] $locations
* @property-read int|null $locations_count * @property-read int|null $locations_count
* @property-read int|null $transaction_journals_count * @property-read int|null $transaction_journals_count
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property string $tagMode
* @property string|null $description
* @property float|null $latitude
* @property float|null $longitude
* @property int|null $zoomLevel
*/ */
class Tag extends Model class Tag extends Model
{ {

View File

@@ -325,9 +325,11 @@ class Amount
*/ */
public function getLocaleInfo(): array public function getLocaleInfo(): array
{ {
$locale = explode(',', (string) trans('config.locale')); // get config from preference, not from translation:
$locale = array_map('trim', $locale); $locale = app('steam')->getLocale();
setlocale(LC_MONETARY, $locale); $array = app('steam')->getLocaleArray($locale);
setlocale(LC_MONETARY, $array);
$info = localeconv(); $info = localeconv();
// correct variables // correct variables
$info['n_cs_precedes'] = $this->getLocaleField($info, 'n_cs_precedes'); $info['n_cs_precedes'] = $this->getLocaleField($info, 'n_cs_precedes');

View File

@@ -61,6 +61,7 @@ trait ChartGeneration
return $cache->get(); // @codeCoverageIgnore return $cache->get(); // @codeCoverageIgnore
} }
Log::debug('Regenerate chart.account.account-balance-chart from scratch.'); Log::debug('Regenerate chart.account.account-balance-chart from scratch.');
$locale = app('steam')->getLocale();
/** @var GeneratorInterface $generator */ /** @var GeneratorInterface $generator */
$generator = app(GeneratorInterface::class); $generator = app(GeneratorInterface::class);
@@ -89,7 +90,7 @@ trait ChartGeneration
$previous = array_values($range)[0]; $previous = array_values($range)[0];
while ($currentStart <= $end) { while ($currentStart <= $end) {
$format = $currentStart->format('Y-m-d'); $format = $currentStart->format('Y-m-d');
$label = $currentStart->formatLocalized((string)trans('config.month_and_day')); $label = $currentStart->formatLocalized((string)trans('config.month_and_day', [], $locale));
$balance = isset($range[$format]) ? round($range[$format], 12) : $previous; $balance = isset($range[$format]) ? round($range[$format], 12) : $previous;
$previous = $balance; $previous = $balance;
$currentStart->addDay(); $currentStart->addDay();

View File

@@ -287,10 +287,11 @@ class Navigation
*/ */
public function listOfPeriods(Carbon $start, Carbon $end): array public function listOfPeriods(Carbon $start, Carbon $end): array
{ {
$locale = app('steam')->getLocale();
// define period to increment // define period to increment
$increment = 'addDay'; $increment = 'addDay';
$format = $this->preferredCarbonFormat($start, $end); $format = $this->preferredCarbonFormat($start, $end);
$displayFormat = (string)trans('config.month_and_day'); $displayFormat = (string)trans('config.month_and_day', [], $locale);
// increment by month (for year) // increment by month (for year)
if ($start->diffInMonths($end) > 1) { if ($start->diffInMonths($end) > 1) {
$increment = 'addMonth'; $increment = 'addMonth';
@@ -391,13 +392,14 @@ class Navigation
*/ */
public function preferredCarbonLocalizedFormat(Carbon $start, Carbon $end): string public function preferredCarbonLocalizedFormat(Carbon $start, Carbon $end): string
{ {
$format = (string)trans('config.month_and_day'); $locale = app('steam')->getLocale();
$format = (string)trans('config.month_and_day', [], $locale);
if ($start->diffInMonths($end) > 1) { if ($start->diffInMonths($end) > 1) {
$format = (string)trans('config.month'); $format = (string)trans('config.month', [], $locale);
} }
if ($start->diffInMonths($end) > 12) { if ($start->diffInMonths($end) > 12) {
$format = (string)trans('config.year'); $format = (string)trans('config.year', [], $locale);
} }
return $format; return $format;

View File

@@ -610,4 +610,17 @@ class Steam
return $locale; return $locale;
} }
/**
* @param string $locale
*
* @return array
*/
public function getLocaleArray(string $locale): array {
return [
sprintf('%s', $locale),
sprintf('%s.utf8', $locale),
sprintf('%s.UTF-8', $locale),
];
}
} }

View File

@@ -323,7 +323,8 @@ return [
*/ */
'languages' => [ 'languages' => [
// currently enabled languages // currently enabled languages
'en_US' => ['name_locale' => 'English', 'name_english' => 'English'], 'en_US' => ['name_locale' => 'English (US)', 'name_english' => 'English (US)'],
'en_GB' => ['name_locale' => 'English (GB)', 'name_english' => 'English (GB)'],
'cs_CZ' => ['name_locale' => 'Czech', 'name_english' => 'Czech'], 'cs_CZ' => ['name_locale' => 'Czech', 'name_english' => 'Czech'],
'el_GR' => ['name_locale' => 'Ελληνικά', 'name_english' => 'Greek'], 'el_GR' => ['name_locale' => 'Ελληνικά', 'name_english' => 'Greek'],
'es_ES' => ['name_locale' => 'Español', 'name_english' => 'Spanish'], 'es_ES' => ['name_locale' => 'Español', 'name_english' => 'Spanish'],
@@ -557,6 +558,7 @@ return [
], ],
'default_currency' => 'EUR', 'default_currency' => 'EUR',
'default_language' => envNonEmpty('DEFAULT_LANGUAGE', 'en_US'), 'default_language' => envNonEmpty('DEFAULT_LANGUAGE', 'en_US'),
'default_locale' => envNonEmpty('DEFAULT_LOCALE', 'equal'),
'search_modifiers' => ['amount_is', 'amount', 'amount_max', 'amount_min', 'amount_less', 'amount_more', 'source', 'destination', 'category', 'search_modifiers' => ['amount_is', 'amount', 'amount_max', 'amount_min', 'amount_less', 'amount_more', 'source', 'destination', 'category',
'budget', 'bill', 'type', 'date', 'date_before', 'date_after', 'on', 'before', 'after', 'from', 'to', 'tag', 'created_on', 'budget', 'bill', 'type', 'date', 'date_before', 'date_after', 'on', 'before', 'after', 'from', 'to', 'tag', 'created_on',
'updated_on',], 'updated_on',],