Sunday, April 7, 2024

How to Implement Multi-Language Support in Flutter app (Localization)

Hey dear readers, welcome back in our blog 1GB Developer. Now we are going to implement the feature which is responsible for supporting multi-language in your device. For example if you dont like english then change the app language into hindi.

So let start our coding for multi-language  support in flutter application :


Step 1. 

create two file named a) app_en.arb and b)app_hi.arb in lib/l10n :


a) app_hi.arb which contain data like this:

{

  "signup_welcome_desc": "เคถुเคฐू เค•เคฐเคจे เค•े เคฒिเค เคซॉเคฐ्เคฎ เคญเคฐें",

  "welcome_back": "เคตाเคชเคธी เคชเคฐ เคธ्เคตाเค—เคค เคนै!",

  "login_welcome_desc": "เค…เคชเคจे เค–ाเคคा เค•ो เคช्เคฐเคฌंเคงिเคค เค•เคฐเคจे เค•े เคฒिเค เคฒॉเค—िเคจ เค•เคฐें",

  "mobile_no": "เคฎोเคฌाเค‡เคฒ เคจंเคฌเคฐ",

  "email_address": "เคˆเคฎेเคฒ เคชเคคा",

  "mobile_no_email":"เคฎोเคฌाเค‡เคฒ เคจंเคฌเคฐ/เคˆเคฎेเคฒ",

  "forgot_password":"เคชाเคธเคตเคฐ्เคก เคญूเคฒ เค—เค?",

  "password": "เคชाเคธเคตเคฐ्เคก",

  "confirm_password": "เคชाเคธเคตเคฐ्เคก เค•ी เคชुเคท्เคŸि เค•เคฐें",

  "name":"เคจाเคฎ",

  "referral":"เคฐेเคซ़เคฐเคฒ เคฆเคฐ्เคœ เค•เคฐें (เคตैเค•เคฒ्เคชिเค•)",

  "get_started": "เคถुเคฐू เคนो เคœाเค“",

  "have_an_account": "{prefix} เคเค• เค–ाเคคा เคนै",

  "login": "เคฒॉเค—िเคจ",

  "sign_up": "เคธाเค‡เคจ เค…เคช",

  "trending_products": "เคšเคฐ्เคšिเคค เค‰เคค्เคชाเคฆ",

  "popular_products": "เคฒोเค•เคช्เคฐिเคฏ เค‰เคค्เคชाเคฆ",

  "new_products": "เคจเค เค‰เคค्เคชाเคฆ",

  "known_products": "เคช्เคฐเคธिเคฆ्เคง เค‰เคค्เคชाเคฆ",

  "signup_for_newsletter": "เคธเคฎाเคšाเคฐ เคชเคค्เคฐिเค•ा เค•े เคฒिเค เคธाเค‡เคจ เค…เคช เค•เคฐें",

  "contact_info": "เคธंเคชเคฐ्เค• เคœाเคจเค•ाเคฐी",

  "rupee":"₹. {value}",

  "edit_profile": "เคช्เคฐोเคซ़ाเค‡เคฒ เคธंเคชाเคฆिเคค เค•เคฐें",

  "wallet": "เคฌเคŸुเค†",

  "manage_address": "เคชเคคे เค•ा เคช्เคฐเคฌंเคงเคจ",

  "language": "เคญाเคทा",

  "setting": "เคธेเคŸिंเค—्เคธ",

  "privacy": "เค—ोเคชเคจीเคฏเคคा",

  "sign_out": "เคธाเค‡เคจ เค†เค‰เคŸ",

  "your_orders": "เค†เคชเค•े เค†เคฆेเคถ",

  "buy_again": "เคซिเคฐ เคธे เค–เคฐीเคฆें",

  "your_account": "เค†เคชเค•ा เค–ाเคคा",

  "your_lists": "เค†เคชเค•ी เคธूเคšिเคฏाँ",

  "term_and_condition": "เคจिเคฏเคฎ เค”เคฐ เคถเคฐ्เคคें",

  "search": "เค–ोเคœें",

  "add_to_cart": "เค•ाเคฐ्เคŸ เคฎें เคกाเคฒें",

  "add_to_wishlist": "เค‡เคš्เค›ा เคธूเคšी เคฎें เคœोเคก़ें",

  "added_to_wishlist": "เค‡เคš्เค›ा เคธूเคšी เคฎें เคœोเคก़ा เค—เคฏा",

  "percent_discount": "{value}% เค›ूเคŸ",

  "compare": "เคคुเคฒเคจा เค•เคฐें",

  "color": "เคฐंเค—",

  "delivery": "เคกिเคฒीเคตเคฐी",

  "product_description": "เค‰เคค्เคชाเคฆ เคตिเคตเคฐเคฃ",

  "available_offers": "เค‰เคชเคฒเคฌ्เคง เค‘เคซ़เคฐ",

  "quantity": "เคฎाเคค्เคฐा",

  "go_to_cart": "เค•ाเคฐ्เคŸ เคชเคฐ เคœाเคं",

  "checkout": "เคšेเค•เค†เค‰เคŸ",

  "save_for_later": "เคฌाเคฆ เคฎें เคธเคนेเคœें",

  "delete": "เคนเคŸाเคं",

  "proceed_with_defaul_address": "เคกिเคซ़ॉเคฒ्เคŸ เคชเคคे เค•े เคธाเคฅ เค†เค—े เคฌเคข़ें",

  "proceed": "เค†เค—े เคฌเคข़ें",

  "pay_online": "เค‘เคจเคฒाเค‡เคจ เคญुเค—เคคाเคจ เค•เคฐें",

  "cash_on_delivery": "เค•ैเคถ เค‘เคจ เคกिเคฒीเคตเคฐी"

}


b) app_hi.arb which contain data like this:


{

  "signup_welcome_desc":"Fill out the form to get started",

  "welcome_back":"Sign in",

  "login_welcome_desc":"Login to manage your account",

  "mobile_no_email":"Mobile No./Email",

  "mobile_no":"Mobile No.",

  "forgot_password":"Forgot Password?",

  "email_address":"Email address",

  "password":"Password",

  "confirm_password":"Confirm Password",

  "name":"Name",

  "referral":"Enter Referral (optional)",

  "get_started":"Get Started",

  "have_an_account":"{prefix} have an account.",

  "login":"Login",

  "sign_up":"Sign Up",

  "trending_products": "Trending Products",

  "popular_products":"Popular Products",

  "new_products":"New Products",

  "known_products":"Known Products",

  "signup_for_newsletter":"Sign up for Newsletter",

  "contact_info":"Contact Info",

  "rupee":"Rs. {value}",

  "edit_profile":"Edit Profile",

  "wallet":"Wallet",

  "manage_address":"Manage Address",

  "language":"Language",

  "setting":"Setting",

  "privacy":"Privacy",

  "sign_out":"Sign Out",

  "your_orders":"Your Orders",

  "buy_again":"Buy Again",

  "your_account":"Your Account",

  "your_lists":"Your Lists",

  "term_and_condition":"Term and conditions",

  "search":"Search",

  "add_to_cart":"Add to cart",

  "add_to_wishlist":"Add to wishlist",

  "added_to_wishlist":"Added to wishlist",

  "percent_discount":"{value}% off",

  "compare":"Compare",

  "color":"Color",

  "delivery":"Delivery",

  "product_description":"Product Description",

  "available_offers":"Available Offers",

  "quantity":"Quantity",

  "go_to_cart":"Go to Kart",

  "checkout":"Checkout",

  "save_for_later":"Save for Later",

  "delete":"Delete",

  "proceed_with_defaul_address":"Proceed with default address",

  "proceed":"Proceed",

  "pay_online":"Pay Online",

  "cash_on_delivery":"Cash On Delivery"


}


step 2.  

In root directory where pubs.yaml file is present create new file with name l10n.yaml and put this code inside:


arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart


Step 3. 

Go to main.dart file and put this code :



import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
@override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Localizations Sample App',
      debugShowCheckedModeBanner: false,
      locale: Locale('hi'),
      localizationsDelegates: const [
        AppLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: const [
        Locale('en'),
        Locale('hi'),
      ],



Now this path will generated later when you build the code:

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

Also dont forget to add this line in your pubs.yaml file:

# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter packages.
flutter:
  generate: true

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  assets:
    - assets/
  #   - images/a_dot_ham.jpeg
  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.dev/assets-and-images/#resolution-aware
  # For details regarding adding assets from package dependencies, see
  # https://flutter.dev/assets-and-images/#from-packages
  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For

This generate:true will generate the file for localization.

also install this package: intl:^0.18.1


Thank you. Happy flutter  ๐Ÿ˜ƒ

No comments:

Post a Comment

๐Ÿ’˜ Constructors & Destructors in C++ – Jab GF-BF Ke Life Me Entry Aur Breakup Hota ๐Ÿ’”๐Ÿฅน

Constructors & Destructors in C++ – GF-BF Format ๐Ÿ’ก "Constructor tab call hota jab relationship start hoyi ...