jsonToLanguageScript function
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() == false) {
await directory.create(recursive: true);
}
final String base_name = path.basename(directory.path);
final List<Map> langugage_data = [];
final String language_parameter =
"${base_name.camelCaseClass()}CodeData".toLowerCaseFirstData();
for (var element in language_raw.entries) {
final String key = element.key;
if (element.value is Map == false) {
continue;
}
final Map value = element.value;
if (value["is_skip"] == true) {
continue;
}
value.remove("is_skip");
final 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";
import "package:general_lib/scheme/language_code_data.dart";
import "package:general_lib/language/language_core.dart";
import "package:general_lib/regexp_replace/regexp_replace.dart";
""";
final 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,
onLanguageData: (languageData) {
script += "\n";
script += languageData.trim();
script += "\n";
},
);
script += """
// extension ${name_extension} on ${base_name.camelCaseClass()} {
extension ${name_extension} on Language {
${script_method}
""";
script += "\n}";
final 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<RegExpReplace>? regexpReplaces,
// 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,
// regexpReplaces: regexpReplaces,
// );
// } catch (e) {
// return "LanguageBot error\\nId: \${id}\\nRes: \${e.toString()}";
// }
// }
// String variableMessage({
// required String text,
// List<RegExpReplace>? regexpReplaces,
// }) {
// 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++) {
final Map language_data_raw = langugage_data[i];
final File file =
File(path.join(directory.path, "${language_data_raw["name"]}.dart"));
await file.writeAsString(language_data_raw["script"]);
}
final Map language_code_json = {
...languageCodeJson,
};
language_code_json["@type"] = "${base_name.camelCaseClass()}CodeData";
// await jsonToScripts(
// [language_code_json],
// directory: directory,
// path_package_json_dart: "import \"package:general_lib/json_scheme/json_scheme.dart\";",
// );
final 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;
}