Skip to content

Commit 1f677da

Browse files
committed
added translation selector
1 parent ccb7dad commit 1f677da

12 files changed

Lines changed: 310 additions & 917 deletions

lib/constants/SettingsConstants.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,23 @@ AppLocalizations appStrings(context) {
4747
return AppLocalizations.of(context);
4848
}
4949

50+
String getLanguageFromCode(String code) {
51+
switch (code) {
52+
case 'de': return 'Deutsch';
53+
break;
54+
case 'en': return 'English';
55+
break;
56+
case 'es': return 'Español';
57+
break;
58+
case 'fr': return 'Français';
59+
break;
60+
case 'zh': return '中国人';
61+
break;
62+
default: return code;
63+
break;
64+
}
65+
}
66+
5067
Map<int, String> albumSort(context) {
5168
return {
5269
0: appStrings(context).categorySort_nameAscending,

lib/l10n/app_en.arb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,7 @@
906906

907907

908908
"settings_appName": "Piwigo NG",
909+
"settings_language": "Select language",
909910
"settingsHeader_about": "Information",
910911
"settings_twitter": "@piwigo",
911912
"settings_twitterURL": "https://twitter.com/piwigo",

lib/l10n/app_es.arb

Lines changed: 0 additions & 761 deletions
This file was deleted.

lib/l10n/app_nl.arb

Lines changed: 0 additions & 3 deletions
This file was deleted.

lib/main.dart

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
22
import 'package:flutter/services.dart';
33
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
44
import 'package:flutter_localizations/flutter_localizations.dart';
5+
import 'package:piwigo_ng/services/LocaleProvider.dart';
56
import 'package:piwigo_ng/services/ThemeProvider.dart';
67
import 'package:piwigo_ng/services/UploadStatusProvider.dart';
78
import 'package:piwigo_ng/views/RootCategoryViewPage.dart';
@@ -27,7 +28,12 @@ void main() async {
2728
return ChangeNotifierProvider(
2829
create: (context) => UploadStatusNotifier(),
2930
builder: (context, _) {
30-
return MyApp();
31+
return ChangeNotifierProvider(
32+
create: (context) => LocaleNotifier(),
33+
builder: (context, _) {
34+
return MyApp();
35+
}
36+
);
3137
},
3238
);
3339
},
@@ -52,6 +58,7 @@ class MyApp extends StatelessWidget {
5258
@override
5359
Widget build(BuildContext context) {
5460
final themeProvider = Provider.of<ThemeNotifier>(context);
61+
final localeProvider = Provider.of<LocaleNotifier>(context);
5562
return MaterialApp(
5663
title: "Piwigo NG",
5764
// localeListResolutionCallback: (locales, supportedLocales) {
@@ -71,8 +78,12 @@ class MyApp extends StatelessWidget {
7178
GlobalWidgetsLocalizations.delegate,
7279
GlobalCupertinoLocalizations.delegate,
7380
],
74-
supportedLocales: AppLocalizations.supportedLocales,
75-
locale: Locale('en', 'US'),
81+
supportedLocales: [
82+
Locale('en'),
83+
Locale('de'),
84+
Locale('fr'),
85+
],
86+
locale: localeProvider.locale,
7687
theme: light,
7788
// theme: themeProvider.darkTheme ? dark : light,
7889
initialRoute: '/',

lib/services/LocaleProvider.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import 'dart:io';
2+
3+
import 'package:flutter/material.dart';
4+
import 'package:piwigo_ng/api/API.dart';
5+
6+
7+
class LocaleNotifier extends ChangeNotifier {
8+
final String key = "locale";
9+
Locale _locale;
10+
Locale get locale => _locale;
11+
12+
LocaleNotifier() {
13+
_locale = Locale('en');
14+
_loadFromPrefs();
15+
}
16+
17+
changeLocale(locale){
18+
_locale = locale;
19+
_saveToPrefs();
20+
notifyListeners();
21+
}
22+
23+
_loadFromPrefs() {
24+
_locale = Locale(API.prefs.getString(key) ?? Platform.localeName.substring(0, 1));
25+
notifyListeners();
26+
}
27+
28+
_saveToPrefs() {
29+
API.prefs.setString(key, _locale.languageCode);
30+
}
31+
}

lib/views/RootCategoryViewPage.dart

Lines changed: 66 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class _RootCategoryViewPageState extends State<RootCategoryViewPage> with Single
2929
TextEditingController _searchController = TextEditingController();
3030
ScrollController _scrollController = ScrollController();
3131
bool _isSearching = false;
32+
final FocusNode _focus = FocusNode();
3233

3334
Future<Map<String,dynamic>> _albumsFuture;
3435
Future<Map<String,dynamic>> _imagesFuture;
@@ -37,11 +38,10 @@ class _RootCategoryViewPageState extends State<RootCategoryViewPage> with Single
3738
void initState() {
3839
super.initState();
3940
_rootCategory = "0";
40-
_searchController.addListener(() {
41-
setState(() {
42-
_isSearching = _searchController.text.length > 0;
43-
//_getData();
44-
});
41+
_focus.addListener(() {
42+
if(!_focus.hasFocus) {
43+
setState(() {});
44+
}
4545
});
4646
WidgetsBinding.instance.addPostFrameCallback((_) {
4747
API.uploader = Uploader(context);
@@ -64,7 +64,7 @@ class _RootCategoryViewPageState extends State<RootCategoryViewPage> with Single
6464
return Scaffold(
6565
body: GestureDetector(
6666
onTap: () {
67-
FocusScope.of(context).unfocus();
67+
_focus.unfocus();
6868
},
6969
child: NestedScrollView(
7070
controller: _scrollController,
@@ -84,6 +84,14 @@ class _RootCategoryViewPageState extends State<RootCategoryViewPage> with Single
8484
),
8585
AppBarExpandableSearch(
8686
textController: _searchController,
87+
onTap: () {},
88+
focusNode: _focus,
89+
onSubmit: (string) {
90+
setState(() {
91+
_isSearching = _searchController.text.length > 0;
92+
_getData();
93+
});
94+
},
8795
),
8896
];
8997
},
@@ -96,27 +104,64 @@ class _RootCategoryViewPageState extends State<RootCategoryViewPage> with Single
96104
_getData();
97105
return Future.delayed(Duration(milliseconds: 1000));
98106
},
99-
child: Builder(builder: (context) {
100-
if(_isSearching) {
101-
return FutureBuilder<Map<String,dynamic>>(
102-
key: UniqueKey(),
103-
future: _imagesFuture,
104-
builder: (BuildContext context, AsyncSnapshot imagesSnapshot) {
105-
if(imagesSnapshot.hasData){
106-
if(imagesSnapshot.data['stat'] == 'fail') {
107+
child: SingleChildScrollView(
108+
child: Builder(builder: (context) {
109+
if(_isSearching) {
110+
return FutureBuilder<Map<String,dynamic>>(
111+
key: UniqueKey(),
112+
future: _imagesFuture,
113+
builder: (BuildContext context, AsyncSnapshot imagesSnapshot) {
114+
if(imagesSnapshot.hasData){
115+
if(imagesSnapshot.data['stat'] == 'fail') {
116+
return Center(
117+
child: Text(appStrings(context).categoryImageList_noDataError),
118+
);
119+
}
120+
var images = imagesSnapshot.data['result']['images'];
121+
var nbImages = images.length;
122+
return Column(
123+
children: [
124+
_imageGrid(images),
125+
Center(
126+
child: Container(
127+
padding: EdgeInsets.all(10),
128+
child: Text(appStrings(context).imageCount(nbImages), style: TextStyle(fontSize: 20, color: _theme.textTheme.bodyText2.color, fontWeight: FontWeight.w300,),),
129+
),
130+
),
131+
],
132+
);
133+
} else {
107134
return Center(
108-
child: Text(appStrings(context).categoryImageList_noDataError),
135+
child: CircularProgressIndicator(),
109136
);
110137
}
111-
var images = imagesSnapshot.data['result']['images'];
112-
var nbImages = images.length;
138+
},
139+
);
140+
}
141+
return FutureBuilder<Map<String,dynamic>>(
142+
key: UniqueKey(),
143+
future: _albumsFuture, // Albums of the list
144+
builder: (BuildContext context, AsyncSnapshot albumSnapshot) {
145+
if(albumSnapshot.hasData){
146+
if(albumSnapshot.data['stat'] == 'fail') {
147+
return Container(
148+
padding: EdgeInsets.all(10),
149+
child: Text(albumSnapshot.data['result']),
150+
); //appStrings(context).categoryMainEmpty
151+
}
152+
var albums = albumSnapshot.data['result']['categories'];
153+
int nbPhotos = 0;
154+
albums.forEach((cat) => nbPhotos+=cat["total_nb_images"]);
155+
albums.removeWhere((category) => (
156+
category["id"].toString() == _rootCategory
157+
));
113158
return Column(
114159
children: [
115-
_imageGrid(images),
160+
_albumGrid(albums),
116161
Center(
117162
child: Container(
118163
padding: EdgeInsets.all(10),
119-
child: Text(appStrings(context).imageCount(nbImages), style: TextStyle(fontSize: 20, color: _theme.textTheme.bodyText2.color, fontWeight: FontWeight.w300,),),
164+
child: Text(appStrings(context).imageCount(nbPhotos), style: TextStyle(fontSize: 20, color: _theme.textTheme.bodyText2.color, fontWeight: FontWeight.w300,),),
120165
),
121166
),
122167
],
@@ -128,43 +173,8 @@ class _RootCategoryViewPageState extends State<RootCategoryViewPage> with Single
128173
}
129174
},
130175
);
131-
}
132-
return FutureBuilder<Map<String,dynamic>>(
133-
key: UniqueKey(),
134-
future: _albumsFuture, // Albums of the list
135-
builder: (BuildContext context, AsyncSnapshot albumSnapshot) {
136-
if(albumSnapshot.hasData){
137-
if(albumSnapshot.data['stat'] == 'fail') {
138-
return Container(
139-
padding: EdgeInsets.all(10),
140-
child: Text(albumSnapshot.data['result']),
141-
); //appStrings(context).categoryMainEmpty
142-
}
143-
var albums = albumSnapshot.data['result']['categories'];
144-
int nbPhotos = 0;
145-
albums.forEach((cat) => nbPhotos+=cat["total_nb_images"]);
146-
albums.removeWhere((category) => (
147-
category["id"].toString() == _rootCategory
148-
));
149-
return Column(
150-
children: [
151-
_albumGrid(albums),
152-
Center(
153-
child: Container(
154-
padding: EdgeInsets.all(10),
155-
child: Text(appStrings(context).imageCount(nbPhotos), style: TextStyle(fontSize: 20, color: _theme.textTheme.bodyText2.color, fontWeight: FontWeight.w300,),),
156-
),
157-
),
158-
],
159-
);
160-
} else {
161-
return Center(
162-
child: CircularProgressIndicator(),
163-
);
164-
}
165-
},
166-
);
167-
}),
176+
}),
177+
),
168178
),
169179
),
170180
),

0 commit comments

Comments
 (0)