mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
l10n: improve the display when the ftl is invalid
This commit is contained in:
parent
7102e1a4b5
commit
a36d5455ab
4 changed files with 47 additions and 13 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -3601,6 +3601,7 @@ dependencies = [
|
||||||
"dunce",
|
"dunce",
|
||||||
"fluent",
|
"fluent",
|
||||||
"fluent-bundle",
|
"fluent-bundle",
|
||||||
|
"fluent-syntax",
|
||||||
"glob",
|
"glob",
|
||||||
"hex",
|
"hex",
|
||||||
"itertools 0.14.0",
|
"itertools 0.14.0",
|
||||||
|
|
|
@ -364,6 +364,7 @@ digest = "0.10.7"
|
||||||
fluent-bundle = "0.16.0"
|
fluent-bundle = "0.16.0"
|
||||||
fluent = "0.17.0"
|
fluent = "0.17.0"
|
||||||
unic-langid = "0.9.6"
|
unic-langid = "0.9.6"
|
||||||
|
fluent-syntax = "0.12.0"
|
||||||
|
|
||||||
uucore = { version = "0.1.0", package = "uucore", path = "src/uucore" }
|
uucore = { version = "0.1.0", package = "uucore", path = "src/uucore" }
|
||||||
uucore_procs = { version = "0.1.0", package = "uucore_procs", path = "src/uucore_procs" }
|
uucore_procs = { version = "0.1.0", package = "uucore_procs", path = "src/uucore_procs" }
|
||||||
|
|
|
@ -60,6 +60,7 @@ selinux = { workspace = true, optional = true }
|
||||||
# Fluent dependencies
|
# Fluent dependencies
|
||||||
fluent-bundle = { workspace = true }
|
fluent-bundle = { workspace = true }
|
||||||
fluent = { workspace = true }
|
fluent = { workspace = true }
|
||||||
|
fluent-syntax = { workspace = true }
|
||||||
unic-langid = { workspace = true }
|
unic-langid = { workspace = true }
|
||||||
thiserror = { workspace = true }
|
thiserror = { workspace = true }
|
||||||
[target.'cfg(unix)'.dependencies]
|
[target.'cfg(unix)'.dependencies]
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
use crate::error::UError;
|
use crate::error::UError;
|
||||||
use fluent::{FluentArgs, FluentBundle, FluentResource};
|
use fluent::{FluentArgs, FluentBundle, FluentResource};
|
||||||
|
use fluent_syntax::parser::ParserError;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
@ -21,8 +22,14 @@ pub enum LocalizationError {
|
||||||
source: std::io::Error,
|
source: std::io::Error,
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
},
|
},
|
||||||
#[error("Parse error: {0}")]
|
#[error("Parse-locale error: {0}")]
|
||||||
Parse(String),
|
ParseLocale(String),
|
||||||
|
#[error("Resource parse error at '{snippet}': {error:?}")]
|
||||||
|
ParseResource {
|
||||||
|
#[source]
|
||||||
|
error: ParserError,
|
||||||
|
snippet: String,
|
||||||
|
},
|
||||||
#[error("Bundle error: {0}")]
|
#[error("Bundle error: {0}")]
|
||||||
Bundle(String),
|
Bundle(String),
|
||||||
#[error("Locales directory not found: {0}")]
|
#[error("Locales directory not found: {0}")]
|
||||||
|
@ -261,9 +268,9 @@ fn detect_system_locale() -> Result<LanguageIdentifier, LocalizationError> {
|
||||||
.next()
|
.next()
|
||||||
.unwrap_or(DEFAULT_LOCALE)
|
.unwrap_or(DEFAULT_LOCALE)
|
||||||
.to_string();
|
.to_string();
|
||||||
|
LanguageIdentifier::from_str(&locale_str).map_err(|_| {
|
||||||
LanguageIdentifier::from_str(&locale_str)
|
LocalizationError::ParseLocale(format!("Failed to parse locale: {}", locale_str))
|
||||||
.map_err(|_| LocalizationError::Parse(format!("Failed to parse locale: {}", locale_str)))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets up localization using the system locale with English fallback.
|
/// Sets up localization using the system locale with English fallback.
|
||||||
|
@ -461,7 +468,7 @@ invalid-syntax = This is { $missing
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_localization_error_uerror_impl() {
|
fn test_localization_error_uerror_impl() {
|
||||||
let error = LocalizationError::Parse("test error".to_string());
|
let error = LocalizationError::Bundle("some error".to_string());
|
||||||
assert_eq!(error.code(), 1);
|
assert_eq!(error.code(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -500,10 +507,14 @@ invalid-syntax = This is { $missing
|
||||||
let result = create_bundle(&locale, temp_dir.path());
|
let result = create_bundle(&locale, temp_dir.path());
|
||||||
assert!(result.is_err());
|
assert!(result.is_err());
|
||||||
|
|
||||||
if let Err(LocalizationError::Parse(_)) = result {
|
if let Err(LocalizationError::ParseResource {
|
||||||
// Expected parse error
|
error: _parser_err,
|
||||||
|
snippet: _,
|
||||||
|
}) = result
|
||||||
|
{
|
||||||
|
// Expected ParseResource variant
|
||||||
} else {
|
} else {
|
||||||
panic!("Expected parse error");
|
panic!("Expected ParseResource error");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1042,12 +1053,32 @@ invalid-syntax = This is { $missing
|
||||||
assert!(error_string.contains("I/O error loading"));
|
assert!(error_string.contains("I/O error loading"));
|
||||||
assert!(error_string.contains("/test/path.ftl"));
|
assert!(error_string.contains("/test/path.ftl"));
|
||||||
|
|
||||||
let parse_error = LocalizationError::Parse("Syntax error".to_string());
|
|
||||||
let parse_string = format!("{}", parse_error);
|
|
||||||
assert!(parse_string.contains("Parse error: Syntax error"));
|
|
||||||
|
|
||||||
let bundle_error = LocalizationError::Bundle("Bundle creation failed".to_string());
|
let bundle_error = LocalizationError::Bundle("Bundle creation failed".to_string());
|
||||||
let bundle_string = format!("{}", bundle_error);
|
let bundle_string = format!("{}", bundle_error);
|
||||||
assert!(bundle_string.contains("Bundle error: Bundle creation failed"));
|
assert!(bundle_string.contains("Bundle error: Bundle creation failed"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_resource_error_includes_snippet() {
|
||||||
|
let temp_dir = create_test_locales_dir();
|
||||||
|
let locale = LanguageIdentifier::from_str("es-ES").unwrap();
|
||||||
|
|
||||||
|
let result = create_bundle(&locale, temp_dir.path());
|
||||||
|
assert!(result.is_err());
|
||||||
|
|
||||||
|
if let Err(LocalizationError::ParseResource {
|
||||||
|
error: _err,
|
||||||
|
snippet,
|
||||||
|
}) = result
|
||||||
|
{
|
||||||
|
// The snippet should contain exactly the invalid text from es-ES.ftl
|
||||||
|
assert!(
|
||||||
|
snippet.contains("This is { $missing"),
|
||||||
|
"snippet was `{}` but did not include the invalid text",
|
||||||
|
snippet
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
panic!("Expected LocalizationError::ParseResource with snippet");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue