|
| 1 | +use crate::error::TranslateError; |
1 | 2 | use crate::snippet::Style;
|
2 | 3 | use crate::{DiagnosticArg, DiagnosticMessage, FluentBundle};
|
3 | 4 | use rustc_data_structures::sync::Lrc;
|
4 |
| -use rustc_error_messages::{ |
5 |
| - fluent_bundle::resolver::errors::{ReferenceKind, ResolverError}, |
6 |
| - FluentArgs, FluentError, |
7 |
| -}; |
| 5 | +use rustc_error_messages::FluentArgs; |
8 | 6 | use std::borrow::Cow;
|
| 7 | +use std::error::Report; |
9 | 8 |
|
10 | 9 | /// Convert diagnostic arguments (a rustc internal type that exists to implement
|
11 | 10 | /// `Encodable`/`Decodable`) into `FluentArgs` which is necessary to perform translation.
|
@@ -63,75 +62,50 @@ pub trait Translate {
|
63 | 62 | }
|
64 | 63 | DiagnosticMessage::FluentIdentifier(identifier, attr) => (identifier, attr),
|
65 | 64 | };
|
| 65 | + let translate_with_bundle = |
| 66 | + |bundle: &'a FluentBundle| -> Result<Cow<'_, str>, TranslateError<'_>> { |
| 67 | + let message = bundle |
| 68 | + .get_message(identifier) |
| 69 | + .ok_or(TranslateError::message(identifier, args))?; |
| 70 | + let value = match attr { |
| 71 | + Some(attr) => message |
| 72 | + .get_attribute(attr) |
| 73 | + .ok_or(TranslateError::attribute(identifier, args, attr))? |
| 74 | + .value(), |
| 75 | + None => message.value().ok_or(TranslateError::value(identifier, args))?, |
| 76 | + }; |
| 77 | + debug!(?message, ?value); |
66 | 78 |
|
67 |
| - let translate_with_bundle = |bundle: &'a FluentBundle| -> Option<(Cow<'_, str>, Vec<_>)> { |
68 |
| - let message = bundle.get_message(identifier)?; |
69 |
| - let value = match attr { |
70 |
| - Some(attr) => message.get_attribute(attr)?.value(), |
71 |
| - None => message.value()?, |
| 79 | + let mut errs = vec![]; |
| 80 | + let translated = bundle.format_pattern(value, Some(args), &mut errs); |
| 81 | + debug!(?translated, ?errs); |
| 82 | + if errs.is_empty() { |
| 83 | + Ok(translated) |
| 84 | + } else { |
| 85 | + Err(TranslateError::fluent(identifier, args, errs)) |
| 86 | + } |
72 | 87 | };
|
73 |
| - debug!(?message, ?value); |
74 |
| - |
75 |
| - let mut errs = vec![]; |
76 |
| - let translated = bundle.format_pattern(value, Some(args), &mut errs); |
77 |
| - debug!(?translated, ?errs); |
78 |
| - Some((translated, errs)) |
79 |
| - }; |
80 | 88 |
|
81 |
| - self.fluent_bundle() |
82 |
| - .and_then(|bundle| translate_with_bundle(bundle)) |
83 |
| - // If `translate_with_bundle` returns `None` with the primary bundle, this is likely |
84 |
| - // just that the primary bundle doesn't contain the message being translated, so |
85 |
| - // proceed to the fallback bundle. |
86 |
| - // |
87 |
| - // However, when errors are produced from translation, then that means the translation |
88 |
| - // is broken (e.g. `{$foo}` exists in a translation but `foo` isn't provided). |
89 |
| - // |
90 |
| - // In debug builds, assert so that compiler devs can spot the broken translation and |
91 |
| - // fix it.. |
92 |
| - .inspect(|(_, errs)| { |
93 |
| - debug_assert!( |
94 |
| - errs.is_empty(), |
95 |
| - "identifier: {:?}, attr: {:?}, args: {:?}, errors: {:?}", |
96 |
| - identifier, |
97 |
| - attr, |
98 |
| - args, |
99 |
| - errs |
100 |
| - ); |
101 |
| - }) |
102 |
| - // ..otherwise, for end users, an error about this wouldn't be useful or actionable, so |
103 |
| - // just hide it and try with the fallback bundle. |
104 |
| - .filter(|(_, errs)| errs.is_empty()) |
105 |
| - .or_else(|| translate_with_bundle(self.fallback_fluent_bundle())) |
106 |
| - .map(|(translated, errs)| { |
107 |
| - // Always bail out for errors with the fallback bundle. |
| 89 | + let ret: Result<Cow<'_, str>, TranslateError<'_>> = try { |
| 90 | + match self.fluent_bundle().map(|b| translate_with_bundle(b)) { |
| 91 | + // The primary bundle was present and translation succeeded |
| 92 | + Some(Ok(t)) => t, |
108 | 93 |
|
109 |
| - let mut help_messages = vec![]; |
| 94 | + // Always yeet out for errors on debug |
| 95 | + Some(Err(primary)) if cfg!(debug_assertions) => do yeet primary, |
110 | 96 |
|
111 |
| - if !errs.is_empty() { |
112 |
| - for error in &errs { |
113 |
| - match error { |
114 |
| - FluentError::ResolverError(ResolverError::Reference( |
115 |
| - ReferenceKind::Message { id, .. }, |
116 |
| - )) if args.iter().any(|(arg_id, _)| arg_id == id) => { |
117 |
| - help_messages.push(format!("Argument `{id}` exists but was not referenced correctly. Try using `{{${id}}}` instead")); |
118 |
| - } |
119 |
| - _ => {} |
120 |
| - } |
121 |
| - } |
| 97 | + // If `translate_with_bundle` returns `Err` with the primary bundle, this is likely |
| 98 | + // just that the primary bundle doesn't contain the message being translated or |
| 99 | + // something else went wrong) so proceed to the fallback bundle. |
| 100 | + Some(Err(primary)) => translate_with_bundle(self.fallback_fluent_bundle()) |
| 101 | + .map_err(|fallback| primary.and(fallback))?, |
122 | 102 |
|
123 |
| - panic!( |
124 |
| - "Encountered errors while formatting message for `{identifier}`\n\ |
125 |
| - help: {}\n\ |
126 |
| - attr: `{attr:?}`\n\ |
127 |
| - args: `{args:?}`\n\ |
128 |
| - errors: `{errs:?}`", |
129 |
| - help_messages.join("\nhelp: ") |
130 |
| - ); |
131 |
| - } |
132 |
| - |
133 |
| - translated |
134 |
| - }) |
| 103 | + // The primary bundle is missing, proceed to the fallback bundle |
| 104 | + None => translate_with_bundle(self.fallback_fluent_bundle()) |
| 105 | + .map_err(|fallback| TranslateError::primary(identifier, args).and(fallback))?, |
| 106 | + } |
| 107 | + }; |
| 108 | + ret.map_err(Report::new) |
135 | 109 | .expect("failed to find message in primary or fallback fluent bundles")
|
136 | 110 | }
|
137 | 111 | }
|
0 commit comments