jsonToLanguageScript function

Future<Directory> jsonToLanguageScript({
  1. required Map language_raw,
  2. required String default_language_code_id,
  3. required Directory directory,
  4. required Future<String?> onData(
    1. String origin_data,
    2. String language_code_id,
    3. String default_language_code_id
    ),
  5. bool is_translate = true,
})

Implementation

Future<Directory> jsonToLanguageScript({
  required Map language_raw,
  required String default_language_code_id,
  required Directory directory,
  required Future<String?> Function(String origin_data, String language_code_id,
          String default_language_code_id)
      onData,
  bool is_translate = true,
}) async {
  if (!directory.existsSync()) {
    await directory.create(recursive: true);
  }

  String base_name = path.basename(directory.path);

  List<Map> langugage_data = [];
  String language_parameter =
      "${base_name.camelCaseClass()}CodeData".toLowerCaseFirstData();

  for (var element in language_raw.entries) {
    String key = element.key;
    if (element.value is Map == false) {
      continue;
    }
    Map value = element.value;
    if (value["is_skip"] == true) {
      continue;
    }
    value.remove("is_skip");

    String name_extension =
        "${key.toString().camelCaseClass()}Extension${base_name.camelCaseClass()}";
    String script = """
// ignore_for_file: non_constant_identifier_names

import "${base_name}_core.dart";
import "${base_name}_code_data.dart";

extension ${name_extension} on ${base_name.camelCaseClass()} {

""";
    String script_method = await languageMapToStringScript(
      key_name: key,
      data: value,
      language_parameter: language_parameter,
      default_language_code_id: default_language_code_id,
      is_translate: is_translate,
      base_name: base_name,
      onData: onData,
    );

    script += "\n${script_method}\n";

    script += "\n}";
    Map jsonData = {
      "name": key,
      "script": script,
    };

    langugage_data.add(jsonData);
  }
  langugage_data.add({
    "name": "${base_name}_scheme",
    "script": langugage_data
        .map((e) => "export ${json.encode("${e["name"]}.dart")};")
        .toSet()
        .toList()
        .join("\n"),
  });

  langugage_data.add({
    "name": "${base_name}_core",
    "script": """
// ignore_for_file: non_constant_identifier_names, empty_catches, unnecessary_brace_in_string_interps
//
import '${base_name}_code_data.dart';

class ${base_name.camelCaseClass()} {
  String language_code = "id";

  List<Map<String, String>> replace_data = [];
  ${base_name.camelCaseClass()}({
    String languageCode = "id",
    required List<Map<String, String>> replaceData,
  }) {
    language_code = languageCode;
    replace_data = replaceData;
  }

  String sendLanguage({
    required ${base_name.camelCaseClass()}CodeData ${language_parameter},
    required String id,
    List<Map<String, String>>? replacesDatas,
    String? languageCode,
  }) {
    languageCode ??= language_code;
    replacesDatas ??= replace_data;
    try {
      String language_result = ${language_parameter}[languageCode] ?? ${language_parameter}["id"] ?? "Translation not found\\n\\n\${id}: Tidak ada bahasa \${languageCode}";
      if (language_result.isEmpty) {
        language_result = "Translation not found\\n\\n\${id}: Tidak ada bahasa \${languageCode}";
      }

      return variableMessage(
        text: language_result,
        replacesDatas: replacesDatas,
      );
    } catch (e) {
      return "LanguageBot error\\nId: \${id}\\nRes: \${e.toString()}";
    }
  }

  String variableMessage({
    required String text,
    List<Map<String, String>>? replacesDatas,
  }) {
    replacesDatas ??= replace_data;
    try {
      String message = "";
      message = text;
      List<Map<String, String>> replaceData = [...replacesDatas];
      for (var i = 0; i < replaceData.length; i++) {
        Map loopData = replaceData[i];
        if (loopData["origin"] != null && loopData["replace"] != null) {
          try {
            message = message.replaceAll(RegExp(loopData["origin"] ?? "", caseSensitive: false), loopData["replace"] ?? "");
          } catch (e) {}
        }
      }

      return message;
    } catch (e) {
      return text;
    }
  }
}

""",
  });

  // print(langugage_data);
  for (var i = 0; i < langugage_data.length; i++) {
    Map language_data_raw = langugage_data[i];

    File file =
        File(path.join(directory.path, "${language_data_raw["name"]}.dart"));
    await file.writeAsString(language_data_raw["script"]);
  }

  Map language_code_json = {
    ...languageCodeJson,
  };

  language_code_json["@type"] = "${base_name.camelCaseClass()}CodeData";
  await jsonToScripts([language_code_json], directory: directory);

  File file = File(path.join(directory.path, "${base_name}.dart"));

  if (file.existsSync()) {
    String contents = """

export ${json.encode("${base_name}_core.dart")};
export ${json.encode("${base_name}_scheme.dart")};

""";
    await file.writeAsString(contents, mode: FileMode.writeOnlyAppend);
  }

  return directory;
}