Now, it's time to generate a set of Dart libraries that contain translated versions of our messages—one per locale from the ARB files prepared before. We use the generate_from_arb
program from the intl
package:
pub run intl:generate_from_arb --output-dir=web web/registration_form.dart web/translate_en.arb web/translate_de.arb
The program generates the message_de.dart
, message_en.dart
, and messages_all.dart
files in the specified web directory. Each message_<locale_tag>.dart
file contains the MessageLookup
class that implements MessageLookupByLibrary
. The MessageLookup
class has a getter method localeName
, a set of static functions that are returned translated on the specific locale text messages, and final constant messages
that contain the name of all the static methods. The messages_all.dart
file combines all the lookups in one place to make them available for the localization code from the Intl
library. The single available public method of the message_all
library is initializeMessages
, as shown in the following code:
Future initializeMessages(String localeName)
This method should be called first before using the specified localeName
method. Let's change our code to make the German locale available by default. All we need to do is import the messages_all.dart
file to our project and add initializeMessages
in the main
method, as shown in the following code:
import 'messages_all.dart'; … void main() { initializeMessages('de').then((_) { Intl.defaultLocale = 'de'; querySelector("#formHead").text = formHead(); querySelector("#firstNameLbl").text = firstNameLbl(); querySelector("#lastNameLbl").text = lastNameLbl(); querySelector("#genderLbl").text = genderLbl(); querySelector("#maleLbl").text = maleLbl(); querySelector("#femaleLbl").text = femaleLbl(); (querySelector("#registerBtn") as InputElement) .value = registerBtn(); BidiFormatter bidiFormatter = new BidiFormatter.UNKNOWN(); querySelector("#copyrightLbl").text = bidiFormatter.wrapWithUnicode(copyrightLbl()); }); }
In the preceding code, we specified the German locale when we initialized the messages as default. Now, open the index.html
file in the browser to see the correct translation of our form in the German locale:
3.149.249.174