<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: MOHAMMAD ABU SALH</title>
    <description>The latest articles on Forem by MOHAMMAD ABU SALH (@mohammad_abusalh_e0e13d5).</description>
    <link>https://forem.com/mohammad_abusalh_e0e13d5</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3589663%2Ffcae5665-f78e-4578-af49-71d55a57e097.png</url>
      <title>Forem: MOHAMMAD ABU SALH</title>
      <link>https://forem.com/mohammad_abusalh_e0e13d5</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mohammad_abusalh_e0e13d5"/>
    <language>en</language>
    <item>
      <title>New Packge 🎉| Easiest Way To Make The Application Work In More Than One Language Automatically (Flutter Localization)</title>
      <dc:creator>MOHAMMAD ABU SALH</dc:creator>
      <pubDate>Thu, 30 Oct 2025 13:52:32 +0000</pubDate>
      <link>https://forem.com/mohammad_abusalh_e0e13d5/new-packge-easiest-way-to-make-the-application-work-in-more-than-one-language-automatically-k7</link>
      <guid>https://forem.com/mohammad_abusalh_e0e13d5/new-packge-easiest-way-to-make-the-application-work-in-more-than-one-language-automatically-k7</guid>
      <description>&lt;p&gt;🚀 Why Use Motrgem?&lt;br&gt;
Manually finding and converting all hardcoded text into localization keys can be tedious. Motrgem does this for you automatically by:&lt;br&gt;
Scanning your Flutter project for text in widgets like Text, AppBar, Button, Tooltip, etc.&lt;br&gt;
Generating unique, camelCase localization keys (e.g., "Hello World" → helloWorld)&lt;br&gt;
Updating .arb files with extracted texts&lt;br&gt;
Replacing hardcoded strings in your Dart code&lt;br&gt;
Managing localization imports and multi-language support&lt;/p&gt;




&lt;p&gt;Step-by-Step Guide&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Install Motrgem
&lt;/h3&gt;

&lt;p&gt;Run inside your project root:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;flutter pub add dev:motrgem&lt;br&gt;
dart pub global activate motrgem&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This adds motrgem to dev_dependencies and runs flutter pub get.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Initialize L10n in your project (one-time setup)
&lt;/h3&gt;

&lt;p&gt;Run (use dart run if Motrgem is a dev dependency):&lt;/p&gt;

&lt;p&gt;&lt;code&gt;dart run motrgem start&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;What this does:&lt;br&gt;
Adds required localization dependencies (flutter_localizations, intl, analyzer, etc.) to pubspec.yaml.&lt;br&gt;
Creates l10n.yaml config and lib/l10n/ directory.&lt;br&gt;
Generates an initial app_en.arb and enables flutter: generate: true.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Dry run (preview what will change)
&lt;/h3&gt;

&lt;p&gt;Before modifying files, preview extraction:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;dart run motrgem --dry-run&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This prints what strings would be extracted and where - no code changes. Use this to review and avoid surprises.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Extract texts and replace hardcoded strings
&lt;/h3&gt;

&lt;p&gt;When ready to apply changes:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;dart run motrgem --replace&lt;br&gt;
flutter clean&lt;br&gt;
flutter pub get&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;What happens:&lt;br&gt;
Motrgem scans source (using Dart analyzer), generates camelCase IDs and ARB entries, updates lib/l10n/app_en.arb, and replaces hardcoded strings in your Dart files with AppLocalizations references (e.g. Text('Hi') → Text(AppLocalizations.of(context)!.hi)).&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Important:
&lt;/h3&gt;

&lt;p&gt;After replacement, remove const from widgets that use AppLocalizations (e.g. const Text(AppLocalizations.of(context)!.helloWorld) should become Text(AppLocalizations.of(context)!.helloWorld)). Motrgem cannot keep them const.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Add additional locales
&lt;/h3&gt;

&lt;p&gt;Create new locale ARB files (example: Spanish, French, Arabic):&lt;/p&gt;

&lt;p&gt;&lt;code&gt;dart run motrgem --add-locale es&lt;br&gt;
dart run motrgem --add-locale fr&lt;br&gt;
dart run motrgem --add-locale ar&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This creates lib/l10n/app_es.arb, app_fr.arb etc., with auto transilation for the main file in english app_en.arb.&lt;/p&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h3&gt;
  
  
  🪄 Step 7 - Enable Language Switching at Runtime
&lt;/h3&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h4&gt;
  
  
  7.1 Add setLocale in main.dart to change language from anywhere:
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;class MyApp extends StatefulWidget { const MyApp({Key? key}) :   super(key: key);&lt;br&gt;
 @override&lt;br&gt;
  State&amp;lt;MyApp&amp;gt; createState() =&amp;gt; _MyAppState();&lt;br&gt;
  // Helper to change language from anywhere&lt;br&gt;
  static void setLocale(BuildContext context, Locale newLocale) {&lt;br&gt;
    _MyAppState? state = context.findAncestorStateOfType&amp;lt;_MyAppState&amp;gt;();&lt;br&gt;
    state?.setLocale(newLocale);&lt;br&gt;
  }&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  7.2 Add locale handling inside _MyAppStat in main.dart
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;class _MyAppState extends State&amp;lt;MyApp&amp;gt; { &lt;br&gt;
Locale? _locale;  &lt;br&gt;
void setLocale(Locale locale) {&lt;br&gt;
    setState(() =&amp;gt; _locale = locale);&lt;br&gt;
  }&lt;br&gt;
  @override&lt;br&gt;
  Widget build(BuildContext context) {&lt;br&gt;
    return MaterialApp(&lt;br&gt;
      locale: _locale,&lt;br&gt;
      localizationsDelegates: AppLocalizations.localizationsDelegates,&lt;br&gt;
      supportedLocales: AppLocalizations.supportedLocales,&lt;br&gt;
      home: const HomePage(),&lt;br&gt;
    );&lt;br&gt;
  }&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  7.3 Switch language anywhere in your app
&lt;/h4&gt;

&lt;p&gt;Call the helper method when the user chooses a new language:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;MyApp.setLocale(context, const Locale('es')); // Spanish&lt;br&gt;
MyApp.setLocale(context, const Locale('ar')); // Arabic&lt;br&gt;
MyApp.setLocale(context, const Locale('en')); // English&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Motrgem is open-source under the MIT License.&lt;br&gt;
Learn more: pub.dev/packages/motrgem&lt;br&gt;
Githup: &lt;a href="https://github.com/mohammadabusalh1/motrgem" rel="noopener noreferrer"&gt;https://github.com/mohammadabusalh1/motrgem&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks 🙏&lt;/p&gt;

&lt;p&gt;Mohammad Abu Salh&lt;br&gt;
&lt;a href="mailto:abusalhm102@gmail.com"&gt;abusalhm102@gmail.com&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/abusalh" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/abusalh&lt;/a&gt;&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>dart</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
