Localization in Dart

 Localization in Dart


Localization is an important aspect of app development, especially for apps that target a global audience. Flutter provides built-in support for localizing your app using the intl package.


import 'package:flutter/material.dart';

import 'package:intl/intl.dart';


void main() {

  runApp(MyApp());

}


class MyApp extends StatelessWidget {

  @override

  Widget build(BuildContext context) {

    return MaterialApp(

      localizationsDelegates: [

        GlobalMaterialLocalizations.delegate,

        GlobalWidgetsLocalizations.delegate,

        GlobalCupertinoLocalizations.delegate,

      ],

      supportedLocales: [

        Locale('en', 'US'),

        Locale('fr', 'FR'),

      ],

      title: 'My App',

      home: Scaffold(

        appBar: AppBar(

          title: Text('My App'),

        ),

        body: Center(

          child: Text(

            DateFormat.yMMMMd().format(DateTime.now()),

          ),

        ),

      ),

    );

  }

}

In the above example, we are using the intl package to format a DateTime object as a string using the DateFormat class. We have also specified the supported locales for our app, and Flutter will automatically select the appropriate localization based on the user's device settings.

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...