How to add localization support inside Flutter packages - flutter

I am creating a Flutter package that has some text inside it. I want the consumer application of my package to pass locale to it, based on that locale my package should decide whether to show this text in 'Arabic' or 'English' (This means my package will have resource file containing strings inside it for these locales). How can I achieve this?
The only thing I was able to achieve was that my consumer application has the resource file and both my consumer application and package have to register the same localization plugin as dependency. I do not want my consumer app to worry about localization and instead my package should handle showing of translated strings based on locale. Is there anything wrong with my approach?

I have implemented the same by following what is done in another package - Catcher. Here is the file - https://github.com/jhomlala/catcher/blob/master/lib/model/localization_options.dart

In order to generate localization files inside a package:
Create localization files in .arb format
add l10n.yaml file inside the root folder of the package with these settings:
# Where to find translation files
arb-dir: lib/l10n
# Which translation is the default/template
template-arb-file: app_en.arb
# What to call generated dart files
output-localization-file: app_localizations.dart
make sure your main localization file and the 'template-arb-file' names are the same
update 'dependencies' and 'flutter' in pubspec.yaml file of the package as below:
dependencies:
flutter_localizations:
sdk: flutter
intl: ^0.17.0
flutter:
uses-material-design: true
generate: true
run 'flutter pub get' inside the package root location
Wrap your main UI widget with a MaterialApp
Update MaterialApp as below
MaterialApp(
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
),
Call the localization key inside a widget:
Text(AppLocalizations.of(context)!.helloWorld)
Note: Basically do almost same steps in official documentation inside your package root folder with a Custom MaterialApp
https://docs.flutter.dev/development/accessibility-and-localization/internationalization

Related

How to bundle asset for mobile apps only and not for Web

I'm trying to build a Flutter app with support for custom fonts. The quirk is, I ideally only want to bundle the fonts in mobile apps and not on Web without making significant changes to code at build time.
Is there any method?
I added Font Files into assets and pubspec, also used the package google_fonts.
Here's how you can do it:
Create two folders in your project directory: fonts and fonts_web.
Add your font files to the fonts folder.
Update your pubspec.yaml file to include the font files in the assets section:
yaml
flutter:
assets:
- fonts/
Then you need to Install google_fonts package to make it easy and smooth to use custom fonts in your app:
yaml
dependencies:
google_fonts: ^2.1.0
Then nn your code, import google_fonts package, and use GoogleFonts widget to specify custom font:
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
dart
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My App',
theme: ThemeData(
textTheme: GoogleFonts.openSansTextTheme(),
),
home: MyHomePage(),
);
}
}
For including the fonts only for mobile apps, you need use conditional imports based on platform. Then Replace the assets section in pubspec.yaml file with the code shown below :
yaml
flutter:
assets:
- ${{false ? "ignore" : "fonts/"}}
- ${{true ? "fonts_web/" : "ignore/"}}
Create the fonts_web folder, add an empty ignore file in it. It will prevent your fonts from being included in the web builds.

How to exclude particular Dart files while compilation in flutter

I'm trying to run my flutter-app in web. So I created a web folder and added all the necessary things. Since I'm using the objectBox in multiple dart files, I'm getting error based on ObjectBox.
So what I did to find the issue was, In the main.dart file i'm just trying to display only simple Container widget containing a text. But still I'm getting the same error related to objectBox with its entity.
So my doubt is, Is it compiling all the dart files located in lib? If then, can I exclude them.
I have tried(created a build.yaml file) to exclude all my files which are using ObjectBox.
My build.yaml file:
targets:
  $default:
sources:
exclude:
- lib/test_driver/runner.dart
- lib/tools/**.dart

Not a valid Dart package name - Flutter error

I want my app to be available for the web.
I run this command: flutter create .
I get this error: "Fredi" is not a valid Dart package name.
I know that there shouldn't be any capital letters. However, I do not know how to change the dart package name.
I have already used these packages successfully, but they seem to be changing other things, not the Dart Package Name.
change_app_package_name: ^1.1.0
rename: ^2.0.1
The only thing in my project with the name 'Fredi' is the root folder (original project name). However, I don't know how to change it (file explorer won't let me) and it will probably break things.
What step can I take?
Config Files
app/build.gradle -> applicationId "com.tomasward.fredi"
android manifest -> package="com.tomasward.fredi"
android manifest -> android:label="Fredi"> [I understand this is the name displayed to the user, I've tried changing it but it doesn't fix the error.]
You can run flutter create --project-name "your_valid_package_name" InvalidPackageNameDirectory to create a Flutter project in a directory with a non-valid package name.
In an already existing project, you can change the package name by modifying the name property in the pubspec.yaml at the root of the project (see here for more details on the pubspec).
You can probably rename your package name with the help of some dependency that you can find on pub.dev by typing "rename" in the search bar.
All those dependencies will help you rename your package to a lowercase only package name, then you will be able to use the flutter create . command to create the web folder for your app.

Combining multiple different flutter projects

So I have received a project which requires e-commerce, food ordering and car hailing services like Grab to be inside one app. I have delivered all these 3 apps in different projects before and for this new project I'm wondering whether it is possible to combine all my previous source code into one project, running on different dependencies.
You can create packages for all 3 modules (food, e-commerce, car-hailing services)
To create a Flutter package, use the --template=package flag with flutter create:
flutter create --template=package food
Folder Structure
lib
packages
- food
- e_commerce
- car_hailing_service
Import this in the main module's pubspec.yaml file
dependencies:
flutter:
sdk: flutter
# My Custom Packages
food:
path: packages/food
e_commerce:
path: packages/e_commerce
car_hailing_service:
path: packages/car_hailing_service
If you have any common functionalities for all 3 packages then create a separate package for that also and import that common package for all 3 packages.
Refer to this document for creating flutter packages. I hope this helps you.

Create a package in dart

How do I create a package in the new Dart Editor?
There is no "Add Pub support" checkbox?
Also how to create "packages" with the new editor?
Is a tutorial out there that describes the process with the new Editor?
To create a package named mypackage.
For Dart package:
dart create --template=package-simple mypackage
For Flutter package:
flutter create --template=package mypackage
From the Dart/Flutter Documentation:
Step 1: Create the package
To create a Flutter package, use the --template=package flag with flutter create:
flutter create --template=package hello
This creates a package project in the hello folder with the following content:
LICENSE
A (mostly) empty license text file.
test/hello_test.dart
The unit tests for the package.
hello.iml
A configuration file used by the IntelliJ IDEs.
.gitignore
A hidden file that tells Git which files or folders to ignore in a project.
.metadata
A hidden file used by IDEs to track the properties of the Flutter project.
pubspec.yaml
A yaml file containing metadata that specifies the package’s dependencies. Used by the pub tool.
README.md
A starter markdown file that briefly describes the package’s purpose.
lib/hello.dart
A starter app containing Dart code for the package.
.idea/modules.xml, .idea/modules.xml, .idea/workspace.xml**
A hidden folder containing configuration files for the IntelliJ IDEs.
CHANGELOG.md
A (mostly) empty markdown file for tracking version changes to the package.
There's no such possibilty in the Dart Editor for now. To create a package follow these steps :
create an New Application mylib without sample content
add a pubspec.yaml file
add a lib folder
create a mylib.dart containing the code you want to package
See the Package layout conventions for more informations.
You can create a dart project following the flutter way that allow you to auto generate the structure and the hierarchy of the package.
Follow below steps to create a package in DART:
Step 1: Create the package
$ flutter create --template=package hello
Step 2: Implement the package
For pure Dart packages, simply add the functionality inside the main lib/.dart file, or in several files in the lib directory.
To test the package, add unit tests in a test directory.
For additional details on how to organize the package contents, see the Dart library package documentation:
https://flutter.dev/docs/development/packages-and-plugins/developing-packages
Any dart app is a package. To create a new Dart app use:
dart create my_package