Skip to main content

🌐 Localization Guide

Follow this guide to localize text in your Flutter project effectively. 🚀


🛠️ Steps for Localizing Text

1. Localizing Hardcoded Strings 📝

Definition: A hardcoded string is a fixed, unchanging piece of text with no placeholders or variables.

Steps:

1️⃣ Identify the hardcoded string in your project. 2️⃣ Add it to your app_en.arb file (lib\l10n\app_en.arb) using a filenamehere_theWordExactlyInEng name:

{
"home_title": "Welcome to the Home Page"
}

3️⃣ Run the following command to generate the localization files:

flutter gen-l10n

4️⃣ Replace the hardcoded text in your Dart code with the generated localization reference:

LocalizationHelper.instance.home_title

2. Localizing Strings with Variables 🧩

Definition: A string with variables contains placeholders for dynamic values, such as names, amounts, or dates.

Key Difference:

  • Hardcoded strings are static and do not change.
  • Strings with variables are dynamic and require input during runtime.

🤖 Handling Strings with Variables

Follow these steps for strings with placeholders like {NAME} or {amount}.

Step 1: Define the String in .arb 📂

Add the string to your app_en.arb file using placeholders:

{
"balances_amount": "Amount: ₹{amount}",
"@balances_amount": {
"placeholders": {
"amount": {
"type": "String",
"example": "10"
}
}
}
}

💡 Explanation:

  • The balances_amount key holds the string with a placeholder {amount}.
  • The @balances_amount object defines the placeholder amount, its type (String), and an example value (10).

Step 2: Use the String in Dart Code 💻

Use the generated localization method and provide the variable value:

LocalizationHelper.instance.balances_amount('10')

⚖️ Comparison Between Hardcoded Strings and Strings with Variables

TypeExample in .arbUsage in Dart Code
📌 Hardcoded String"home_title": "Welcome to the Home Page"LocalizationHelper.instance.home_title
🧩 String with Variable"balances_amount": "Amount: ₹{amount)"LocalizationHelper.instance.balances_amount('10')

🎯 Complete Example

📌 Hardcoded String

{
"welcome_message": "Welcome to our app!"
}

Usage in Dart:

LocalizationHelper.instance.welcome_message

🧩 String with Variables

{
"greeting_name": "Hello, {name}!",
"@greeting_name": {
"placeholders": {
"name": {
"type": "String",
"example": "John"
}
}
}
}

Usage in Dart:

LocalizationHelper.instance.greeting_name('John')

📋 Notes

  • Use 📌 hardcoded strings for fixed text and 🧩 strings with variables for dynamic content.
  • Clearly define placeholders in .arb files for strings with variables.
  • Always test your localization by running the flutter gen-l10n command.

With these steps, you can effectively manage both static and dynamic text in your Flutter project! 🌟