add i18n package #164
@ -15,6 +15,7 @@
 | 
				
			|||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
 | 
					// along with this program. If not, see <https://www.gnu.org/licenses/>.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import 'package:flutter/material.dart';
 | 
					import 'package:flutter/material.dart';
 | 
				
			||||||
 | 
					import 'package:flutter_localizations/flutter_localizations.dart';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import 'package:wyatt_i18n/wyatt_i18n.dart';
 | 
					import 'package:wyatt_i18n/wyatt_i18n.dart';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -28,51 +29,36 @@ class App extends StatelessWidget {
 | 
				
			|||||||
  static const String title = 'Wyatt i18n Example';
 | 
					  static const String title = 'Wyatt i18n Example';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @override
 | 
					  @override
 | 
				
			||||||
  Widget build(BuildContext context) => MaterialApp(
 | 
					  Widget build(BuildContext context) {
 | 
				
			||||||
        title: title,
 | 
					    final I18nDataSource dataSource = NetworkDataSourceImpl(
 | 
				
			||||||
        theme: ThemeData(
 | 
					      baseUri: Uri.parse(
 | 
				
			||||||
          primarySwatch: Colors.blue,
 | 
					        'https://git.wyatt-studio.fr/Wyatt-FOSS/wyatt-packages/raw/commit/75f561a19e0484e67e511dbf29601ec5f58544aa/packages/wyatt_i18n/example/assets/',
 | 
				
			||||||
      ),
 | 
					      ),
 | 
				
			||||||
        home: Scaffold(
 | 
					    );
 | 
				
			||||||
          appBar: AppBar(
 | 
					 | 
				
			||||||
            title: const Text(title),
 | 
					 | 
				
			||||||
          ),
 | 
					 | 
				
			||||||
          body: Center(
 | 
					 | 
				
			||||||
            child: FutureBuilder<String>(
 | 
					 | 
				
			||||||
              future: Future(() async {
 | 
					 | 
				
			||||||
                // test
 | 
					 | 
				
			||||||
                const I18nDataSource dataSource = AssetsFileDataSourceImpl();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    final I18nRepository repository =
 | 
					    final I18nRepository repository =
 | 
				
			||||||
        I18nRepositoryImpl(dataSource: dataSource);
 | 
					        I18nRepositoryImpl(dataSource: dataSource);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                final test1 = (await repository.load(locale: 'en')).ok;
 | 
					    return MaterialApp(
 | 
				
			||||||
                // final test2 = (await repository.loadFrom(
 | 
					      title: title,
 | 
				
			||||||
                //   Uri.parse('assets/i18n.en.yaml'),
 | 
					      theme: ThemeData(
 | 
				
			||||||
                //   parser: const YamlParser(),
 | 
					        primarySwatch: Colors.blue,
 | 
				
			||||||
                // ))
 | 
					      ),
 | 
				
			||||||
                //     .ok;
 | 
					      localizationsDelegates: [
 | 
				
			||||||
 | 
					        I18nDelegate(repository: repository),
 | 
				
			||||||
                // print(test1?.locale);
 | 
					        GlobalMaterialLocalizations.delegate,
 | 
				
			||||||
                // print(test1?.data);
 | 
					        GlobalWidgetsLocalizations.delegate
 | 
				
			||||||
                // print(test1?.data['btnAddFile']);
 | 
					      ],
 | 
				
			||||||
 | 
					      home: Scaffold(
 | 
				
			||||||
                final text = repository.get('youHavePushed', {
 | 
					        appBar: AppBar(
 | 
				
			||||||
                  'count': 8,
 | 
					          title: const Text(title),
 | 
				
			||||||
                });
 | 
					        ),
 | 
				
			||||||
 | 
					        body: Builder(
 | 
				
			||||||
                print(text.ok);
 | 
					          builder: (ctx) => Center(
 | 
				
			||||||
 | 
					            child: Text(ctx.i18n('youHavePushed', {'count': 654})),
 | 
				
			||||||
 | 
					 | 
				
			||||||
                // print(test2?.locale);
 | 
					 | 
				
			||||||
                // print(test2?.data);
 | 
					 | 
				
			||||||
                // print(test2?.data['btnAddFile']);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                return 'test';
 | 
					 | 
				
			||||||
              }),
 | 
					 | 
				
			||||||
              builder: (context, snapshot) => const Text('WyattI18n'),
 | 
					 | 
				
			||||||
          ),
 | 
					          ),
 | 
				
			||||||
        ),
 | 
					        ),
 | 
				
			||||||
      ),
 | 
					      ),
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -1,11 +1,12 @@
 | 
				
			|||||||
name: wyatt_i18n_example
 | 
					name: wyatt_i18n_example
 | 
				
			||||||
description: A new Flutter project.
 | 
					description: A new Flutter project.
 | 
				
			||||||
version: 1.0.0
 | 
					version: 1.0.0
 | 
				
			||||||
publish_to: 'none'
 | 
					publish_to: "none"
 | 
				
			||||||
environment:
 | 
					environment:
 | 
				
			||||||
  sdk: ">=2.19.0 <3.0.0"
 | 
					  sdk: ">=2.19.0 <3.0.0"
 | 
				
			||||||
dependencies:
 | 
					dependencies:
 | 
				
			||||||
  flutter: { sdk: flutter }
 | 
					  flutter: { sdk: flutter }
 | 
				
			||||||
 | 
					  flutter_localizations: { sdk: flutter }
 | 
				
			||||||
  wyatt_i18n:
 | 
					  wyatt_i18n:
 | 
				
			||||||
    path: "../"
 | 
					    path: "../"
 | 
				
			||||||
dev_dependencies:
 | 
					dev_dependencies:
 | 
				
			||||||
 | 
				
			|||||||
@ -16,4 +16,5 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
export 'enums/format.dart';
 | 
					export 'enums/format.dart';
 | 
				
			||||||
export 'exceptions/exceptions.dart';
 | 
					export 'exceptions/exceptions.dart';
 | 
				
			||||||
 | 
					export 'extensions/build_context_extension.dart';
 | 
				
			||||||
export 'utils/utils.dart';
 | 
					export 'utils/utils.dart';
 | 
				
			||||||
 | 
				
			|||||||
@ -0,0 +1,36 @@
 | 
				
			|||||||
 | 
					// Copyright (C) 2023 WYATT GROUP
 | 
				
			||||||
 | 
					// Please see the AUTHORS file for details.
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					// This program is free software: you can redistribute it and/or modify
 | 
				
			||||||
 | 
					// it under the terms of the GNU General Public License as published by
 | 
				
			||||||
 | 
					// the Free Software Foundation, either version 3 of the License, or
 | 
				
			||||||
 | 
					// any later version.
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					// This program is distributed in the hope that it will be useful,
 | 
				
			||||||
 | 
					// but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
				
			||||||
 | 
					// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 | 
				
			||||||
 | 
					// GNU General Public License for more details.
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					// You should have received a copy of the GNU General Public License
 | 
				
			||||||
 | 
					// along with this program. If not, see <https://www.gnu.org/licenses/>.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import 'package:flutter/widgets.dart';
 | 
				
			||||||
 | 
					import 'package:wyatt_i18n/wyatt_i18n.dart';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Provides extensions for [BuildContext].
 | 
				
			||||||
 | 
					extension BuildContextExtension on BuildContext {
 | 
				
			||||||
 | 
					  /// Returns the current locale of the app.
 | 
				
			||||||
 | 
					  String get locale => Localizations.localeOf(this).languageCode;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /// Returns the current [I18n] instance.
 | 
				
			||||||
 | 
					  ///
 | 
				
			||||||
 | 
					  /// Throws a [NotLoadedException] if the [I18n] instance is not loaded.
 | 
				
			||||||
 | 
					  I18n get i18n {
 | 
				
			||||||
 | 
					    final i18n = Localizations.of<I18n>(this, I18n);
 | 
				
			||||||
 | 
					    if (i18n == null) {
 | 
				
			||||||
 | 
					      throw NotLoadedException();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return i18n;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										51
									
								
								packages/wyatt_i18n/lib/src/presentation/i18n_delegate.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								packages/wyatt_i18n/lib/src/presentation/i18n_delegate.dart
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,51 @@
 | 
				
			|||||||
 | 
					// Copyright (C) 2023 WYATT GROUP
 | 
				
			||||||
 | 
					// Please see the AUTHORS file for details.
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					// This program is free software: you can redistribute it and/or modify
 | 
				
			||||||
 | 
					// it under the terms of the GNU General Public License as published by
 | 
				
			||||||
 | 
					// the Free Software Foundation, either version 3 of the License, or
 | 
				
			||||||
 | 
					// any later version.
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					// This program is distributed in the hope that it will be useful,
 | 
				
			||||||
 | 
					// but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
				
			||||||
 | 
					// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 | 
				
			||||||
 | 
					// GNU General Public License for more details.
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					// You should have received a copy of the GNU General Public License
 | 
				
			||||||
 | 
					// along with this program. If not, see <https://www.gnu.org/licenses/>.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import 'package:flutter/cupertino.dart';
 | 
				
			||||||
 | 
					import 'package:wyatt_i18n/wyatt_i18n.dart';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// {@template i18n_delegate}
 | 
				
			||||||
 | 
					/// A [LocalizationsDelegate] that loads the [I18n] instance.
 | 
				
			||||||
 | 
					/// {@endtemplate}
 | 
				
			||||||
 | 
					class I18nDelegate extends LocalizationsDelegate<I18n> {
 | 
				
			||||||
 | 
					  /// {@macro i18n_delegate}
 | 
				
			||||||
 | 
					  I18nDelegate({
 | 
				
			||||||
 | 
					    required this.repository,
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /// The [I18nRepository] that will be used to load the i18n file.
 | 
				
			||||||
 | 
					  final I18nRepository repository;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /// The current locale.
 | 
				
			||||||
 | 
					  Locale? currentLocale;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /// Since we are using the [I18nRepository] to load the i18n file,
 | 
				
			||||||
 | 
					  /// we don't need to check if the locale is supported.
 | 
				
			||||||
 | 
					  /// We can just return `true`.
 | 
				
			||||||
 | 
					  @override
 | 
				
			||||||
 | 
					  bool isSupported(Locale locale) => true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @override
 | 
				
			||||||
 | 
					  Future<I18n> load(Locale locale) async {
 | 
				
			||||||
 | 
					    await repository.load(locale: locale.languageCode);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return I18n(i18nRepository: repository);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @override
 | 
				
			||||||
 | 
					  bool shouldReload(I18nDelegate old) =>
 | 
				
			||||||
 | 
					      currentLocale != null || currentLocale != old.currentLocale;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -17,3 +17,4 @@
 | 
				
			|||||||
export 'core/core.dart';
 | 
					export 'core/core.dart';
 | 
				
			||||||
export 'data/data.dart';
 | 
					export 'data/data.dart';
 | 
				
			||||||
export 'domain/domain.dart';
 | 
					export 'domain/domain.dart';
 | 
				
			||||||
 | 
					export 'presentation/i18n_delegate.dart';
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user