Skip to content

Commit c358571

Browse files
committed
i18n: conditionally load SELinux locale strings based on feature flag
1 parent 20a5c3a commit c358571

9 files changed

Lines changed: 99 additions & 21 deletions

File tree

.vscode/cSpell.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"vendor/**",
2929
"**/*.svg",
3030
"src/uu/*/locales/*.ftl",
31-
"src/uucore/locales/*.ftl",
31+
"src/uucore/locales/**/*.ftl",
3232
".devcontainer/**",
3333
"util/gnu-patches/**",
3434
"docs/src/release-notes/**",

src/uucore/build.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,21 @@ fn embed_single_utility_locale(
149149
project_root.join(format!("src/uucore/locales/{locale}.ftl"))
150150
})?;
151151

152+
// Conditionally embed SELinux locales when the selinux feature is enabled
153+
#[cfg(feature = "selinux")]
154+
embed_component_locales(
155+
embedded_file,
156+
locales_to_embed,
157+
"uucore/selinux",
158+
|locale| project_root.join(format!("src/uucore/locales/selinux/{locale}.ftl")),
159+
)?;
160+
161+
// Conditionally embed SMACK locales when the smack feature is enabled
162+
#[cfg(feature = "smack")]
163+
embed_component_locales(embedded_file, locales_to_embed, "uucore/smack", |locale| {
164+
project_root.join(format!("src/uucore/locales/smack/{locale}.ftl"))
165+
})?;
166+
152167
Ok(())
153168
}
154169

@@ -199,6 +214,21 @@ fn embed_all_utility_locales(
199214
project_root.join(format!("src/uucore/locales/{locale}.ftl"))
200215
})?;
201216

217+
// Conditionally embed SELinux locales when the selinux feature is enabled
218+
#[cfg(feature = "selinux")]
219+
embed_component_locales(
220+
embedded_file,
221+
locales_to_embed,
222+
"uucore/selinux",
223+
|locale| project_root.join(format!("src/uucore/locales/selinux/{locale}.ftl")),
224+
)?;
225+
226+
// Conditionally embed SMACK locales when the smack feature is enabled
227+
#[cfg(feature = "smack")]
228+
embed_component_locales(embedded_file, locales_to_embed, "uucore/smack", |locale| {
229+
project_root.join(format!("src/uucore/locales/smack/{locale}.ftl"))
230+
})?;
231+
202232
embedded_file.flush()?;
203233
Ok(())
204234
}
@@ -230,6 +260,21 @@ fn embed_static_utility_locales(
230260
Path::new(&manifest_dir).join(format!("locales/{locale}.ftl"))
231261
})?;
232262

263+
// Conditionally embed SELinux locales when the selinux feature is enabled
264+
#[cfg(feature = "selinux")]
265+
embed_component_locales(
266+
embedded_file,
267+
locales_to_embed,
268+
"uucore/selinux",
269+
|locale| Path::new(&manifest_dir).join(format!("locales/selinux/{locale}.ftl")),
270+
)?;
271+
272+
// Conditionally embed SMACK locales when the smack feature is enabled
273+
#[cfg(feature = "smack")]
274+
embed_component_locales(embedded_file, locales_to_embed, "uucore/smack", |locale| {
275+
Path::new(&manifest_dir).join(format!("locales/smack/{locale}.ftl"))
276+
})?;
277+
233278
// Collect and sort for deterministic builds
234279
let mut entries: Vec<_> = std::fs::read_dir(registry_dir)?
235280
.filter_map(Result::ok)

src/uucore/locales/en-US.ftl

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,6 @@ action-creating = creating
3939
action-reading = reading
4040
action-writing = writing
4141
42-
# SELinux error messages
43-
selinux-error-not-enabled = SELinux is not enabled on this system
44-
selinux-error-file-open-failure = failed to open the file: { $error }
45-
selinux-error-context-retrieval-failure = failed to retrieve the security context: { $error }
46-
selinux-error-context-set-failure = failed to set default file creation context to '{ $context }': { $error }
47-
selinux-error-context-conversion-failure = failed to set default file creation context to '{ $context }': { $error }
48-
49-
# SMACK error messages
50-
smack-error-not-enabled = SMACK is not enabled on this system
51-
smack-error-label-retrieval-failure = failed to get security context: { $error }
52-
smack-error-label-set-failure = failed to set default file creation context to '{ $context }': { $error }
53-
smack-error-no-label-set = no security context set
54-
5542
# Safe traversal error messages
5643
safe-traversal-error-path-contains-null = path contains null byte
5744
safe-traversal-error-open-failed = failed to open { $path }: { $source }

src/uucore/locales/fr-FR.ftl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,6 @@ action-creating = création
3939
action-reading = lecture
4040
action-writing = écriture
4141
42-
# Messages d'erreur SELinux
43-
selinux-error-not-enabled = SELinux n'est pas activé sur ce système
44-
selinux-error-file-open-failure = échec de l'ouverture du fichier : { $error }
45-
selinux-error-context-retrieval-failure = échec de la récupération du contexte de sécurité : { $error }
46-
selinux-error-context-set-failure = échec de la définition du contexte de création de fichier par défaut à '{ $context }' : { $error }
47-
selinux-error-context-conversion-failure = échec de la définition du contexte de création de fichier par défaut à '{ $context }' : { $error }
48-
4942
# Messages d'erreur de traversée sécurisée
5043
safe-traversal-error-path-contains-null = le chemin contient un octet null
5144
safe-traversal-error-open-failed = échec de l'ouverture de { $path } : { $source }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SELinux error messages
2+
# These strings are only loaded when the selinux feature is enabled
3+
4+
selinux-error-not-enabled = SELinux is not enabled on this system
5+
selinux-error-file-open-failure = failed to open the file: { $error }
6+
selinux-error-context-retrieval-failure = failed to retrieve the security context: { $error }
7+
selinux-error-context-set-failure = failed to set default file creation context to '{ $context }': { $error }
8+
selinux-error-context-conversion-failure = failed to set default file creation context to '{ $context }': { $error }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Messages d'erreur SELinux
2+
# Ces chaînes ne sont chargées que lorsque la fonctionnalité selinux est activée
3+
4+
selinux-error-not-enabled = SELinux n'est pas activé sur ce système
5+
selinux-error-file-open-failure = échec de l'ouverture du fichier : { $error }
6+
selinux-error-context-retrieval-failure = échec de la récupération du contexte de sécurité : { $error }
7+
selinux-error-context-set-failure = échec de la définition du contexte de création de fichier par défaut à '{ $context }' : { $error }
8+
selinux-error-context-conversion-failure = échec de la définition du contexte de création de fichier par défaut à '{ $context }' : { $error }

src/uucore/locales/smack/en-US.ftl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SMACK error messages
2+
# These strings are only loaded when the smack feature is enabled
3+
4+
smack-error-not-enabled = SMACK is not enabled on this system
5+
smack-error-label-retrieval-failure = failed to get security context: { $error }
6+
smack-error-label-set-failure = failed to set default file creation context to '{ $context }': { $error }
7+
smack-error-no-label-set = no security context set

src/uucore/locales/smack/fr-FR.ftl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Messages d'erreur SMACK
2+
# Ces chaines ne sont chargees que lorsque la fonctionnalite smack est activee
3+
4+
smack-error-not-enabled = SMACK n'est pas active sur ce systeme
5+
smack-error-label-retrieval-failure = echec de la recuperation du contexte de securite : { $error }
6+
smack-error-label-set-failure = echec de la definition du contexte de creation de fichier par defaut a '{ $context }' : { $error }
7+
smack-error-no-label-set = aucun contexte de securite defini

src/uucore/src/lib/mods/locale.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,15 @@ fn create_bundle(
153153

154154
// Load common strings from uucore locales directory
155155
try_add_resource_from(find_uucore_locales_dir(locales_dir));
156+
157+
// Conditionally load SELinux locales when the selinux feature is enabled
158+
#[cfg(feature = "selinux")]
159+
try_add_resource_from(find_uucore_locales_dir(locales_dir).map(|p| p.join("selinux")));
160+
161+
// Conditionally load SMACK locales when the smack feature is enabled
162+
#[cfg(feature = "smack")]
163+
try_add_resource_from(find_uucore_locales_dir(locales_dir).map(|p| p.join("smack")));
164+
156165
// Then, try to load utility-specific strings from the utility's locale directory
157166
try_add_resource_from(get_locales_dir(util_name).ok());
158167

@@ -245,6 +254,20 @@ fn create_english_bundle_from_embedded(
245254
bundle.add_resource_overriding(uucore_resource);
246255
}
247256

257+
// Conditionally load SELinux embedded locales when the selinux feature is enabled
258+
#[cfg(feature = "selinux")]
259+
if let Some(selinux_content) = get_embedded_locale("uucore/selinux/en-US.ftl") {
260+
let selinux_resource = parse_fluent_resource(selinux_content)?;
261+
bundle.add_resource_overriding(selinux_resource);
262+
}
263+
264+
// Conditionally load SMACK embedded locales when the smack feature is enabled
265+
#[cfg(feature = "smack")]
266+
if let Some(smack_content) = get_embedded_locale("uucore/smack/en-US.ftl") {
267+
let smack_resource = parse_fluent_resource(smack_content)?;
268+
bundle.add_resource_overriding(smack_resource);
269+
}
270+
248271
// Then, try to load utility-specific strings
249272
let locale_key = format!("{util_name}/en-US.ftl");
250273
if let Some(ftl_content) = get_embedded_locale(&locale_key) {

0 commit comments

Comments
 (0)