Article image Internationalization and Localization in Flutter: Currency Format

10.6. Internationalization and Localization in Flutter: Currency Format

Page 139 | Listen in audio

Internationalization and localization are crucial aspects of application development that are often overlooked. However, they are vital to reaching a global audience. In this context, Flutter and Dart offer a series of tools and resources to facilitate this process. One of the most important aspects of localization is the currency format. In this chapter, we'll explore how we can implement internationalization and localization in Flutter, with a focus on the currency format.

To begin with, we need to understand what internationalization and localization are. Internationalization, or i18n, is the process of developing software products that can be easily adapted for multiple languages ​​and regions without engineering modifications. On the other hand, localization, or l10n, is the process of adapting an internationalized product for a specific region or language.

In Flutter, internationalization is facilitated by the `WidgetsApp` class, which uses the device's system language to determine localization settings. To add support for additional languages, you can extend the `LocalizationsDelegate` class.

When it comes to formatting currencies, Dart's `intl` package is extremely useful. This package provides many functions to format numbers, dates and currencies. To use the `intl` package, you need to add it to your `pubspec.yaml` file and import it into your Dart file.

dependencies:
  flutter:
    sdk: flutter
  intl: ^0.17.0

After importing the `intl` package, you can use the `NumberFormat` class to format currencies. The `NumberFormat` class has a `currency` method that can be used to format numbers as currencies. The `currency` method accepts three arguments: `locale`, `name` and `symbol`. The `locale` argument is a string representing the locale of the language you want to use to format the currency. The `name` argument is a string representing the name of the currency you want to format. Finally, the `symbol` argument is a string representing the currency symbol you want to use.

var format = NumberFormat.currency(locale: 'en_US', name: 'USD', symbol: '\$');
print(format.format(123456789.987654321)); // Output: \$123,456,789.99

As you can see in the example above, the `currency` method formats the number as a US dollar currency, including the dollar sign and two decimal places.

It is important to note that the `intl` package supports many different languages ​​and currencies. To see the complete list of supported languages ​​and currencies, you can refer to the official Dart documentation.

Furthermore, Flutter also supports finding text in your app. To localize text in your application, you can use the `Intl` class from the `intl` package. The `Intl` class has a `message` method that can be used to define messages that can be translated into different languages. To use the `message` method, you need to define a message map for each language your application supports. Each message map is a string-to-string map, where the key is the original English message and the value is the translation of the message into the corresponding language.

var messages = {
  'en': {
    'hello': 'Hello',
  },
  'is': {
    'hello': 'Hello',
  },
};

print(Intl.message(
  'hello',
  name: 'hello',
  locale: 'es',
  defaultMessage: 'Hello',
  messages: messages,
)); // Output: Hello

As you can see in the example above, the `message` method translates the message "hello" into Spanish, because the `locale` is set to 'es'.

In summary, internationalization and localization are important aspects of app development that can help you reach a global audience. Flutter and Dart make it easy to implement internationalization and localization, especially when it comes to formatting currencies. With the `intl` package, you can easily format numbers as currencies in many different languages ​​and currencies. In addition, you can also localize text in your application using the `Intl` class.

Now answer the exercise about the content:

What is the difference between internationalization and localization in the context of application development?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Internationalization and localization in Flutter: Measure format

Next page of the Free Ebook:

140Internationalization and localization in Flutter: Measure format

3 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text