App Translation

Jun's Coding Journey·2023년 9월 9일
0

[Learn] Flutter

목록 보기
15/22

Localizations


localizationDelegates is a list of delegates that produce localized resources for the Flutter app. These delegates are part of the MaterialApp, WidgetsApp, or CupertinoApp widgets. They work in tandem with the Flutter localization system to provide localized content for different languages and regions.

Here's a breakdown of how they work:

Localization Delegates: These are instances of classes that provide localizations for specific libraries. A delegate is responsible for loading and caching localized values while also being aware of the current locale.

Main Players:

  • GlobalMaterialLocalizations.delegate: Provides localized strings and other values for the Material library. This includes translations for the default widget text in Material components like AlertDialog, DatePicker, and more.

  • GlobalWidgetsLocalizations.delegate: Provides localized strings and other values for the basic Flutter widgets library. It mainly deals with text directionality (LTR or RTL).
    GlobalCupertinoLocalizations.delegate: Offers localized strings and other values for the Cupertino library, ensuring iOS-specific components have localized content.

  • Custom Localization: If you're introducing custom text or other resources that need to be localized, you'll likely need a custom delegate. This involves creating your own localization class and delegate to fetch and cache these resources.

MaterialApp(
  title: 'Flutter Demo',
  locale: Locale('en', 'US'),  // Specifies the app's starting locale.
  supportedLocales: [          // List of supported locales
    Locale('en', 'US'),
    Locale('es', 'ES'),
  ],
  localizationDelegates: [
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
    // ... other delegates as needed
  ],
  home: MyHomePage(),
)

 

App Localizations


AppLocalizations is part of the internationalization (i18n) and localization (l10n) support. It provides a way to make your app more accessible to a global audience by translating the text and other locale-specific data into multiple languages. Here's an overview of the concept and how it is implemented:

  • Basics: When you want to support multiple languages, you often have text in your app that needs to be translated. Flutter uses the Intl package to facilitate this.

  • AppLocalizations Class: This is a custom class where you define all the text and other content that you want to make available in multiple languages.

  • Loading Translations: You'll use .arb (Application Resource Bundle) files to store key-value pairs for each piece of localized content. Each language you support would have its own .arb file.

  • Accessing Localized Content: Once set up, you can use AppLocalizations.of(context).yourKey to get the correct translation for the current locale. The exact key you use would depend on how you've set up your translations.

  • Integration with Flutter: Flutter has a Localizations widget that provides locale-specific values to its descendants. You typically wrap your app (or part of it) in this widget to provide the necessary localizations.

MaterialApp(
  localizationsDelegates: [
    AppLocalizations.delegate,
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
  ],
  supportedLocales: [
    const Locale('en', ''),
    const Locale('es', ''),
  ],
  // ... other properties ...
)

 

Flutter Intl


flutter_intl is a package and a set of tooling in Flutter for internationalization (i18n) and localization (l10n) of apps. It's built around the intl package, which provides base functionalities for i18n/l10n, but flutter_intl takes it a step further by simplifying the setup and process of localizing a Flutter application.

// setup
MaterialApp(
  localizationsDelegates: const [
    S.delegate,
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
  ],
  supportedLocales: S.delegate.supportedLocales,
  // ... other properties ...
)
// usage
Text(S.of(context).greeting)

 

Pluralization


Pluralization often refers to the process of displaying different strings based on a numeric value. For example, you may want to display "1 item" versus "2 items" based on the count. The difference between these two strings isn't just appending an "s"; different languages have different rules for plural forms.

{
  "items": "{count, plural, =0{No items} =1{1 item} other{{count} items}}"
}
Text(S.of(context).items(3))  // Displays: 3 items

 

Selection


Select is another way to deal with localization by selecting a specific message based on a variable, similar to how plural works for numbers. However, while plural is specific to numeric values and plural forms of words, select is a general mechanism to choose among alternative translations based on a string value.

{
  "userGenderMessage": "{gender, select, male{He is a user.} female{She is a user.} other{They are a user.}}"
}
Text(S.of(context).userGenderMessage('male'))  // Displays: He is a user.

profile
Greatness From Small Beginnings

0개의 댓글