|
1 | 1 | use crate::error::{TranslateError, TranslateErrorKind}; |
| 2 | +use crate::fluent_bundle::FluentResource; |
2 | 3 | use crate::snippet::Style; |
3 | 4 | use crate::{DiagnosticArg, DiagnosticMessage, FluentBundle}; |
| 5 | +use fluent::FluentBundle as RawFluentBundle; |
4 | 6 | use rustc_data_structures::sync::Lrc; |
5 | 7 | use rustc_error_messages::FluentArgs; |
6 | 8 | use std::borrow::Cow; |
7 | 9 | use std::env; |
8 | 10 | use std::error::Report; |
| 11 | +use unic_langid::langid; |
9 | 12 |
|
10 | 13 | /// Convert diagnostic arguments (a rustc internal type that exists to implement |
11 | 14 | /// `Encodable`/`Decodable`) into `FluentArgs` which is necessary to perform translation. |
@@ -62,7 +65,29 @@ pub trait Translate { |
62 | 65 | trace!(?message, ?args); |
63 | 66 | let (identifier, attr) = match message { |
64 | 67 | DiagnosticMessage::Str(msg) | DiagnosticMessage::Eager(msg) => { |
65 | | - return Ok(Cow::Borrowed(msg)); |
| 68 | + if args.iter().next().is_none() || (!msg.contains("$") && !msg.contains("`{")) { |
| 69 | + return Ok(Cow::Borrowed(msg)); |
| 70 | + } else { |
| 71 | + // FIXME(yukang) A hacky for raw fluent content |
| 72 | + let fluent_text = format!("dummy = {}", msg); |
| 73 | + if let Ok(resource) = FluentResource::try_new(fluent_text) { |
| 74 | + let langid_en = langid!("en-US"); |
| 75 | + let mut bundle = RawFluentBundle::new(vec![langid_en]); |
| 76 | + bundle.add_resource(resource).unwrap(); |
| 77 | + let mut errors = vec![]; |
| 78 | + let pattern = bundle.get_message("dummy").unwrap().value().unwrap(); |
| 79 | + let res = bundle.format_pattern(&pattern, Some(args), &mut errors); |
| 80 | + //eprintln!("translated: {:?}", msg); |
| 81 | + //eprintln!("args: {:?}", args); |
| 82 | + //eprintln!("res: {:?}", res); |
| 83 | + return Ok(Cow::Owned( |
| 84 | + res.to_string().replace("\u{2068}", "").replace("\u{2069}", ""), |
| 85 | + )); |
| 86 | + } else { |
| 87 | + //eprintln!("translate error: {}, args: {:?}", msg, args); |
| 88 | + return Ok(Cow::Borrowed(msg)); |
| 89 | + } |
| 90 | + } |
66 | 91 | } |
67 | 92 | DiagnosticMessage::FluentIdentifier(identifier, attr) => (identifier, attr), |
68 | 93 | }; |
|
0 commit comments