extract_messages library
This is for use in extracting messages from a Dart program
using the Intl.message() mechanism and writing them to a file for
translation. This provides only the stub of a mechanism, because it
doesn't define how the file should be written. It provides an
IntlMessage
class that holds the extracted data and parseString
and parseFile
methods which
can extract messages that conform to the expected pattern:
(parameters) => Intl.message("Message $parameters", desc: ...);
It uses the analyzer package to do the parsing, so may
break if there are changes to the API that it provides.
An example can be found in test/message_extraction/extract_to_json.dart
Note that this does not understand how to follow part directives, so it has to explicitly be given all the files that it needs. A typical use case is to run it on all .dart files in a directory.
Classes
- InterpolationVisitor
- Given an interpolation, find all of its chunks, validate that they are only simple variable substitutions or else Intl.plural/gender calls, and keep track of the pieces of text so that other parts of the program can deal with the simple string sections and the generated parts separately. Note that this is a SimpleAstVisitor, so it only traverses one level of children rather than automatically recursing. If we find a plural or gender, which requires recursion, we do it with a separate special-purpose visitor.
- MessageExtraction
- A particular message extraction run.
- MessageFindingVisitor
- This visits the program source nodes looking for Intl.message uses that conform to its pattern and then creating the corresponding IntlMessage objects. We have to find both the enclosing function, and the Intl.message invocation.
- PluralAndGenderVisitor
- A visitor to extract information from Intl.plural/gender sends. Note that this is a SimpleAstVisitor, so it doesn't automatically recurse. So this needs to be called where we expect a plural or gender immediately below.
Functions
-
addArgumentFor(
MainMessage message, String arg, Map result) → void -
arbMetadata(
MainMessage message) → Map -
computeMessageName(
String? name, String text, String? meaning) → String - If a message is a string literal without interpolation, compute a name based on that and the meaning, if present.
-
escape(
String s) → String -
icuForm(
MainMessage message) → String - Return a version of the message string with with ICU parameters "{variable}" rather than Dart interpolations "$variable".
-
leaveTheInterpolationsInDartForm(
MainMessage msg, dynamic chunk) → String - This is a placeholder for transforming a parameter substitution from the translation file format into a Dart interpolation. In our case we store it to the file in Dart interpolation syntax, so the transformation is trivial.
-
toARB(
MainMessage message, {bool supressMetadata = false, bool includeSourceText = false}) → Map - Convert the MainMessage to a trivial JSON format.
-
turnInterpolationIntoICUForm(
Message message, dynamic chunk, {bool shouldEscapeICU = false}) → String