Skip to content

Commit 3802613

Browse files
committed
find fluent resource from hash slug
1 parent d9cb8d3 commit 3802613

File tree

5 files changed

+67
-22
lines changed

5 files changed

+67
-22
lines changed

Cargo.lock

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2338,10 +2338,11 @@ dependencies = [
23382338

23392339
[[package]]
23402340
name = "md-5"
2341-
version = "0.10.5"
2341+
version = "0.10.6"
23422342
source = "registry+https://github.com/rust-lang/crates.io-index"
2343-
checksum = "6365506850d44bff6e2fbcb5176cf63650e48bd45ef2fe2665ae1570e0f4b9ca"
2343+
checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf"
23442344
dependencies = [
2345+
"cfg-if",
23452346
"digest",
23462347
]
23472348

@@ -3812,6 +3813,7 @@ dependencies = [
38123813
"annotate-snippets",
38133814
"derive_setters",
38143815
"fluent",
3816+
"md-5",
38153817
"rustc_ast",
38163818
"rustc_ast_pretty",
38173819
"rustc_data_structures",

compiler/rustc_errors/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ edition = "2021"
88
annotate-snippets = "0.9"
99
derive_setters = "0.1.6"
1010
fluent = "0.16.0"
11+
md-5 = "0.10.6"
1112
rustc_ast = { path = "../rustc_ast" }
1213
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
1314
rustc_data_structures = { path = "../rustc_data_structures" }

compiler/rustc_errors/src/translation.rs

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::fluent_bundle::FluentResource;
33
use crate::snippet::Style;
44
use crate::{DiagnosticArg, DiagnosticMessage, FluentBundle};
55
use fluent::FluentBundle as RawFluentBundle;
6+
use md5::{Digest, Md5};
67
use rustc_data_structures::sync::Lrc;
78
use rustc_error_messages::FluentArgs;
89
use std::borrow::Cow;
@@ -69,23 +70,68 @@ pub trait Translate {
6970
}
7071
DiagnosticMessage::FluentRaw(msg) => {
7172
// FIXME(yukang): A hack for raw fluent content for new diagnostics proc format
72-
let fluent_text = format!("dummy = {}", msg);
73-
if let Ok(resource) = FluentResource::try_new(fluent_text) {
74-
let mut bundle = RawFluentBundle::new(vec![langid!("en-US")]);
75-
bundle.add_resource(resource).unwrap();
76-
let mut errors = vec![];
77-
let pattern = bundle.get_message("dummy").unwrap().value().unwrap();
78-
let res = bundle.format_pattern(&pattern, Some(args), &mut errors);
79-
return Ok(Cow::Owned(
80-
res.to_string().replace("\u{2068}", "").replace("\u{2069}", ""),
81-
));
82-
}
73+
// calculate the `slug` from the raw fluent content
74+
let mut hasher = Md5::new();
75+
hasher.update(msg.to_string());
76+
let digest = hasher.finalize();
77+
let id = format!("{:x}", digest);
78+
let identifier = format!("slug-{}", id[0..8].to_string());
79+
let id = Cow::Borrowed(identifier.as_str());
8380

84-
// If the message is not a valid Fluent resource, just return the original
85-
return Ok(Cow::Borrowed(msg));
81+
//eprintln!("trying id: {}", identifier);
82+
let translate_with_bundle =
83+
|bundle: &'a FluentBundle| -> Result<Cow<'_, str>, TranslateError<'_>> {
84+
let message = bundle
85+
.get_message(&identifier)
86+
.ok_or(TranslateError::message(&id, args))?;
87+
let value = message.value().ok_or(TranslateError::value(&id, args))?;
88+
debug!(?message, ?value);
89+
90+
let mut errs = vec![];
91+
let translated = bundle.format_pattern(value, Some(args), &mut errs);
92+
debug!(?translated, ?errs);
93+
if errs.is_empty() {
94+
Ok(translated)
95+
} else {
96+
Err(TranslateError::fluent(&id, args, errs))
97+
}
98+
};
99+
100+
return {
101+
match self.fluent_bundle().map(|b| translate_with_bundle(b)) {
102+
// The primary bundle was present and translation succeeded
103+
Some(Ok(t)) => {
104+
// eprintln!("translated id OK: {} => {}", identifier, t);
105+
Ok(t)
106+
}
107+
108+
// If `translate_with_bundle` returns `Err` with the primary bundle, this is likely
109+
// just that the primary bundle doesn't contain the message being translated, so
110+
// proceed to the fallback bundle.
111+
_ => {
112+
// eprintln!("fallback to en.... for id: {}", identifier);
113+
// fallback to en-US, we don't need fluent bundle for raw fluent content in English
114+
// here we just interpret the variables in the fluent content.
115+
let fluent_text = format!("dummy = {}", msg);
116+
if let Ok(resource) = FluentResource::try_new(fluent_text) {
117+
let mut bundle = RawFluentBundle::new(vec![langid!("en-US")]);
118+
bundle.add_resource(resource).unwrap();
119+
let mut errors = vec![];
120+
let pattern = bundle.get_message("dummy").unwrap().value().unwrap();
121+
let res = bundle.format_pattern(&pattern, Some(args), &mut errors);
122+
Ok(Cow::Owned(
123+
res.to_string().replace("\u{2068}", "").replace("\u{2069}", ""),
124+
))
125+
} else {
126+
Ok(Cow::Owned(msg.to_string()))
127+
}
128+
}
129+
}
130+
};
86131
}
87132
DiagnosticMessage::FluentIdentifier(identifier, attr) => (identifier, attr),
88133
};
134+
// FIXME(yukang): remove this part for fluent resource id after all diagnostics are migrated to Fluent
89135
let translate_with_bundle =
90136
|bundle: &'a FluentBundle| -> Result<Cow<'_, str>, TranslateError<'_>> {
91137
let message = bundle

tests/run-make/translation/Makefile

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@ FAKEROOT=$(TMPDIR)/fakeroot
99
RUSTC_LOG:=rustc_error_messages
1010
export RUSTC_TRANSLATION_NO_DEBUG_ASSERT:=1
1111

12-
all: normal missing broken sysroot-invalid sysroot-missing
12+
all: normal custom missing broken sysroot sysroot-invalid sysroot-missing
1313

1414
# Check that the test works normally, using the built-in fallback bundle.
1515
normal: test.rs
1616
$(RUSTC) $< 2>&1 | $(CGREP) "struct literal body without path"
1717

1818
# Check that a primary bundle can be loaded and will be preferentially used
1919
# where possible.
20-
# FIXME(yukang): This test is broken because the compiler doesn't look for the fluent slugs now
21-
# we need to fix it after we have implemented the new way to find the fluent resources
2220
custom: test.rs working.ftl
2321
$(RUSTC) $< -Ztranslate-additional-ftl=$(CURDIR)/working.ftl 2>&1 | $(CGREP) "this is a test message"
2422

@@ -36,8 +34,6 @@ broken: test.rs broken.ftl
3634
# identifier by making a local copy of the sysroot and adding the custom locale
3735
# to it.
3836

39-
# FIXME(yukang): This test is broken because the compiler doesn't look for the fluent slugs now
40-
# we need to fix it after we have implemented the new way to find the fluent resources
4137
sysroot: test.rs working.ftl
4238
rm -rf $(FAKEROOT)
4339
mkdir $(FAKEROOT)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
parse_struct_literal_body_without_path = this is a test message
2-
.suggestion = this is a test suggestion
1+
slug-687d6246 = this is a test message
2+
slug-7d40154a = this is a test suggestion

0 commit comments

Comments
 (0)