@@ -3,6 +3,7 @@ use crate::fluent_bundle::FluentResource;
3
3
use crate :: snippet:: Style ;
4
4
use crate :: { DiagnosticArg , DiagnosticMessage , FluentBundle } ;
5
5
use fluent:: FluentBundle as RawFluentBundle ;
6
+ use md5:: { Digest , Md5 } ;
6
7
use rustc_data_structures:: sync:: Lrc ;
7
8
use rustc_error_messages:: FluentArgs ;
8
9
use std:: borrow:: Cow ;
@@ -69,23 +70,68 @@ pub trait Translate {
69
70
}
70
71
DiagnosticMessage :: FluentRaw ( msg) => {
71
72
// 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 ( ) ) ;
83
80
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
+ } ;
86
131
}
87
132
DiagnosticMessage :: FluentIdentifier ( identifier, attr) => ( identifier, attr) ,
88
133
} ;
134
+ // FIXME(yukang): remove this part for fluent resource id after all diagnostics are migrated to Fluent
89
135
let translate_with_bundle =
90
136
|bundle : & ' a FluentBundle | -> Result < Cow < ' _ , str > , TranslateError < ' _ > > {
91
137
let message = bundle
0 commit comments