languageMapToStringScript function

Future<String> languageMapToStringScript({
  1. required String key_name,
  2. required Map data,
  3. required String language_parameter,
  4. bool is_translate = true,
  5. required String default_language_code_id,
  6. required String base_name,
  7. required Future<String?> onData(
    1. String origin_data,
    2. String language_code_id,
    3. String default_language_code_id
    ),
  8. required FutureOr<void> onLanguageData(
    1. String languageData
    ),
})

Implementation

Future<String> languageMapToStringScript({
  required String key_name,
  required Map data,
  required String language_parameter,
  bool is_translate = true,
  required String default_language_code_id,
  required String base_name,
  required Future<String?> Function(String origin_data, String language_code_id,
          String default_language_code_id)
      onData,
  required FutureOr<void> Function(String languageData) onLanguageData,
}) async {
  String script = "";

  for (var element in data.entries) {
    final String key = element.key;

    if (element.value is String) {
      final String value = element.value;

      String script_new = "";
      final Map language_raw_data = {
        default_language_code_id: value,
      };

      if (is_translate) {
        for (var i = 0; i < languageCodeNames.length; i++) {
          final String language_code_new = languageCodeNames[i];
          if (language_raw_data.containsKey(language_code_new)) {
            continue;
          }
          try {
            final String? result_translate = await onData(
                value, language_code_new, default_language_code_id);
            if (result_translate != value && result_translate != null) {
              if (result_translate.isEmpty) {
                continue;
              }
              language_raw_data[language_code_new] = result_translate;
            }
          } catch (e) {}
        }
      }
      script_new += "\n";
      onLanguageData(
          "final Map _${key_name}_${key}_data = ${json.encode(language_raw_data)};");
      script_new += """
    /// default return
    /// ```dart
    /// \"\"\"
    ${value.toString().split("\n").map((e) => "    /// ${e}").join("\n").trim()}
    /// \"\"\";
    /// ```
    String ${key_name}_${key}({
      String? languageCode,
      List<RegExpReplace>? regexpReplaces,
    }) {


      return sendLanguage(
        // ${language_parameter}: ${base_name.camelCaseClass()}CodeData(
        languageCodeData: LanguageCodeData(
          _${key_name}_${key}_data,
        ),
        id: ${json.encode("${key_name}_${key}")},
        regexpReplaces: regexpReplaces,
        languageCode: languageCode,
      );

    }""";

      script += script_new;
    }

    if (element.value is Map) {
      final Map value = element.value;

      String script_new = "";
      final Map language_raw_data = {
        default_language_code_id: value[default_language_code_id],
      };
      for (var element in value.entries) {
        if (element.key is String && element.value is String) {
          if (language_raw_data.containsKey(element.key)) {
            continue;
          }
          if (languageCodeNames.contains(element.key)) {
            if (element.value.toString().isNotEmpty) {
              language_raw_data[element.key] = element.value;
            }
          }
        }
      }

      if (is_translate) {
        for (var i = 0; i < languageCodeNames.length; i++) {
          final String language_code_new = languageCodeNames[i];
          if (language_raw_data.containsKey(language_code_new)) {
            continue;
          }
          try {
            final String? result_translate = await onData(
                value[default_language_code_id],
                language_code_new,
                default_language_code_id);
            if (result_translate != value[default_language_code_id] &&
                result_translate != null) {
              if (result_translate.isEmpty) {
                continue;
              }
              language_raw_data[language_code_new] = result_translate;
            }
          } catch (e) {}
        }
      }

      script_new += "\n";
      onLanguageData(
          "final Map _${key_name}_${key}_data = ${json.encode(language_raw_data)};");

      script_new += """
    /// default return
    /// ```dart
    /// \"\"\"
    ${value[default_language_code_id].toString().split("\n").map((e) => "    /// ${e}").join("\n").trim()}
    /// \"\"\";
    /// ```
    String ${key_name}_${key}({
      String? languageCode,
      List<RegExpReplace>? regexpReplaces,
    }) {

      return sendLanguage(
        languageCodeData: LanguageCodeData(
          _${key_name}_${key}_data,
        ),
        id: ${json.encode("${key_name}_${key}")},
        regexpReplaces: regexpReplaces,
        languageCode: languageCode,
      );

    }""";

      script += script_new;
    }
  }
  return script;
}