Skip to content

Commit

Permalink
add localization support
Browse files Browse the repository at this point in the history
  • Loading branch information
qcasey committed Apr 21, 2021
1 parent 12c543a commit 70935a0
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 20 deletions.
3 changes: 3 additions & 0 deletions l10n.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
22 changes: 12 additions & 10 deletions lib/class/login_form.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../model/auth.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

// Define a custom Form widget.
class LoginForm extends StatefulWidget {
Expand Down Expand Up @@ -36,15 +37,15 @@ class LoginFormState extends State<LoginForm> {
controller: serverController,
validator: (value) {
if (value.isEmpty) {
return 'Server is required.';
return AppLocalizations.of(context).fieldRequired;
}
return null;
},
decoration: new InputDecoration(
contentPadding:
EdgeInsets.symmetric(horizontal: 10, vertical: 10),
hintText: "http://192.168.1.50:8000",
labelText: 'Server'),
labelText: AppLocalizations.of(context).server),
),
),
new AutofillGroup(
Expand All @@ -55,12 +56,12 @@ class LoginFormState extends State<LoginForm> {
controller: usernameController,
validator: (value) {
if (value.isEmpty) {
return 'Username is required.';
return AppLocalizations.of(context).fieldRequired;
}
return null;
},
decoration: new InputDecoration(
labelText: 'Username',
labelText: AppLocalizations.of(context).username,
contentPadding:
EdgeInsets.symmetric(horizontal: 10, vertical: 10),
),
Expand All @@ -72,12 +73,12 @@ class LoginFormState extends State<LoginForm> {
controller: passwordController,
validator: (value) {
if (value.isEmpty) {
return 'Password is required.';
return AppLocalizations.of(context).fieldRequired;
}
return null;
},
decoration: new InputDecoration(
labelText: 'Password',
labelText: AppLocalizations.of(context).password,
contentPadding:
EdgeInsets.symmetric(horizontal: 10, vertical: 10),
),
Expand All @@ -99,11 +100,11 @@ class LoginFormState extends State<LoginForm> {
child: RaisedButton(
textColor: Colors.white,
color: Color(0xFF17541f),
child: Text('Login'),
child: Text(AppLocalizations.of(context).login),
onPressed: () async {
if (_formKey.currentState.validate()) {
Scaffold.of(context)
.showSnackBar(SnackBar(content: Text('Logging in...')));
Scaffold.of(context).showSnackBar(SnackBar(
content: Text(AppLocalizations.of(context).loggingIn)));

_auth
.login(
Expand All @@ -117,7 +118,8 @@ class LoginFormState extends State<LoginForm> {
} else {
print(loginError);
Scaffold.of(context).showSnackBar(SnackBar(
content: Text('Failed to login: ' + loginError)));
content: Text(AppLocalizations.of(context).loginFailed +
loginError)));
}
});
}
Expand Down
16 changes: 16 additions & 0 deletions lib/l10n/app_de.arb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"yes": "Ja",
"no": "Nein",
"logout": "Ausloggen?",
"login": "Anmeldung",
"loggingIn": "Einloggen...",
"loginFailed": "Einloggen fehlgeschlagen: ",
"username": "Nutzername",
"password": "Passwort",
"server": "Server",
"fieldRequired": "Erforderlich.",
"fileUploaded": "Datei hochgeladen.",
"readyToUpload": "Bereit zum Hochladen.",
"shareInstructions": "Wählen Sie im Menü \"Freigeben\" die Option \"Paperless Share\", um Bilder und Dokumente hochzuladen.",
"serverInstructions": "Ihr Server wird versuchen, es automatisch zu verarbeiten. Es sollte in wenigen Augenblicken erscheinen."
}
16 changes: 16 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"yes": "Yes",
"no": "No",
"logout": "Logout?",
"login": "Login",
"loggingIn": "Logging in...",
"loginFailed": "Failed to login: ",
"username": "Username",
"password": "Password",
"server": "Server",
"fieldRequired": "Required.",
"fileUploaded": "File uploaded.",
"readyToUpload": "Ready to upload!",
"shareInstructions": "Select Paperless Share in the Share Menu to upload pictures and documents",
"serverInstructions": "Your server will attempt to process it automatically. It should appear within a few moments."
}
12 changes: 12 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'package:provider/provider.dart';
import 'model/auth.dart';
import 'login.dart';
import 'share.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

void main() {
runApp(PaperlessShare());
Expand Down Expand Up @@ -75,6 +77,16 @@ class _PaperlessShareState extends State<PaperlessShare> {
'/login': (context) => new LoginPage(),
'/share': (context) => new SharePage(),
},
localizationsDelegates: [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('en', ''),
const Locale('de', ''),
],
));
}
}
17 changes: 8 additions & 9 deletions lib/share.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:fluttertoast/fluttertoast.dart';
import 'package:flutter/services.dart';
import 'model/auth.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class SharePage extends StatefulWidget {
@override
Expand Down Expand Up @@ -69,7 +70,7 @@ class _SharePageState extends State<SharePage> {

if (response.statusCode == 200) {
Fluttertoast.showToast(
msg: "File uploaded.",
msg: AppLocalizations.of(context).fileUploaded,
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
Expand Down Expand Up @@ -141,17 +142,17 @@ class _SharePageState extends State<SharePage> {
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: new Text("Logout?"),
title: new Text(AppLocalizations.of(context).logout),
actions: [
new FlatButton(
child: new Text("Yes"),
child: new Text(AppLocalizations.of(context).yes),
onPressed: () {
_auth.logout();
Navigator.pushReplacementNamed(context, "/login");
},
),
new FlatButton(
child: new Text("No"),
child: new Text(AppLocalizations.of(context).no),
onPressed: () {
Navigator.of(context).pop();
}),
Expand All @@ -168,11 +169,9 @@ class _SharePageState extends State<SharePage> {
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
_paddedText("Ready to upload!"),
_paddedText(
"Select Paperless Share in the Share Menu to upload pictures and documents."),
_paddedText(
"Your server will attempt to process it automatically. It should appear within a few moments."),
_paddedText(AppLocalizations.of(context).readyToUpload),
_paddedText(AppLocalizations.of(context).shareInstructions),
_paddedText(AppLocalizations.of(context).serverInstructions),
],
));
}
Expand Down
12 changes: 12 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ packages:
url: "https://github.com/knyghtryda/flutter_launcher_icons"
source: git
version: "0.8.1"
flutter_localizations:
dependency: "direct main"
description: flutter
source: sdk
version: "0.0.0"
flutter_spinkit:
dependency: "direct main"
description:
Expand Down Expand Up @@ -179,6 +184,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.19"
intl:
dependency: "direct main"
description:
name: intl
url: "https://pub.dartlang.org"
source: hosted
version: "0.17.0"
js:
dependency: transitive
description:
Expand Down
6 changes: 5 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ environment:
dependencies:
flutter:
sdk: flutter

flutter_localizations:
sdk: flutter

intl: ^0.17.0
encrypted_shared_preferences: ^1.0.7
http: ^0.13.1
provider: ^5.0.0
Expand Down Expand Up @@ -66,6 +69,7 @@ flutter:
- assets/logo.png
- assets/icon.png

generate: true
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.

Expand Down

0 comments on commit 70935a0

Please sign in to comment.