Skip to content

Commit

Permalink
refactor: migrate to gen_l10n for localization (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
brainwo authored Sep 12, 2024
1 parent 80c9ff1 commit a5f1a7e
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 39 deletions.
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/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
10 changes: 10 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"noSearchQuery": "No search query",
"noResultsFound": "No results were found for this search query",
"searchPlaceholderAll": "Search or paste your link here",
"searchPlaceholder": "Search...",
"errorInformation": "Error: ",
"errorTitle": "It appears that there is some kind of error",
"video": "Video",
"playlist": "Playlist"
}
22 changes: 0 additions & 22 deletions lib/locale/en_us.dart

This file was deleted.

23 changes: 15 additions & 8 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter/services.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:youtube_api/youtube_api.dart';

import 'const.dart';
import 'helper/command_parser.dart';
import 'intent.dart';
import 'locale/en_us.dart';
import 'model/config.dart';
import 'model/setting_options.dart';
import 'model/state.dart';
Expand Down Expand Up @@ -152,8 +153,11 @@ class App extends StatelessWidget {
'/history': (final _) => const HistoryPage(),
'/settings': (final _) => const SettingsPage(),
},
supportedLocales: [
const Locale('en'),
supportedLocales: AppLocalizations.supportedLocales,
localizationsDelegates: [
AppLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
],
),
);
Expand Down Expand Up @@ -200,7 +204,8 @@ class HomePage extends StatefulWidget {

class _HomePageState extends State<HomePage> {
late YoutubeApi youtubeApi;
String noResultString = AppString.noSearchQuery;
late String noResultString =
AppLocalizations.of(context)?.noSearchQuery ?? '';
Future<List<YoutubeVideo>>? searchResult;

final FocusNode searchBarFocus = FocusNode();
Expand Down Expand Up @@ -304,8 +309,10 @@ class _HomePageState extends State<HomePage> {
);
} else {
// Return error Widget here
return const SearchError(
errorText: AppString.noResultsFound);
return SearchError(
errorText:
AppLocalizations.of(context)?.noResultsFound ??
'');
}
},
),
Expand Down Expand Up @@ -435,8 +442,8 @@ class _TopBarState extends State<_TopBar> {
focusNode: widget.focusNode,
autocorrect: false,
placeholder: searchBoxMode.isSearchCategory
? AppString.searchPlaceholder
: AppString.searchPlaceholderAll,
? AppLocalizations.of(context)?.searchPlaceholder
: AppLocalizations.of(context)?.searchPlaceholderAll,
controller: textBoxController,
onSubmitted: _handleTopBar,
),
Expand Down
2 changes: 1 addition & 1 deletion lib/widget/list_items/list_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import 'dart:io';

import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter/services.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:youtube_api/youtube_api.dart';
import '../../helper/command_parser.dart';
import '../../helper/time.dart';
import '../../intent.dart';
import '../../locale/en_us.dart';
import '../keyboard_navigation.dart';

part 'channel.dart';
Expand Down
6 changes: 3 additions & 3 deletions lib/widget/list_items/playlist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ class ListItemPlaylist extends StatelessWidget {
style: const TextStyle(fontWeight: FontWeight.w300),
),
const SizedBox(height: 8),
const Text(
AppString.playlist,
style: TextStyle(fontWeight: FontWeight.w300),
Text(
AppLocalizations.of(context)?.playlist ?? '',
style: const TextStyle(fontWeight: FontWeight.w300),
),
],
),
Expand Down
5 changes: 2 additions & 3 deletions lib/widget/search_error.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter/services.dart';

import '../locale/en_us.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

// TODO: use Regex to match known errors
// YouTube Data API v3 has not been used in project 144809266352 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?project=144809266352 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
Expand Down Expand Up @@ -45,7 +44,7 @@ class SearchError extends StatelessWidget {
),
const SizedBox(height: 8),
Text(
AppString.errorTitle,
AppLocalizations.of(context)?.errorTitle ?? '',
style: FluentTheme.of(context).typography.title,
textAlign: TextAlign.center,
),
Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ packages:
source: sdk
version: "0.0.0"
flutter_localizations:
dependency: transitive
dependency: "direct main"
description: flutter
source: sdk
version: "0.0.0"
Expand Down Expand Up @@ -293,7 +293,7 @@ packages:
source: hosted
version: "4.0.2"
intl:
dependency: transitive
dependency: "direct main"
description:
name: intl
sha256: "3bc132a9dbce73a7e4a21a17d06e1878839ffbf975568bc875c60537824b0c4d"
Expand Down
4 changes: 4 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ dependencies:
fluent_ui: ^4.7.3
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
flutter_riverpod: ^2.3.6
intl: any
rss_dart:
git:
url: https://github.com/brainwo/rss.dart.git
Expand All @@ -36,3 +39,4 @@ dev_dependencies:

flutter:
uses-material-design: true
generate: true

0 comments on commit a5f1a7e

Please sign in to comment.