You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
code() is a short identifier that is used to represent a locale. For the above example, its the "en_us".
Example
...
@overrideStringcode() =>'en_us';
...
ordinals(): Returns a list of ordinal suffixes for this locale.
ordinals() is a suffix that is used to indicate the order of a number. For example, the ordinal for the number 1 is "st", the ordinal for the number 2 is "nd", and the ordinal for the number 3 is "rd"
startOfWeek(): Returns the day on which the week starts for this locale.
startOfWeek() returns the day of the week that is considered to be the first day of the week. In the United States, the start of week is Sunday. In many other countries, the start of week could be Sunday or Monday.
relativeDateTime(): Returns a RelativeDateTime instance for this locale.
RelativeDateTime is a class in the Jiffy Dart library that encapsulates the rules for formatting relative date/time values (For example, "3 hours ago", "in 2 days") for a specific locale. It is used to format relative time values in a way that is consistent with the conventions of a particular language or culture.
Create another new class that extends the RelativeDateTime
Still using the example, the new class should names as EnUsRelativeTime.dart
This new class should be added in the same file as EnUsLocale.dart
Step 2: Add the new locale to the _supportedLocales map.
The _supportedLocales map is a private variable in the Jiffy Dart library that contains a list of all the supported locales. The keys of the map are the locale codes, and the values are the Locale instances for those locales.
Once done, the new locale can now be used as follows
awaitJiffy.setLocale('en_us');
Jiffy.parse('1997/09/23')
.startOf(Unit.year)
.yMMMMEEEEd; // Wednesday, January 1, 1997Jiffy.parse('1997/09/23').from(Jiffy.parse('2002/10/26')); // 5 years ago
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
The following steps will guide you on how to add a new locale into Jiffy, using the
en_us
locale as an example.Step 1: Create a new class that extends the
Locale
class.Continuing with the same example, we will name the new class as
EnUsLocale.dart
.The next step is to add this class to the lib/src/locale/locales folder.
Override the following methods
code()
: Returns the locale code for this locale.code()
is a short identifier that is used to represent a locale. For the above example, its the "en_us".Example
ordinals()
: Returns a list of ordinal suffixes for this locale.ordinals()
is a suffix that is used to indicate the order of a number. For example, the ordinal for the number 1 is "st", the ordinal for the number 2 is "nd", and the ordinal for the number 3 is "rd"Example
This may differ to different locales, like
ja
it will bestartOfWeek()
: Returns the day on which the week starts for this locale.startOfWeek()
returns the day of the week that is considered to be the first day of the week. In the United States, the start of week is Sunday. In many other countries, the start of week could be Sunday or Monday.Example
relativeDateTime()
: Returns aRelativeDateTime
instance for this locale.RelativeDateTime
is a class in the Jiffy Dart library that encapsulates the rules for formatting relative date/time values (For example, "3 hours ago", "in 2 days") for a specific locale. It is used to format relative time values in a way that is consistent with the conventions of a particular language or culture.Create another new class that extends the
RelativeDateTime
Still using the example, the new class should names as
EnUsRelativeTime.dart
This new class should be added in the same file as
EnUsLocale.dart
And now override the
relativeDateTime()
like soAn now the new
EnUsLocale.dart
locale file will look like thisStep 2: Add the new locale to the
_supportedLocales
map.The _supportedLocales map is a private variable in the Jiffy Dart library that contains a list of all the supported locales. The keys of the map are the locale codes, and the values are the Locale instances for those locales.
Once done, the new locale can now be used as follows
Step 3: Add tests
Don't forget to add tests here locales_test.dart
and here supported_locales_test.dart
Step 4: Analyse, format dart code and run tests
There are scripts to help you with this in the
./bin
foldersIf in Windows run the following in the terminal
.\bin\dart_analyze.bat
to analyze dart code.\bin\dart_format.bat
to format dart code.\bin\dart_test.bat
to run test and make sure they passIf in Linux or Mac
.\bin\dart_analyze.sh
to analyze dart code.\bin\dart_format.sh
to format dart code.\bin\dart_test.sh
to run test and make sure they passStep 5: Mention the newly added locale in the documentation
And finally mention the new locale in the documentation here Supported Locales in Jiffy
Beta Was this translation helpful? Give feedback.
All reactions