Skip to content

Commit

Permalink
Multilingual Support added - Working only for Login otp screen for no…
Browse files Browse the repository at this point in the history
…w and only supports hindi and english language as of now
  • Loading branch information
iampawan committed Aug 25, 2018
1 parent a2e3b04 commit f4901c7
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 66 deletions.
12 changes: 11 additions & 1 deletion lib/myapp.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_uikit/ui/page/dashboard/dashboard_one.page.dart';
import 'package:flutter_uikit/ui/page/dashboard/dashboard_two_page.dart';
import 'package:flutter_uikit/ui/page/home_page.dart';
Expand All @@ -15,7 +16,7 @@ import 'package:flutter_uikit/ui/page/shopping/shopping_details_page.dart';
import 'package:flutter_uikit/ui/page/shopping/shopping_one_page.dart';
import 'package:flutter_uikit/ui/page/timeline/timeline_one_page.dart';
import 'package:flutter_uikit/ui/page/timeline/timeline_two_page.dart';

import 'package:flutter_uikit/utils/translations.dart';
import 'package:flutter_uikit/utils/uidata.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';

Expand All @@ -29,6 +30,15 @@ class MyApp extends StatelessWidget {
debugShowCheckedModeBanner: false,
showPerformanceOverlay: false,
home: HomePage(),
localizationsDelegates: [
const TranslationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
const Locale("en", "US"),
const Locale("hi", "IN"),
],
// initialRoute: UIData.notFoundRoute,

//routes
Expand Down
24 changes: 14 additions & 10 deletions lib/ui/page/login/login_one/login_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:flutter_uikit/model/fetch_process.dart';
import 'package:flutter_uikit/ui/page/login/login_page.dart';
import 'package:flutter_uikit/ui/widgets/api_subscription.dart';
import 'package:flutter_uikit/ui/widgets/gradient_button.dart';
import 'package:flutter_uikit/utils/uidata.dart';
import 'package:flutter_uikit/utils/translations.dart';

class LoginCard extends StatefulWidget {
@override
Expand Down Expand Up @@ -38,8 +38,10 @@ class _LoginCardState extends State<LoginCard>
enabled: !snapshot.data,
style: new TextStyle(fontSize: 15.0, color: Colors.black),
decoration: new InputDecoration(
hintText: UIData.enter_code_hint,
labelText: UIData.enter_code_label,
hintText:
Translations.of(context).text("enter_code_hint"),
labelText:
Translations.of(context).text("enter_code_label"),
labelStyle: TextStyle(fontWeight: FontWeight.w700)),
),
new SizedBox(
Expand All @@ -53,8 +55,10 @@ class _LoginCardState extends State<LoginCard>
style: new TextStyle(
fontSize: 15.0, color: Colors.black),
decoration: new InputDecoration(
hintText: UIData.enter_otp_hint,
labelText: UIData.enter_otp_label,
hintText: Translations.of(context)
.text("enter_otp_hint"),
labelText: Translations.of(context)
.text("enter_otp_label"),
labelStyle:
TextStyle(fontWeight: FontWeight.w700)),
obscureText: true,
Expand All @@ -69,7 +73,7 @@ class _LoginCardState extends State<LoginCard>
? loginBloc.otpSink.add(UserLoginViewModel(
phonenumber: phoneNumber))
: showPhoneError(context),
text: UIData.get_otp)
text: Translations.of(context).text("get_otp"))
: new GradientButton(
onPressed: () {
otp?.length == 4
Expand All @@ -78,11 +82,12 @@ class _LoginCardState extends State<LoginCard>
phonenumber: phoneNumber, otp: otp))
: showOTPError(context);
},
text: UIData.login),
text: Translations.of(context).text("login")),
),
snapshot.data == true
? new FlatButton(
child: Text(UIData.resend_otp),
child: Text(
Translations.of(context).text("resend_otp")),
onPressed: () => loginBloc.resendOtpSink.add(true),
)
: new Container()
Expand Down Expand Up @@ -127,8 +132,7 @@ class _LoginCardState extends State<LoginCard>
}

showPhoneError(BuildContext context) {
LoginProvider
.of(context)
LoginProvider.of(context)
.validationErrorCallback(LoginValidationType.phone);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/ui/page/shopping/shopping_details/shopping_action.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ShoppingActionState extends State<ShoppingAction> {
selected: _value == pc.colorName,
onSelected: (selected) {
setState(() {
return _value = selected ? pc.colorName : null;
_value = selected ? pc.colorName : null;
});
}),
))
Expand Down Expand Up @@ -76,7 +76,7 @@ class ShoppingActionState extends State<ShoppingAction> {
selected: _sizeValue == pc,
onSelected: (selected) {
setState(() {
return _sizeValue = selected ? pc : null;
_sizeValue = selected ? pc : null;
});
}),
))
Expand Down
46 changes: 46 additions & 0 deletions lib/utils/translations.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;

class Translations {
Translations(Locale locale) {
this.locale = locale;
_localizedValues = null;
}

Locale locale;
static Map<dynamic, dynamic> _localizedValues;

static Translations of(BuildContext context) {
return Localizations.of<Translations>(context, Translations);
}

String text(String key) {
return _localizedValues[key] ?? '** $key not found';
}

static Future<Translations> load(Locale locale) async {
Translations translations = new Translations(locale);
String jsonContent =
await rootBundle.loadString("locale/i18n_${locale.languageCode}.json");
_localizedValues = json.decode(jsonContent);
return translations;
}

get currentLanguage => locale.languageCode;
}

class TranslationsDelegate extends LocalizationsDelegate<Translations> {
const TranslationsDelegate();

@override
bool isSupported(Locale locale) => ['en', 'hi'].contains(locale.languageCode);

@override
Future<Translations> load(Locale locale) => Translations.load(locale);

@override
bool shouldReload(TranslationsDelegate old) => false;
}
12 changes: 12 additions & 0 deletions locale/i18n_en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"app_name":"फ़्लटर यूआई-किट ",
"enter_code_label":"फ़ोन नम्बर",
"enter_code_hint":"10 डिजिट का फ़ोन नम्बर ",
"enter_otp_label":"ओ टी पी",
"enter_otp_hint":"4 डिजिट ओ टी पी",
"get_otp":"ओ टी पी मँगायें ",
"resend_otp":"ओ टी पी दुबारा मँगायें",
"login":"लॉगिन",
"enter_valid_number":"10 डिजिट का ही नम्बर डालें",
"enter_valid_otp":"4 डिजिट का ही ओ टी पी डालें"
}
12 changes: 12 additions & 0 deletions locale/i18n_hi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"app_name":"Flutter UI-Kit ",
"enter_code_label":"Phone Number",
"enter_code_hint":"10 Digit Phone Number",
"enter_otp_label":"OTP",
"enter_otp_hint":"4 Digit OTP",
"get_otp":"Get OTP",
"resend_otp":"Resend OTP",
"login":"Login",
"enter_valid_number":"Enter 10 digit phone number",
"enter_valid_otp":"Enter 4 digit otp"
}
Loading

0 comments on commit f4901c7

Please sign in to comment.