Skip to content

Commit

Permalink
refactor: switch from localizel to intl (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkkiller authored Jan 3, 2024
1 parent d3468ac commit 6650041
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 48 deletions.
1 change: 0 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"recommendations": [
"Dart-Code.dart-code",
"Dart-Code.flutter",
"localizely.flutter-intl",
"FelixAngelov.bloc",
"redhat.vscode-yaml"
],
Expand Down
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/src/core/localization/translations
template-arb-file: intl_en.arb
output-localization-file: app_localizations.dart
44 changes: 11 additions & 33 deletions lib/src/core/localization/localization.dart
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:sizzle_starter/src/core/localization/generated/l10n.dart';

/// {@template localization}
/// Localization class which is used to localize app.
/// This class provides handy methods and tools.
/// {@endtemplate}
final class Localization extends GeneratedLocalization {
final class Localization {
/// {@macro localization}
Localization._({required this.locale});

/// {@macro localization}
static const localizationDelegate = _LocalizationDelegate(
AppLocalizationDelegate(),
);
const Localization._({required this.locale});

/// List of supported locales.
static List<Locale> get supportedLocales =>
const AppLocalizationDelegate().supportedLocales;
static List<Locale> get supportedLocales => AppLocalizations.supportedLocales;

static const _delegate = AppLocalizations.delegate;

/// List of localization delegates.
static List<LocalizationsDelegate<void>> get localizationDelegates => [
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
Localization.localizationDelegate,
_delegate,
];

/// {@macro localization}
Expand All @@ -42,31 +38,13 @@ final class Localization extends GeneratedLocalization {
static Locale computeDefaultLocale() {
final locale = WidgetsBinding.instance.platformDispatcher.locale;

if (const AppLocalizationDelegate().isSupported(locale)) return locale;
if (_delegate.isSupported(locale)) return locale;

return const Locale('en');
}

/// Obtain [Localization] instance from [BuildContext].
static Localization of(BuildContext context) =>
Localizations.of<Localization>(context, Localization) ??
/// Obtain [AppLocalizations] instance from [BuildContext].
static AppLocalizations of(BuildContext context) =>
AppLocalizations.of(context) ??
(throw ArgumentError('No Localization found in context'));
}

final class _LocalizationDelegate extends LocalizationsDelegate<Localization> {
const _LocalizationDelegate(this._delegate);

final AppLocalizationDelegate _delegate;

@override
bool isSupported(Locale locale) => _delegate.isSupported(locale);

@override
Future<Localization> load(Locale locale) =>
GeneratedLocalization.load(locale).then(
(value) => Localization._current = Localization._(locale: locale),
);

@override
bool shouldReload(_LocalizationDelegate old) => false;
}
12 changes: 11 additions & 1 deletion lib/src/core/localization/translations/intl_en.arb
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
{
"@@locale": "en",
"appTitle": "sizzle_starter",
"@appTitle": {
"description": "The title of the application"
},
"custom_colors": "Custom Colors",
"@custom_colors": {
"description": "Custom colors for the application"
},
"default_themes": "Default Themes",
"locales": "Locales"
"@default_themes": {
"description": "Default themes for the application"
},
"locales": "Locales",
"@locales": {
"description": "Locales for the application"
}
}
17 changes: 9 additions & 8 deletions lib/src/core/localization/translations/intl_es.arb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"appTitle": "sizzle_starter",
"@appTitle": {
"description": "El título de la aplicación"
},
"custom_colors": "Colores personalizados",
"default_themes": "Temas predeterminados",
"locales": "Idioma"
}
"@@locale": "es",
"appTitle": "sizzle_starter",
"@appTitle": {
"description": "El título de la aplicación"
},
"custom_colors": "Colores personalizados",
"default_themes": "Temas predeterminados",
"locales": "Idioma"
}
7 changes: 2 additions & 5 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ dependencies:
collection: any
async: any
meta: any

# Localization
intl: 0.18.1
intl: any
flutter_localizations:
sdk: flutter

Expand Down Expand Up @@ -68,7 +66,6 @@ dev_dependencies:
# Utils
flutter_gen_runner: ^5.3.2
flutter_launcher_icons: ^0.13.1
intl_utils: ^2.8.5
flutter_native_splash: ^2.3.5

# Codegen
Expand All @@ -80,7 +77,7 @@ dev_dependencies:
flutter:
uses-material-design: true

generate: false
generate: true

assets:
# Add assets from the images directory to the application.
Expand Down

0 comments on commit 6650041

Please sign in to comment.