buildArb function

void buildArb(
  1. Map<String, Map<String, String>> map
)

Creates the {locale}.arb files to be used by intl package or similar. when config.yaml has intl:enabled:true. Takes the map with all the translations to generates the files.

Implementation

void buildArb(Map<String, Map<String, String>> map) {
  trace('Building arb files ... ');
  var appName = '';
  // detect if we have a l10n.yaml file.
  // var arbDir = config.arbOutputDir;

  /// look for base locales.
  var sublist = config.locales.where((element) => element.contains('_'));
  for (var e in sublist) {
    var langCode = e.split('_').first;
    if (!map.containsKey(langCode)) {
      trace('Adding base langCode: $langCode for arb generation');
      map[langCode] = map[e]!;
    }
  }

  for (var localeKey in map.keys) {
    // trace('my locale key: ', localeKey);
    final output = <String, dynamic>{
      '@@last_modified': DateTime.now().toIso8601String(),
      '@@locale': localeKey,
      // special case for countryCode, add base language as well zh-tw
      if (appName.isNotEmpty) 'appName': appName,
    };
    var localeMap = map[localeKey]!;
    final pluralMaps = <String, dynamic>{};
    final selectorMaps = <String, dynamic>{};

    // make arb file.
    for (var k in localeMap.keys) {
      if (k.contains(_kSelectorSearch)) {
        _customModifier(
          key: k,
          value: localeMap[k] as String,
          searchKey: _kSelectorSearch,
          targetMap: selectorMaps,
          errorOnVar: _errorStringInvalidSelector,
          // metaVarType: 'String',
        );
      } else if (k.contains(_kPluralSearch)) {
        _customModifier(
          key: k,
          value: localeMap[k] as String,
          searchKey: _kPluralSearch,
          targetMap: pluralMaps,
          errorOnVar: _errorStringInvalidPlural,
          // metaVarType: 'num',
        );
      } else {
        var newKey = k.camelCase;
        var textValue = localeMap[k]!;
        output[newKey] = textValue;

        /// we are sending the original Key ("k", field name separated by dots)
        /// to make compatible with `varsByKeys` accessible and read keys.
        _addSimpleVarsMetadata(k, newKey, textValue, output);
        // _addMetaKey(newKey, textValue, output, metaFallbackProperties);
      }
    }

    /// add pluralKeys
    for (var k in pluralMaps.keys) {
      /// add meta keys
      output[k] = _resolvePluralTextFromMap(pluralMaps[k]);
      _addMetaKey(k, output[k], output, metaProperties);
      _addMetaKey(k, output[k], output, metaFallbackProperties);
    }

    /// add selector keys
    for (var k in selectorMaps.keys) {
      output[k] = _resolveSelectorTextFromMap(selectorMaps[k]);
      _addMetaKey(k, output[k], output, metaProperties);
      _addMetaKey(k, output[k], output, metaFallbackProperties);
    }
    var jsonString = prettyJson(output);
    var outputPath = config.getArbFilePath(localeKey);
    saveString(outputPath, jsonString);
  }
  trace('arb files generated');
  // runPubGet();
}