text stringlengths 1 474 |
|---|
is called NnMaterialLocalizations, |
and the LocalizationsDelegate subclass is |
_NnMaterialLocalizationsDelegate. |
The value of NnMaterialLocalizations.delegate |
is an instance of the delegate, and is all |
that’s needed by an app that uses these localizations.The delegate class includes basic date and number format |
localizations. All of the other localizations are defined by String |
valued property getters in NnMaterialLocalizations, like this: |
<code_start>@override |
String get moreButtonTooltip => r'More'; |
@override |
String get aboutListTileTitleRaw => r'About $applicationName'; |
@override |
String get alertDialogLabel => r'Alert';<code_end> |
These are the English translations, of course. |
To complete the job you need to change the return |
value of each getter to an appropriate Nynorsk string.The getters return “raw” Dart strings that have an r prefix, |
such as r'About $applicationName', |
because sometimes the strings contain variables with a $ prefix. |
The variables are expanded by parameterized localization methods: |
<code_start>@override |
String get pageRowsInfoTitleRaw => r'$firstRow–$lastRow of $rowCount'; |
@override |
String get pageRowsInfoTitleApproximateRaw => |
r'$firstRow–$lastRow of about $rowCount';<code_end> |
The date patterns and symbols of the locale also need to |
be specified, which are defined in the source code as follows: |
<code_start>const nnLocaleDatePatterns = { |
'd': 'd.', |
'E': 'ccc', |
'EEEE': 'cccc', |
'LLL': 'LLL', |
// ... |
}<code_end> |
<code_start>const nnDateSymbols = { |
'NAME': 'nn', |
'ERAS': <dynamic>[ |
'f.Kr.', |
'e.Kr.', |
], |
// ... |
}<code_end> |
These values need to be modified for the locale to use the correct |
date formatting. Unfortunately, since the intl library doesn’t |
share the same flexibility for number formatting, |
the formatting for an existing locale must be used |
as a substitute in _NnMaterialLocalizationsDelegate: |
<code_start>class _NnMaterialLocalizationsDelegate |
extends LocalizationsDelegate<MaterialLocalizations> { |
const _NnMaterialLocalizationsDelegate(); |
@override |
bool isSupported(Locale locale) => locale.languageCode == 'nn'; |
@override |
Future<MaterialLocalizations> load(Locale locale) async { |
final String localeName = intl.Intl.canonicalizedLocale(locale.toString()); |
// The locale (in this case `nn`) needs to be initialized into the custom |
// date symbols and patterns setup that Flutter uses. |
date_symbol_data_custom.initializeDateFormattingCustom( |
locale: localeName, |
patterns: nnLocaleDatePatterns, |
symbols: intl.DateSymbols.deserializeFromMap(nnDateSymbols), |
); |
return SynchronousFuture<MaterialLocalizations>( |
NnMaterialLocalizations( |
localeName: localeName, |
// The `intl` library's NumberFormat class is generated from CLDR data |
// (see https://github.com/dart-lang/i18n/blob/main/pkgs/intl/lib/number_symbols_data.dart). |
// Unfortunately, there is no way to use a locale that isn't defined in |
// this map and the only way to work around this is to use a listed |
// locale's NumberFormat symbols. So, here we use the number formats |
// for 'en_US' instead. |
decimalFormat: intl.NumberFormat('#,##0.###', 'en_US'), |
twoDigitZeroPaddedFormat: intl.NumberFormat('00', 'en_US'), |
// DateFormat here will use the symbols and patterns provided in the |
// `date_symbol_data_custom.initializeDateFormattingCustom` call above. |
// However, an alternative is to simply use a supported locale's |
// DateFormat symbols, similar to NumberFormat above. |
fullYearFormat: intl.DateFormat('y', localeName), |
compactDateFormat: intl.DateFormat('yMd', localeName), |
shortDateFormat: intl.DateFormat('yMMMd', localeName), |
mediumDateFormat: intl.DateFormat('EEE, MMM d', localeName), |
longDateFormat: intl.DateFormat('EEEE, MMMM d, y', localeName), |
yearMonthFormat: intl.DateFormat('MMMM y', localeName), |
shortMonthDayFormat: intl.DateFormat('MMM d'), |
), |
); |
} |
@override |
bool shouldReload(_NnMaterialLocalizationsDelegate old) => false; |
}<code_end> |
For more information about localization strings, |
check out the flutter_localizations README.Once you’ve implemented your language-specific subclasses of |
GlobalMaterialLocalizations and LocalizationsDelegate, |
you need to add the language and a delegate instance to your app. |
The following code sets the app’s language to Nynorsk and |
adds the NnMaterialLocalizations delegate instance to the app’s |
localizationsDelegates list: |
<code_start>const MaterialApp( |
localizationsDelegates: [ |
GlobalWidgetsLocalizations.delegate, |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.