Flutter - Change internationalization of date names

Asked

Viewed 7,739 times

4

I am new to Flutter and I would like to know if it is possible to automatically translate the names of the days of the week, months, etc ... encompassing every application?

  • Yes, it is possible. What have you tried so far? Some code?

  • I have tried using: Intl: 0.15.8 and in void main() { Intl.defaultLocale = 'pt_BR'; initializeDateFormatting("pt_BR", null); }, but I get an exception: Locale data has not been initilized

2 answers

8


It is possible yes.

In the pubspec.yaml add the flutter_localizations as:

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter

So in your MaterialApp initial define the supported delegates and languages:

import 'package:flutter_localizations/flutter_localizations.dart';

//...
MaterialApp(
  localizationsDelegates: [
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate
  ],
  supportedLocales: [const Locale('pt', 'BR')],
);

Now an example of full code using a Datepicker from the SDK itself:

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'dart:async';

void main() {
  runApp(
    MaterialApp(
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
      ],
      supportedLocales: [const Locale('pt', 'BR')],
      home: MyApp(),
    ),
  );
}

class MyApp extends StatefulWidget {
  @override
  _State createState() => _State();
}

class _State extends State<MyApp> {
  String _value = '';

  Future _selectDate() async {
    DateTime picked = await showDatePicker(
        context: context,
        initialDate: DateTime.now(),
        firstDate: DateTime(2019),
        lastDate: DateTime(2020));
    if (picked != null) setState(() => _value = picked.toString());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Exemplo localização'),
      ),
      body: Container(
        padding: EdgeInsets.all(32.0),
        child: Center(
          child: Column(
            children: <Widget>[
              Text(_value),
              RaisedButton(
                onPressed: _selectDate,
                child: Text('CLIQUE'),
              )
            ],
          ),
        ),
      ),
    );
  }
}

Before adding en location:

inserir a descrição da imagem aqui

Now after, note that the Strings have been translated/located to ptbr, not only the month but also the buttons.

inserir a descrição da imagem aqui

For more complex internationalization, I recommend reading on documentation.

  • Vlw Julio, thank you!

3

You can use the library lntl to use the Dateformat this way you can format the dates referring to the locality you want.

dependencies:
  flutter:
   sdk: flutter

  flutter_localizations:
   sdk: flutter

  intl: ^0.16.0

To use just import the package

import 'package:intl/intl.dart';

And use the Dateformat class, for example:

DateFormat(DateFormat.YEAR_MONTH_DAY, 'pt_Br').format(DateTime.now());

This would be the way out: Friday, January 17, 2020

Browser other questions tagged

You are not signed in. Login or sign up in order to post.