parse static method
Parse a YAML string. Throws FormatException on missing
source_locale, malformed target_locales, or non-map root.
Implementation
static DialectConfig parse(String content) {
final root = yaml.loadYaml(content);
if (root is! yaml.YamlMap) {
throw const FormatException(
'dialect.yaml must be a YAML map at the top level.',
);
}
final sourceLocale = root['source_locale'];
if (sourceLocale is! String || sourceLocale.isEmpty) {
throw const FormatException(
'dialect.yaml is missing a `source_locale:` field (e.g. `en`).',
);
}
final targetLocales = <String>[];
final raw = root['target_locales'];
if (raw == null) {
// Allow missing — equivalent to empty list (fresh `dialect init`).
} else if (raw is yaml.YamlList) {
for (final v in raw) {
if (v is! String) {
throw FormatException(
'target_locales must be a list of locale strings; '
'got ${v.runtimeType}.',
);
}
targetLocales.add(v);
}
} else {
throw FormatException(
'target_locales must be a list, got ${raw.runtimeType}.',
);
}
final platforms = <String, PlatformConfig>{};
final rawPlatforms = root['platforms'];
if (rawPlatforms != null) {
if (rawPlatforms is! yaml.YamlMap) {
throw FormatException(
'platforms must be a map, got ${rawPlatforms.runtimeType}.',
);
}
for (final entry in rawPlatforms.entries) {
final platformName = entry.key;
if (platformName is! String) continue;
final platformValue = entry.value;
if (platformValue is! yaml.YamlMap) {
throw FormatException(
'platforms.$platformName must be a map, got '
'${platformValue.runtimeType}.',
);
}
platforms[platformName] = PlatformConfig._fromYaml(
platformName,
platformValue,
);
}
}
final extras = <String, Object?>{};
for (final entry in root.entries) {
final k = entry.key;
if (k is! String) continue;
if (k == 'source_locale' || k == 'target_locales' || k == 'platforms') {
continue;
}
extras[k] = _unwrap(entry.value);
}
return DialectConfig(
sourceLocale: sourceLocale,
targetLocales: targetLocales,
platforms: platforms,
extras: extras,
);
}