In Flutter localization (l10n), you can use dynamic keys to handle situations where the translation key needs to be determined at runtime. This can be useful when you have content that is dynamic or depends on user input.

To implement dynamic keys in Flutter localization, you can follow these steps:

  1. Set Up Localization: Set up localization in your Flutter project. You can use the flutter_localizations package to manage translations and support multiple locales. Make sure you have a intl directory containing your .arb (Application Resource Bundle) files with translations for each supported locale.

  2. Define Dynamic Keys: In your .arb files, you can define dynamic keys using placeholders. For example, you can use the %s placeholder to represent a dynamic value in your translation. Here's an example of an English .arb file:

    { "@@locale": "en", "dynamicGreeting": "Hello, %s!" }
  3. Use Dynamic Keys in Dart Code: In your Dart code, you can use the intl package to retrieve the translated strings and replace the dynamic values. The intl package provides the Intl.message() function to handle dynamic keys. Here's an example of how you can use it:

    import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; import 'l10n/messages_all.dart'; class MyLocalizations { static Future<MyLocalizations> load(Locale locale) { final name = (locale.countryCode == null || locale.countryCode.isEmpty) ? locale.languageCode : locale.toString(); final localeName = Intl.canonicalizedLocale(name); return initializeMessages(localeName).then((_) { Intl.defaultLocale = localeName; return MyLocalizations(); }); } static MyLocalizations of(BuildContext context) { return Localizations.of<MyLocalizations>(context, MyLocalizations); } String get dynamicGreeting => Intl.message('Hello, %s!', name: 'dynamicGreeting', desc: 'Greeting with dynamic name', args: ['John']); }
  4. Load the Localizations: In your main.dart or any other entry point of your app, load the localizations using the MaterialApp widget. Make sure to include the supportedLocales and localizationsDelegates parameters:

    void main() async { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( supportedLocales: [ const Locale('en', ''), const Locale('es', ''), // Add more supported locales as needed ], localizationsDelegates: [ MyLocalizations.delegate, GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, // Add more localization delegates as needed ], home: MyHomePage(), ); } }
  5. Use the Dynamic Key in the UI: Finally, you can use the translated dynamic key in your UI by retrieving it from the MyLocalizations class:

    class MyHomePage extends StatelessWidget { @override Widget build(BuildContext context) { final localizations = MyLocalizations.of(context); final dynamicName = 'John'; // You can replace this with your dynamic value return Scaffold( appBar: AppBar(title: Text('Dynamic Greeting')), body: Center( child: Text(localizations.dynamicGreeting.replaceAll('%s', dynamicName)), ), ); } }

In this example, the %s placeholder in the translation is replaced with the dynamic value 'John'. You can replace 'John' with any value you want to display dynamically based on your app's logic.

By following these steps, you can implement dynamic keys in Flutter localization and handle translations with dynamic content.

Have questions or queries?
Get in Touch