Skip to content

Commit

Permalink
added Multi-Language Support and enabled arabic and english with arab…
Browse files Browse the repository at this point in the history
…ic fonts.
  • Loading branch information
Furqankhanzada committed Nov 7, 2019
1 parent d4298b0 commit 7fcd556
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 7 deletions.
Binary file added assets/fonts/dubai/Dubai-Bold.ttf
Binary file not shown.
Binary file added assets/fonts/dubai/Dubai-Light.ttf
Binary file not shown.
Binary file added assets/fonts/dubai/Dubai-Medium.ttf
Binary file not shown.
Binary file added assets/fonts/dubai/Dubai-Regular.ttf
Binary file not shown.
3 changes: 3 additions & 0 deletions assets/i18n/ar.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"NEW_ARRIVALS": "القادمون الجدد"
}
3 changes: 3 additions & 0 deletions assets/i18n/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"NEW_ARRIVALS": "New Arrivals"
}
16 changes: 10 additions & 6 deletions lib/home/home.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_scaffold/localizations.dart';

import 'drawer.dart';
import 'slider.dart';
Expand All @@ -18,7 +19,6 @@ class _HomeState extends State<Home> {
'https://images.unsplash.com/photo-1508704019882-f9cf40e475b4?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=8c6e5e3aba713b17aa1fe71ab4f0ae5b&auto=format&fit=crop&w=1352&q=80',
'https://images.unsplash.com/photo-1519985176271-adb1088fa94c?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=a0c8d632e977f94e5d312d9893258f59&auto=format&fit=crop&w=1355&q=80'
];

@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down Expand Up @@ -59,8 +59,11 @@ class _HomeState extends State<Home> {
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 14.0, left: 8.0),
child: Text('New Arrivals',
padding:
EdgeInsets.only(top: 14.0, left: 8.0, right: 8.0),
child: Text(
AppLocalizations.of(context)
.translate('NEW_ARRIVALS'),
style: TextStyle(
color: Theme.of(context).accentColor,
fontSize: 18,
Expand Down Expand Up @@ -141,16 +144,17 @@ class _HomeState extends State<Home> {
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 8.0, left: 8.0),
padding: EdgeInsets.only(
top: 8.0, left: 8.0, right: 8.0),
child: Text('Shop By Category',
style: TextStyle(
color: Theme.of(context).accentColor,
fontSize: 18,
fontWeight: FontWeight.w700)),
),
Padding(
padding:
const EdgeInsets.only(right: 8.0, top: 8.0),
padding: const EdgeInsets.only(
right: 8.0, top: 8.0, left: 8.0),
child: RaisedButton(
color: Theme.of(context).primaryColor,
child: Text('View All',
Expand Down
41 changes: 41 additions & 0 deletions lib/localizations.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_scaffold/localizations_delegate.dart';

class AppLocalizations {
final Locale locale;

AppLocalizations(this.locale);

// Helper method to keep the code in the widgets concise
// Localizations are accessed using an InheritedWidget "of" syntax
static AppLocalizations of(BuildContext context) {
return Localizations.of<AppLocalizations>(context, AppLocalizations);
}

// Static member to have a simple access to the delegate from the MaterialApp
static const LocalizationsDelegate<AppLocalizations> delegate =
AppLocalizationsDelegate();

Map<String, String> _localizedStrings;

Future<bool> load() async {
// Load the language JSON file from the "lang" folder
String jsonString =
await rootBundle.loadString('assets/i18n/${locale.languageCode}.json');
Map<String, dynamic> jsonMap = jsonDecode(jsonString);

_localizedStrings = jsonMap.map((key, value) {
return MapEntry(key, value.toString());
});

return true;
}

// This method will be called from every widget which needs a localized text
String translate(String key) {
return _localizedStrings[key];
}
}
25 changes: 25 additions & 0 deletions lib/localizations_delegate.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:flutter/material.dart';
import 'package:flutter_scaffold/localizations.dart';

class AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations> {
// This delegate instance will never change (it doesn't even have fields!)
// It can provide a constant constructor.
const AppLocalizationsDelegate();

@override
bool isSupported(Locale locale) {
// Include all of your supported language codes here
return ['en', 'ar'].contains(locale.languageCode);
}

@override
Future<AppLocalizations> load(Locale locale) async {
// AppLocalizations class is where the JSON loading actually runs
AppLocalizations localizations = new AppLocalizations(locale);
await localizations.load();
return localizations;
}

@override
bool shouldReload(AppLocalizationsDelegate old) => false;
}
12 changes: 11 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_scaffold/auth/auth.dart';
import 'package:flutter_scaffold/blocks/auth_block.dart';
import 'package:flutter_scaffold/cart.dart';
import 'package:flutter_scaffold/categorise.dart';
import 'package:flutter_scaffold/home/home.dart';
import 'package:flutter_scaffold/localizations.dart';
import 'package:flutter_scaffold/product_detail.dart';
import 'package:flutter_scaffold/settings.dart';
import 'package:flutter_scaffold/shop/shop.dart';
Expand All @@ -12,14 +14,22 @@ import 'package:provider/provider.dart';

void main() {
WidgetsFlutterBinding.ensureInitialized();
final Locale locale = Locale('en');
runApp(MultiProvider(
providers: [ChangeNotifierProvider<AuthBlock>.value(value: AuthBlock())],
child: MaterialApp(
localizationsDelegates: [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate
],
supportedLocales: [Locale('en'), Locale('ar')],
locale: locale,
debugShowCheckedModeBanner: false,
theme: ThemeData(
primaryColor: Colors.deepOrange[500],
accentColor: Colors.lightBlue[900],
fontFamily: 'Lato'),
fontFamily: locale.languageCode == 'ar' ? 'Dubai' : 'Lato'),
initialRoute: '/',
routes: <String, WidgetBuilder>{
'/': (BuildContext context) => Home(),
Expand Down
12 changes: 12 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.3"
flutter_localizations:
dependency: "direct main"
description: flutter
source: sdk
version: "0.0.0"
flutter_secure_storage:
dependency: "direct main"
description:
Expand Down Expand Up @@ -130,6 +135,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.4"
intl:
dependency: "direct main"
description:
name: intl
url: "https://pub.dartlang.org"
source: hosted
version: "0.16.0"
matcher:
dependency: transitive
description:
Expand Down
14 changes: 14 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ environment:
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
intl: ^0.16.0
carousel_slider: ^1.3.1
transparent_image: ^1.0.0
cached_network_image: 2.0.0-rc
Expand Down Expand Up @@ -54,6 +57,8 @@ flutter:
# - images/a_dot_ham.jpegslide2.jpg
#summer-slider.png
assets:
- assets/i18n/en.json
- assets/i18n/ar.json
- assets/images/drawer-header.jpg
- assets/images/banner-1.png
- assets/images/banner-2.png
Expand Down Expand Up @@ -106,6 +111,15 @@ flutter:
- asset: assets/fonts/lato/Lato-ThinItalic.ttf
weight: 100
style: italic
- family: Dubai
fonts:
- asset: assets/fonts/dubai/Dubai-Regular.ttf
- asset: assets/fonts/dubai/Dubai-Light.ttf
weight: 300
- asset: assets/fonts/dubai/Dubai-Bold.ttf
weight: 700
- asset: assets/fonts/dubai/Dubai-Medium.ttf
weight: 900

# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages

0 comments on commit 7fcd556

Please sign in to comment.