Add i18n via slang

This commit is contained in:
2025-05-12 00:02:19 +02:00
parent 99ab2f006d
commit e0fba11f25
25 changed files with 447 additions and 415 deletions

View File

@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:okane/database/collections/expense_category.dart';
import 'package:okane/database/database.dart';
import 'package:okane/i18n/strings.g.dart';
import 'package:okane/ui/state/core.dart';
class AddExpenseCategory extends StatefulWidget {
@@ -39,7 +40,9 @@ class AddExpenseCategoryState extends State<AddExpenseCategory> {
),
TextField(
decoration: InputDecoration(hintText: "Category name"),
decoration: InputDecoration(
hintText: t.common.expenseCategory.name,
),
controller: _categoryNameController,
),
Row(
@@ -54,7 +57,7 @@ class AddExpenseCategoryState extends State<AddExpenseCategory> {
_categoryNameController.text = "";
Navigator.of(context).pop(category);
},
child: Text("Add"),
child: Text(t.modals.add),
),
],
),

View File

@@ -1,291 +0,0 @@
import 'package:flutter/material.dart';
import 'package:flutter_picker_plus/picker.dart';
import 'package:get_it/get_it.dart';
import 'package:okane/database/collections/account.dart';
import 'package:okane/database/collections/beneficiary.dart';
import 'package:okane/database/collections/recurrent.dart';
import 'package:okane/database/collections/template.dart';
import 'package:okane/database/database.dart';
import 'package:okane/ui/state/core.dart';
import 'package:okane/ui/transaction.dart';
import 'package:okane/ui/utils.dart';
import 'package:searchfield/searchfield.dart';
enum Period { days, weeks, months, years }
class AddRecurringTransactionTemplateWidget extends StatefulWidget {
final VoidCallback onAdd;
final Account activeAccountItem;
const AddRecurringTransactionTemplateWidget({
super.key,
required this.activeAccountItem,
required this.onAdd,
});
@override
State<AddRecurringTransactionTemplateWidget> createState() =>
_AddRecurringTransactionTemplateWidgetState();
}
class _AddRecurringTransactionTemplateWidgetState
extends State<AddRecurringTransactionTemplateWidget> {
final TextEditingController _beneficiaryTextController =
TextEditingController();
final TextEditingController _amountTextController = TextEditingController();
final TextEditingController _templateNameController = TextEditingController();
List<Beneficiary> beneficiaries = [];
SearchFieldListItem<Beneficiary>? _selectedBeneficiary;
TransactionDirection _selectedDirection = TransactionDirection.send;
Period _selectedPeriod = Period.months;
int _periodSize = 1;
String getBeneficiaryName(Beneficiary item) {
return switch (item.type) {
BeneficiaryType.account => "${item.name} (Account)",
BeneficiaryType.other => item.name,
};
}
Future<void> _submit(BuildContext context) async {
final beneficiaryName = _beneficiaryTextController.text;
if (_selectedBeneficiary == null && beneficiaryName.isEmpty) {
return;
}
if (_templateNameController.text.isEmpty) {
return;
}
Beneficiary? beneficiary = _selectedBeneficiary?.item;
if (beneficiary == null ||
getBeneficiaryName(beneficiary) != beneficiaryName) {
// Add a new beneficiary, if none was selected
final result = await showDialog<bool>(
context: context,
builder:
(context) => AlertDialog(
title: const Text("Add Beneficiary"),
content: Text(
"The beneficiary '$beneficiaryName' does not exist. Do you want to add it?",
),
actions: [
TextButton(
style: TextButton.styleFrom(
textStyle: Theme.of(context).textTheme.labelLarge,
),
child: const Text('Add'),
onPressed: () => Navigator.of(context).pop(true),
),
TextButton(
style: TextButton.styleFrom(
textStyle: Theme.of(context).textTheme.labelLarge,
),
child: const Text('Cancel'),
onPressed: () => Navigator.of(context).pop(false),
),
],
),
);
if (result == null || !result) {
return;
}
beneficiary =
Beneficiary()
..name = beneficiaryName
..type = BeneficiaryType.other;
await upsertBeneficiary(beneficiary);
}
final days = switch (_selectedPeriod) {
Period.days => _periodSize,
Period.weeks => _periodSize * 7,
Period.months => _periodSize * 31,
Period.years => _periodSize * 365,
};
final factor = switch (_selectedDirection) {
TransactionDirection.send => -1,
TransactionDirection.receive => 1,
};
final amount = factor * double.parse(_amountTextController.text).abs();
final template =
TransactionTemplate()
..name = _templateNameController.text
..beneficiary.value = beneficiary
..account.value = widget.activeAccountItem
..recurring = true
..amount = amount;
await upsertTransactionTemplate(template);
final transaction =
RecurringTransaction()
..lastExecution = null
..template.value = template
..account.value = widget.activeAccountItem
..days = days;
await upsertRecurringTransaction(transaction);
_periodSize = 1;
_selectedPeriod = Period.weeks;
_amountTextController.text = "";
_templateNameController.text = "";
widget.onAdd();
}
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.symmetric(horizontal: 16),
child: ListView(
shrinkWrap: true,
children: [
Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: TextField(
controller: _templateNameController,
decoration: InputDecoration(label: Text("Template name")),
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: SegmentedButton<TransactionDirection>(
segments: [
ButtonSegment(
value: TransactionDirection.send,
label: Text("Send"),
icon: Icon(Icons.remove),
),
ButtonSegment(
value: TransactionDirection.receive,
label: Text("Receive"),
icon: Icon(Icons.add),
),
],
selected: <TransactionDirection>{_selectedDirection},
multiSelectionEnabled: false,
onSelectionChanged: (selection) {
setState(() => _selectedDirection = selection.first);
},
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: SearchField<Beneficiary>(
suggestions:
beneficiaries
.where((el) {
final bloc = GetIt.I.get<CoreCubit>();
if (el.type == BeneficiaryType.account) {
return el.account.value?.id != bloc.activeAccount?.id;
}
return true;
})
.map((el) {
return SearchFieldListItem(
getBeneficiaryName(el),
item: el,
);
})
.toList(),
hint: switch (_selectedDirection) {
TransactionDirection.send => "Payee",
TransactionDirection.receive => "Payer",
},
controller: _beneficiaryTextController,
selectedValue: _selectedBeneficiary,
onSuggestionTap: (beneficiary) {
setState(() => _selectedBeneficiary = beneficiary);
},
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: TextField(
controller: _amountTextController,
keyboardType: TextInputType.numberWithOptions(
signed: false,
decimal: false,
),
decoration: InputDecoration(
hintText: "Amount",
icon: Icon(Icons.euro),
),
),
),
Padding(
padding: EdgeInsets.only(left: 16, right: 16, top: 16),
child: SegmentedButton<Period>(
segments: [
ButtonSegment(value: Period.days, label: Text("Days")),
ButtonSegment(value: Period.weeks, label: Text("Weeks")),
ButtonSegment(value: Period.months, label: Text("Months")),
ButtonSegment(value: Period.years, label: Text("Years")),
],
selected: <Period>{_selectedPeriod},
multiSelectionEnabled: false,
onSelectionChanged: (selection) {
setState(() => _selectedPeriod = selection.first);
},
),
),
Text.rich(
TextSpan(
text: "Repeat every ",
children: [
WidgetSpan(
child: TextButton(
onPressed: () {
Picker(
adapter: NumberPickerAdapter(
data: [
NumberPickerColumn(
begin: 1,
end: 999,
initValue: _periodSize,
),
],
),
hideHeader: true,
selectedTextStyle: TextStyle(color: Colors.blue),
onConfirm: (Picker picker, List value) {
setState(() {
_periodSize = (value.first as int) + 1;
});
},
).showDialog(context);
},
child: Text(_periodSize.toString()),
),
),
TextSpan(
text: switch (_selectedPeriod) {
Period.days => " days",
Period.weeks => " weeks",
Period.months => " months",
Period.years => " years",
},
),
],
),
),
Align(
alignment: Alignment.centerRight,
child: OutlinedButton(
onPressed: () => _submit(context),
child: Text("Add"),
),
),
],
),
);
}
}

View File

@@ -7,6 +7,7 @@ import 'package:okane/database/collections/expense_category.dart';
import 'package:okane/database/collections/recurrent.dart';
import 'package:okane/database/collections/template.dart';
import 'package:okane/database/database.dart';
import 'package:okane/i18n/strings.g.dart';
import 'package:okane/ui/state/core.dart';
import 'package:okane/ui/transaction.dart';
import 'package:okane/ui/utils.dart';
@@ -51,7 +52,9 @@ class _AddTransactionTemplateWidgetState
String getBeneficiaryName(Beneficiary item) {
return switch (item.type) {
BeneficiaryType.account => "${item.name} (Account)",
BeneficiaryType.account => t.common.beneficiary.nameWithAccount(
name: item.name,
),
BeneficiaryType.other => item.name,
};
}
@@ -59,7 +62,6 @@ class _AddTransactionTemplateWidgetState
Future<void> _submit(BuildContext context) async {
final beneficiaryName = _beneficiaryTextController.text;
if (_selectedBeneficiary == null && beneficiaryName.isEmpty) {
print("No beneficiary");
return;
}
if (_templateNameController.text.isEmpty) {
@@ -74,23 +76,23 @@ class _AddTransactionTemplateWidgetState
context: context,
builder:
(context) => AlertDialog(
title: const Text("Add Beneficiary"),
title: Text(t.common.beneficiary.addBeneficiary.title),
content: Text(
"The beneficiary '$beneficiaryName' does not exist. Do you want to add it?",
t.common.beneficiary.addBeneficiary.body(name: beneficiaryName),
),
actions: [
TextButton(
style: TextButton.styleFrom(
textStyle: Theme.of(context).textTheme.labelLarge,
),
child: const Text('Add'),
child: Text(t.modals.add),
onPressed: () => Navigator.of(context).pop(true),
),
TextButton(
style: TextButton.styleFrom(
textStyle: Theme.of(context).textTheme.labelLarge,
),
child: const Text('Cancel'),
child: Text(t.modals.cancel),
onPressed: () => Navigator.of(context).pop(false),
),
],
@@ -151,7 +153,7 @@ class _AddTransactionTemplateWidgetState
padding: const EdgeInsets.symmetric(vertical: 8),
child: TextField(
controller: _templateNameController,
decoration: InputDecoration(label: Text("Template name")),
decoration: InputDecoration(label: Text(t.common.templateName)),
),
),
@@ -161,12 +163,12 @@ class _AddTransactionTemplateWidgetState
segments: [
ButtonSegment(
value: TransactionDirection.send,
label: Text("Send"),
label: Text(t.common.transaction.directionSend),
icon: Icon(Icons.remove),
),
ButtonSegment(
value: TransactionDirection.receive,
label: Text("Receive"),
label: Text(t.common.transaction.directionReceive),
icon: Icon(Icons.add),
),
],
@@ -201,8 +203,10 @@ class _AddTransactionTemplateWidgetState
})
.toList(),
hint: switch (_selectedDirection) {
TransactionDirection.send => "Payee",
TransactionDirection.receive => "Payer",
TransactionDirection.send =>
t.common.transaction.beneficiaryTextfieldHintSend,
TransactionDirection.receive =>
t.common.transaction.beneficiaryTextfieldHintReceive,
},
controller: _beneficiaryTextController,
selectedValue: _selectedBeneficiary,
@@ -222,7 +226,7 @@ class _AddTransactionTemplateWidgetState
decimal: false,
),
decoration: InputDecoration(
hintText: "Amount",
hintText: t.common.amount,
icon: Icon(Icons.euro),
),
),
@@ -230,7 +234,7 @@ class _AddTransactionTemplateWidgetState
Row(
children: [
Text("Expense category"),
Text(t.common.expenseCategory.name),
Padding(
padding: EdgeInsets.only(left: 16),
@@ -246,7 +250,9 @@ class _AddTransactionTemplateWidgetState
setState(() => _expenseCategory = category);
},
child: Text(_expenseCategory?.name ?? "None"),
child: Text(
_expenseCategory?.name ?? t.common.expenseCategory.none,
),
),
),
],
@@ -254,7 +260,7 @@ class _AddTransactionTemplateWidgetState
Row(
children: [
Text("Is recurring"),
Text(t.pages.templates.addTemplate.isRecurring),
Padding(
padding: EdgeInsets.only(left: 16),
child: Switch(
@@ -272,10 +278,22 @@ class _AddTransactionTemplateWidgetState
padding: EdgeInsets.only(left: 16, right: 16, top: 16),
child: SegmentedButton<Period>(
segments: [
ButtonSegment(value: Period.days, label: Text("Days")),
ButtonSegment(value: Period.weeks, label: Text("Weeks")),
ButtonSegment(value: Period.months, label: Text("Months")),
ButtonSegment(value: Period.years, label: Text("Years")),
ButtonSegment(
value: Period.days,
label: Text(t.common.period.days),
),
ButtonSegment(
value: Period.weeks,
label: Text(t.common.period.weeks),
),
ButtonSegment(
value: Period.months,
label: Text(t.common.period.months),
),
ButtonSegment(
value: Period.years,
label: Text(t.common.period.years),
),
],
selected: <Period>{_selectedPeriod},
multiSelectionEnabled: false,
@@ -311,10 +329,18 @@ class _AddTransactionTemplateWidgetState
child: Center(
child: Text(
switch (_selectedPeriod) {
Period.days => "$_periodSize days",
Period.weeks => "$_periodSize weeks",
Period.months => "$_periodSize months",
Period.years => "$_periodSize years",
Period.days => t.common.period.daysNumber(
number: _periodSize,
),
Period.weeks => t.common.period.weeksNumber(
number: _periodSize,
),
Period.months => t.common.period.monthsNumber(
number: _periodSize,
),
Period.years => t.common.period.yearsNumber(
number: _periodSize,
),
},
style: TextStyle(
color: _isRecurring ? Colors.black : Colors.grey,
@@ -341,7 +367,7 @@ class _AddTransactionTemplateWidgetState
alignment: Alignment.centerRight,
child: OutlinedButton(
onPressed: () => _submit(context),
child: Text("Add"),
child: Text(t.modals.add),
),
),
],

View File

@@ -7,6 +7,7 @@ import 'package:okane/database/collections/expense_category.dart';
import 'package:okane/database/collections/template.dart';
import 'package:okane/database/collections/transaction.dart';
import 'package:okane/database/database.dart';
import 'package:okane/i18n/strings.g.dart';
import 'package:okane/ui/state/core.dart';
import 'package:okane/ui/transaction.dart';
import 'package:okane/ui/utils.dart';
@@ -67,7 +68,9 @@ class _AddTransactionWidgetState extends State<AddTransactionWidget> {
String getBeneficiaryName(Beneficiary item) {
return switch (item.type) {
BeneficiaryType.account => "${item.name} (Account)",
BeneficiaryType.account => t.common.beneficiary.nameWithAccount(
name: item.name,
),
BeneficiaryType.other => item.name,
};
}
@@ -86,23 +89,23 @@ class _AddTransactionWidgetState extends State<AddTransactionWidget> {
context: context,
builder:
(context) => AlertDialog(
title: const Text("Add Beneficiary"),
title: Text(t.common.beneficiary.addBeneficiary.title),
content: Text(
"The beneficiary '$beneficiaryName' does not exist. Do you want to add it?",
t.common.beneficiary.addBeneficiary.body(name: beneficiaryName),
),
actions: [
TextButton(
style: TextButton.styleFrom(
textStyle: Theme.of(context).textTheme.labelLarge,
),
child: const Text('Add'),
child: Text(t.modals.add),
onPressed: () => Navigator.of(context).pop(true),
),
TextButton(
style: TextButton.styleFrom(
textStyle: Theme.of(context).textTheme.labelLarge,
),
child: const Text('Cancel'),
child: Text(t.modals.cancel),
onPressed: () => Navigator.of(context).pop(false),
),
],
@@ -177,7 +180,7 @@ class _AddTransactionWidgetState extends State<AddTransactionWidget> {
template.beneficiary.value!,
);
},
child: Text("Use template"),
child: Text(t.pages.transactions.addTransaction.useTemplate),
),
Padding(
@@ -186,12 +189,12 @@ class _AddTransactionWidgetState extends State<AddTransactionWidget> {
segments: [
ButtonSegment(
value: TransactionDirection.send,
label: Text("Send"),
label: Text(t.common.transaction.directionSend),
icon: Icon(Icons.remove),
),
ButtonSegment(
value: TransactionDirection.receive,
label: Text("Receive"),
label: Text(t.common.transaction.directionReceive),
icon: Icon(Icons.add),
),
],
@@ -226,8 +229,10 @@ class _AddTransactionWidgetState extends State<AddTransactionWidget> {
})
.toList(),
hint: switch (_selectedDirection) {
TransactionDirection.send => "Payee",
TransactionDirection.receive => "Payer",
TransactionDirection.send =>
t.common.transaction.beneficiaryTextfieldHintSend,
TransactionDirection.receive =>
t.common.transaction.beneficiaryTextfieldHintReceive,
},
controller: _beneficiaryTextController,
selectedValue: _selectedBeneficiary,
@@ -247,7 +252,7 @@ class _AddTransactionWidgetState extends State<AddTransactionWidget> {
decimal: false,
),
decoration: InputDecoration(
hintText: "Amount",
hintText: t.common.amount,
icon: Icon(Icons.euro),
),
),
@@ -258,7 +263,7 @@ class _AddTransactionWidgetState extends State<AddTransactionWidget> {
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text("Date"),
Text(t.common.date),
Padding(
padding: EdgeInsets.only(left: 16),
child: OutlinedButton(
@@ -289,7 +294,7 @@ class _AddTransactionWidgetState extends State<AddTransactionWidget> {
Row(
children: [
Text("Expense category"),
Text(t.common.expenseCategory.name),
Padding(
padding: EdgeInsets.only(left: 16),
child: OutlinedButton(
@@ -304,7 +309,9 @@ class _AddTransactionWidgetState extends State<AddTransactionWidget> {
setState(() => _expenseCategory = category);
},
child: Text(_expenseCategory?.name ?? "None"),
child: Text(
_expenseCategory?.name ?? t.common.expenseCategory.none,
),
),
),
],
@@ -314,7 +321,7 @@ class _AddTransactionWidgetState extends State<AddTransactionWidget> {
alignment: Alignment.centerRight,
child: OutlinedButton(
onPressed: () => _submit(context),
child: Text("Add"),
child: Text(t.modals.add),
),
),
],