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
    ),
})

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,
}) async {
  String script = "";

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

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

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

      if (is_translate) {
        for (var i = 0; i < languageCodeNames.length; i++) {
          String language_code_new = languageCodeNames[i];
          if (language_raw_data.containsKey(language_code_new)) {
            continue;
          }
          try {
            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 += """
    /// default return
    /// ```dart
    /// ${json.encode(value)};
    /// ```
    String ${key_name}_${key}({
      String? languageCode,
      List<Map<String, String>>? replacesDatas,
    }) {
      Map language_raw_data = ${json.encode(language_raw_data)};

      return sendLanguage(
        ${language_parameter}: ${base_name.camelCaseClass()}CodeData(
          language_raw_data,
        ),
        id: ${json.encode("${key_name}_${key}")},
        replacesDatas: replacesDatas,
        languageCode: languageCode,
      );

    }""";

      script += script_new;
    }

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

      String script_new = "";
      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++) {
          String language_code_new = languageCodeNames[i];
          if (language_raw_data.containsKey(language_code_new)) {
            continue;
          }
          try {
            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 += """
    /// default return
    /// ```dart
    /// ${json.encode(value[default_language_code_id])};
    /// ```
    String ${key_name}_${key}({
      String? languageCode,
      List<Map<String, String>>? replacesDatas,
    }) {
      Map language_raw_data = ${json.encode(language_raw_data)};

      return sendLanguage(
        ${language_parameter}: ${base_name.camelCaseClass()}CodeData(
          language_raw_data,
        ),
        id: ${json.encode("${key_name}_${key}")},
        replacesDatas: replacesDatas,
        languageCode: languageCode,
      );

    }""";

      script += script_new;
    }
  }
  return script;
}