Search code examples
flutterflutter-localizationsflutter-internationalization

Add separate .arb file in Flutter for formatting


in my project i use the flutter_localizations plugin for internationalizing. So i have different .arb files for each language (e.g. app_en.arb, app_de.arb and so on).

I also use the arb files for formatting e.g. this:

"date_format_year_month_day": "{date}",
    "@date_format_year_month_day": {
        "description": "Do not translate",
        "placeholders": {
            "date": {
                "type": "DateTime",
                "format": "yMMMMd",
                "example": "July 10, 1996"
            }
        }
    },

All the formatting entries are currently in the english arb file, because there are no differences between the languages. But i don't like this, because it's bad organized. Is it possible to have a separate .arb file just for formatting stuff? E.g. "app_formatting.arb"


Solution

  • You can create an extension for DateTime and use it to convert a date into the desired string format:

    extension DateTimeExt on DateTime {
      String get formatYearMonthDay {
        return DateFormat('yMMMMd').format(this);
      }
    }
    

    and then use it in text:

        final date = DateTime.now();
        final widget = Text(date.formatYearMonthDay);