From f765d9d0f99b11c632f14f5c73effc284eaa9db4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Imobach=20Gonz=C3=A1lez=20Sosa?= Date: Tue, 14 May 2024 13:35:48 +0100 Subject: [PATCH] rust: read the list of locales from /etc/agama.d --- rust/agama-server/src/l10n/locale.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/rust/agama-server/src/l10n/locale.rs b/rust/agama-server/src/l10n/locale.rs index 4f38d30abe..98a013c701 100644 --- a/rust/agama-server/src/l10n/locale.rs +++ b/rust/agama-server/src/l10n/locale.rs @@ -5,7 +5,7 @@ use agama_locale_data::{InvalidLocaleCode, LocaleId}; use anyhow::Context; use serde::Serialize; use serde_with::{serde_as, DisplayFromStr}; -use std::process::Command; +use std::{fs, process::Command}; /// Represents a locale, including the localized language and territory. #[serde_as] @@ -37,15 +37,12 @@ impl LocalesDatabase { /// Loads the list of locales. /// + /// It checks for a file in /etc/agama.d/locales containing the list of supported locales (one per line). + /// It it does not exists, calls `localectl list-locales`. + /// /// * `ui_language`: language to translate the descriptions (e.g., "en"). pub fn read(&mut self, ui_language: &str) -> Result<(), Error> { - let result = Command::new("localectl") - .args(["list-locales"]) - .output() - .context("Failed to get the list of locales")?; - let output = - String::from_utf8(result.stdout).context("Invalid UTF-8 sequence from list-locales")?; - self.known_locales = output + self.known_locales = Self::get_locales_list()? .lines() .filter_map(|line| TryInto::::try_into(line).ok()) .collect(); @@ -110,6 +107,20 @@ impl LocalesDatabase { Ok(result) } + + fn get_locales_list() -> Result { + const LOCALES_LIST_PATH: &str = "/etc/agama.d/locales"; + + if let Ok(locales) = fs::read_to_string(LOCALES_LIST_PATH) { + return Ok(locales); + } + + let result = Command::new("localectl") + .args(["list-locales"]) + .output() + .context("Failed to get the list of locales")?; + Ok(String::from_utf8(result.stdout).context("Invalid UTF-8 sequence from list-locales")?) + } } #[cfg(test)]