🌐 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_amountkey holds the string with a placeholder{amount}. - The
@balances_amountobject defines the placeholderamount, 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
| Type | Example in .arb | Usage 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