read method
Future<FileConfig>
read(
- String languageTag, {
- String? fileName,
- TFileReaderFunction? fileReader,
Reads a locale file and registers it with the active TranslationManager. Returns the loaded FileConfig.
fileReader (parameter) overrides the instance-level fileReader
for this single call. If both are null, this throws StateError —
the previous assert-based check was a debug-mode-only guard and
would have produced a confusing NPE in release builds.
Implementation
Future<FileConfig> read(
String languageTag, {
String? fileName,
TFileReaderFunction? fileReader,
}) async {
final effectiveReader = fileReader ?? this.fileReader;
if (effectiveReader == null) {
throw StateError(
'TranslationFileReader.read: no fileReader provided. Pass one to '
'the constructor or to read() directly.',
);
}
if (languageTag.isEmpty && (fileName == null || fileName.isEmpty)) {
throw ArgumentError.value(
languageTag,
'languageTag',
'languageTag must be non-empty when fileName is not provided.',
);
}
// Use posix joining unconditionally. Flutter's `rootBundle` only
// accepts forward-slash paths, and `dart:io` accepts both, so
// posix is the only style that works everywhere without per-host
// normalisation.
final filePath = posix.joinAll([
...translationsDirPath,
fileName ?? '$languageTag.${fileType.extension}',
]);
final fileConfig = FileConfig(
ref: ConfigFileRef(
ref: languageTag,
type: fileType,
read: () => effectiveReader(filePath),
),
settings: const PatternSettings(),
mapper: mapper,
);
return TranslationManager.setConfig(fileConfig);
}