From af23f53d8295affdc54cd3c7fc435e2d1c6bb205 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sat, 13 Jul 2019 19:52:04 +0200 Subject: [PATCH] Simplify / reorganize i18n management for report and description --- data/hooks/diagnosis/10-ip.py | 1 - .../diagnosis/{12-dns.py => 12-dnsrecords.py} | 7 ++- locales/en.json | 4 +- src/yunohost/diagnosis.py | 44 +++++++++++++------ 4 files changed, 36 insertions(+), 20 deletions(-) rename data/hooks/diagnosis/{12-dns.py => 12-dnsrecords.py} (94%) diff --git a/data/hooks/diagnosis/10-ip.py b/data/hooks/diagnosis/10-ip.py index 19e4806f6..665c0ff0d 100644 --- a/data/hooks/diagnosis/10-ip.py +++ b/data/hooks/diagnosis/10-ip.py @@ -10,7 +10,6 @@ from yunohost.diagnosis import Diagnoser class IPDiagnoser(Diagnoser): id_ = os.path.splitext(os.path.basename(__file__))[0].split("-")[1] - description = "internet_connectivity" cache_duration = 60 def validate_args(self, args): diff --git a/data/hooks/diagnosis/12-dns.py b/data/hooks/diagnosis/12-dnsrecords.py similarity index 94% rename from data/hooks/diagnosis/12-dns.py rename to data/hooks/diagnosis/12-dnsrecords.py index 09f8cd4bf..5edfc2d41 100644 --- a/data/hooks/diagnosis/12-dns.py +++ b/data/hooks/diagnosis/12-dnsrecords.py @@ -9,10 +9,9 @@ from moulinette.utils.filesystem import read_file from yunohost.diagnosis import Diagnoser from yunohost.domain import domain_list, _build_dns_conf, _get_maindomain -class DNSDiagnoser(Diagnoser): +class DNSRecordsDiagnoser(Diagnoser): id_ = os.path.splitext(os.path.basename(__file__))[0].split("-")[1] - description = "dns_configurations" cache_duration = 3600*24 def validate_args(self, args): @@ -34,7 +33,7 @@ class DNSDiagnoser(Diagnoser): main_domain = _get_maindomain() for domain in self.args["domains"]: - self.logger_info("Diagnosing DNS conf for %s" % domain) + self.logger_debug("Diagnosing DNS conf for %s" % domain) for report in self.check_domain(domain, domain==main_domain): yield report @@ -92,5 +91,5 @@ class DNSDiagnoser(Diagnoser): def main(args, env, loggers): - return DNSDiagnoser(args, env, loggers).diagnose() + return DNSRecordsDiagnoser(args, env, loggers).diagnose() diff --git a/locales/en.json b/locales/en.json index 0245bf49d..0bb6d7275 100644 --- a/locales/en.json +++ b/locales/en.json @@ -163,7 +163,8 @@ "diagnosis_dns_bad_conf": "Bad DNS configuration for {domain} : {category}.", "diagnosis_dns_missing_record": "According to the recommended DNS configuration, you should add a DNS record with type {0}, name {1} and value {2}", "diagnosis_dns_discrepancy": "According to the recommended DNS configuration, the value for the DNS record with type {0} and name {1} should be {2}, not {3}.", - "dns_configurations": "Domain name configuration (DNS)", + "diagnosis_description_ip": "Internet connectivity", + "diagnosis_description_dnsrecords": "DNS records", "domain_cannot_remove_main": "Cannot remove main domain. Set a new main domain first", "domain_cert_gen_failed": "Could not generate certificate", "domain_created": "Domain created", @@ -245,7 +246,6 @@ "hook_name_unknown": "Unknown hook name '{name:s}'", "installation_complete": "Installation complete", "installation_failed": "Something went wrong with the installation", - "internet_connectivity": "Internet connectivity", "ip6tables_unavailable": "You cannot play with ip6tables here. You are either in a container or your kernel does not support it", "iptables_unavailable": "You cannot play with iptables here. You are either in a container or your kernel does not support it", "log_corrupted_md_file": "The YAML metadata file associated with logs is damaged: '{md_file}\nError: {error}'", diff --git a/src/yunohost/diagnosis.py b/src/yunohost/diagnosis.py index 38c59793f..fb5220679 100644 --- a/src/yunohost/diagnosis.py +++ b/src/yunohost/diagnosis.py @@ -64,18 +64,6 @@ def diagnosis_show(categories=[], full=False): except Exception as e: logger.error("Failed to fetch diagnosis result for category '%s' : %s" % (category, str(e))) # FIXME : i18n - # "Render" the strings with m18n.n - for report in all_reports: - - report["description"] = m18n.n(report["description"]) - - for r in report["reports"]: - type_, message_key, message_args = r["report"] - r["report"] = (type_, m18n.n(message_key, **message_args)) - - if "details" in r: - r["details"] = [ m18n.n(key, *values) for key, values in r["details"] ] - return {"reports": all_reports} def diagnosis_run(categories=[], force=False, args=None): @@ -130,6 +118,13 @@ class Diagnoser(): self.args.update(self.validate_args(self.args)) self.cache_file = Diagnoser.cache_file(self.id_) + descr_key = "diagnosis_description_" + self.id_ + self.description = m18n.n(descr_key) + # If no description available, fallback to id + if self.description == descr_key: + self.description = report["id"] + + def cached_time_ago(self): if not os.path.exists(self.cache_file): @@ -145,12 +140,12 @@ class Diagnoser(): if not self.args.get("force", False) and self.cached_time_ago() < self.cache_duration: self.logger_debug("Cache still valid : %s" % self.cache_file) + # FIXME uhoh that's not consistent with the other return later return self.logger_debug("Running diagnostic for %s" % self.id_) new_report = { "id": self.id_, - "description": self.description, "cached_for": self.cache_duration, "reports": list(self.run()) } @@ -158,6 +153,7 @@ class Diagnoser(): # TODO / FIXME : should handle the case where we only did a partial diagnosis self.logger_debug("Updating cache %s" % self.cache_file) self.write_cache(new_report) + Diagnoser.i18n(new_report) return 0, new_report @@ -170,8 +166,30 @@ class Diagnoser(): filename = Diagnoser.cache_file(id_) report = read_json(filename) report["timestamp"] = int(os.path.getmtime(filename)) + Diagnoser.i18n(report) return report + @staticmethod + def i18n(report): + + # "Render" the strings with m18n.n + # N.B. : we do those m18n.n right now instead of saving the already-translated report + # because we can't be sure we'll redisplay the infos with the same locale as it + # was generated ... e.g. if the diagnosing happened inside a cron job with locale EN + # instead of FR used by the actual admin... + + descr_key = "diagnosis_description_" + report["id"] + report["description"] = m18n.n(descr_key) + # If no description available, fallback to id + if report["description"] == descr_key: + report["description"] = report["id"] + + for r in report["reports"]: + type_, message_key, message_args = r["report"] + r["report"] = (type_, m18n.n(message_key, **message_args)) + + if "details" in r: + r["details"] = [ m18n.n(key, *values) for key, values in r["details"] ] def _list_diagnosis_categories():