Compare commits
2 Commits
15b1d4bfd6
...
5c7f7766de
Author | SHA1 | Date |
---|---|---|
Savanni D'Gerinel | 5c7f7766de | |
Savanni D'Gerinel | 034bda502a |
|
@ -2734,9 +2734,11 @@ dependencies = [
|
|||
"icu",
|
||||
"icu_locid",
|
||||
"icu_provider",
|
||||
"intl-memoizer",
|
||||
"serde 1.0.193",
|
||||
"serde_yaml",
|
||||
"sys-locale",
|
||||
"thiserror",
|
||||
"unic-langid",
|
||||
]
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
// This file was autogenerated from l10n-codegen-rust. Edits will be lost
|
||||
// on next generation.
|
||||
use l10n::Message;
|
||||
use fluent_ergonomics::FluentErgo;
|
||||
use fluent::FluentArgs;
|
||||
|
||||
pub struct Hello {
|
||||
|
@ -9,24 +8,25 @@ pub struct Hello {
|
|||
}
|
||||
|
||||
impl Message for Hello {
|
||||
fn localize(&self, bundle: &FluentErgo) -> String {
|
||||
fn msgid(&self) -> &str {
|
||||
"hello"
|
||||
}
|
||||
|
||||
fn args(&self) -> Option<FluentArgs> {
|
||||
let mut args = FluentArgs::new();
|
||||
args.set("name", self.name.clone());
|
||||
bundle.tr("hello", Some(&args)).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct NothingHere;
|
||||
impl Message for NothingHere {
|
||||
fn localize(&self, bundle: &FluentErgo) -> String {
|
||||
bundle.tr("nothing-here", None).unwrap()
|
||||
Some(args)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Welcome;
|
||||
impl Message for Welcome {
|
||||
fn localize(&self, bundle: &FluentErgo) -> String {
|
||||
bundle.tr("welcome", None).unwrap()
|
||||
fn msgid(&self) -> &str {
|
||||
"welcome"
|
||||
}
|
||||
|
||||
fn args(&self) -> Option<FluentArgs> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -35,9 +35,24 @@ pub struct GamesInDatabase {
|
|||
}
|
||||
|
||||
impl Message for GamesInDatabase {
|
||||
fn localize(&self, bundle: &FluentErgo) -> String {
|
||||
fn msgid(&self) -> &str {
|
||||
"games-in-database"
|
||||
}
|
||||
|
||||
fn args(&self) -> Option<FluentArgs> {
|
||||
let mut args = FluentArgs::new();
|
||||
args.set("count", self.count.clone());
|
||||
bundle.tr("games-in-database", Some(&args)).unwrap()
|
||||
Some(args)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct NothingHere;
|
||||
impl Message for NothingHere {
|
||||
fn msgid(&self) -> &str {
|
||||
"nothing-here"
|
||||
}
|
||||
|
||||
fn args(&self) -> Option<FluentArgs> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,9 +15,11 @@ fluent = { version = "0.16" }
|
|||
icu_locid = { version = "1" }
|
||||
icu_provider = { version = "1" }
|
||||
icu = { version = "1" }
|
||||
intl-memoizer = { version = "*" }
|
||||
serde = { version = "1", features = [ "derive" ] }
|
||||
serde_yaml = { version = "0.9" }
|
||||
sys-locale = { version = "0.3" }
|
||||
thiserror = { version = "1" }
|
||||
unic-langid = { version = "*" }
|
||||
|
||||
[[bin]]
|
||||
|
|
|
@ -81,8 +81,12 @@ impl Message {
|
|||
|
||||
struct_strs.push(format!("pub struct {}; ", self.name.to_case(Case::Pascal)));
|
||||
struct_strs.push(format!("impl Message for {} {{
|
||||
fn localize(&self, bundle: &FluentErgo) -> String {{
|
||||
bundle.tr(\"{}\", None).unwrap()
|
||||
fn msgid(&self) -> &str {{
|
||||
\"{}\"
|
||||
}}
|
||||
|
||||
fn args(&self) -> Option<FluentArgs> {{
|
||||
None
|
||||
}}
|
||||
}}",
|
||||
self.name.to_case(Case::Pascal),
|
||||
|
@ -114,15 +118,19 @@ impl Message {
|
|||
|
||||
struct_strs.push(format!(
|
||||
"impl Message for {} {{
|
||||
fn localize(&self, bundle: &FluentErgo) -> String {{
|
||||
fn msgid(&self) -> &str {{
|
||||
\"{}\"
|
||||
}}
|
||||
|
||||
fn args(&self) -> Option<FluentArgs> {{
|
||||
let mut args = FluentArgs::new();
|
||||
{}
|
||||
bundle.tr(\"{}\", Some(&args)).unwrap()
|
||||
Some(args)
|
||||
}}
|
||||
}}",
|
||||
self.name.to_case(Case::Pascal),
|
||||
parameters,
|
||||
self.name,
|
||||
parameters,
|
||||
));
|
||||
|
||||
struct_strs.join("\n")
|
||||
|
@ -144,7 +152,6 @@ fn main() {
|
|||
println!("// This file was autogenerated from l10n-codegen-rust. Edits will be lost
|
||||
// on next generation.");
|
||||
println!("use l10n::Message;");
|
||||
println!("use fluent_ergonomics::FluentErgo;");
|
||||
println!("use fluent::FluentArgs;");
|
||||
|
||||
let messages = messages
|
||||
|
|
105
l10n/src/lib.rs
105
l10n/src/lib.rs
|
@ -1,12 +1,13 @@
|
|||
use chrono::{Datelike, NaiveDate, Timelike};
|
||||
use chrono_tz::Tz;
|
||||
use fixed_decimal::FixedDecimal;
|
||||
use fluent::{FluentBundle, FluentResource};
|
||||
use fluent_ergonomics::FluentErgo;
|
||||
use fluent::{bundle::FluentBundle, FluentResource};
|
||||
use icu::{datetime::options::length, decimal::FixedDecimalFormatter, locid::Locale};
|
||||
use icu_provider::DataLocale;
|
||||
use std::{collections::HashMap, ops::Deref, path::Path};
|
||||
use std::{fs::File, io::Read, ops::Deref};
|
||||
use sys_locale::get_locale;
|
||||
use thiserror::Error;
|
||||
use unic_langid::LanguageIdentifierError;
|
||||
|
||||
// Re-exports. I'm doing these so that clients of this library don't have to go tracking down
|
||||
// additional structures
|
||||
|
@ -46,7 +47,9 @@ impl<A> Deref for NonEmptyList<A> {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum L10NError {
|
||||
#[error("Unparsable Locale")]
|
||||
UnparsableLocale,
|
||||
}
|
||||
|
||||
|
@ -56,6 +59,33 @@ impl From<icu::locid::Error> for L10NError {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum FileLoadError {
|
||||
#[error("Unparsable Locale")]
|
||||
UnparsableLocale,
|
||||
|
||||
#[error("Source string file not found")]
|
||||
FileNotFound,
|
||||
|
||||
#[error("The Fluent file is malformed")]
|
||||
FluentParseError,
|
||||
|
||||
#[error("An unknown IO error was found")]
|
||||
IOError(std::io::Error),
|
||||
}
|
||||
|
||||
impl From<LanguageIdentifierError> for FileLoadError {
|
||||
fn from(_: LanguageIdentifierError) -> Self {
|
||||
Self::UnparsableLocale
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for FileLoadError {
|
||||
fn from(err: std::io::Error) -> Self {
|
||||
Self::IOError(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Potential Message structure.
|
||||
//
|
||||
// Let's assume the application has an enumeration that implements Message. For each element of the
|
||||
|
@ -68,12 +98,13 @@ impl From<icu::locid::Error> for L10NError {
|
|||
// However, I have not found a mechanism in Fluent to identify all of the placeholders within a
|
||||
// message, so I'm not even sure that I can automate this code generation.
|
||||
pub trait Message {
|
||||
fn localize(&self, bundle: &FluentErgo) -> String;
|
||||
fn msgid(&self) -> &str;
|
||||
fn args(&self) -> Option<FluentArgs>;
|
||||
}
|
||||
|
||||
pub struct L10N {
|
||||
messages_root: std::path::PathBuf,
|
||||
message_bundle: FluentErgo,
|
||||
message_bundles: Vec<FluentBundle<FluentResource, intl_memoizer::concurrent::IntlLangMemoizer>>,
|
||||
|
||||
locales: NonEmptyList<Locale>,
|
||||
zone: chrono_tz::Tz,
|
||||
|
@ -94,31 +125,39 @@ impl L10N {
|
|||
let english_phrases = FluentResource::try_new
|
||||
*/
|
||||
|
||||
let message_bundle = {
|
||||
let mut english_messages = messages_root.clone();
|
||||
english_messages.push("en-US.ftl");
|
||||
|
||||
let langid: unic_langid::LanguageIdentifier = english.to_string().parse().unwrap();
|
||||
let mut messages = FluentErgo::new(&[langid.clone()]);
|
||||
let _ = messages.add_from_file(langid, &english_messages);
|
||||
|
||||
messages
|
||||
};
|
||||
|
||||
Self {
|
||||
let mut s = Self {
|
||||
messages_root,
|
||||
message_bundle,
|
||||
message_bundles: vec![],
|
||||
locales,
|
||||
zone,
|
||||
}
|
||||
};
|
||||
|
||||
s.load_messages_from_file("en-US".to_owned()).unwrap();
|
||||
|
||||
s
|
||||
}
|
||||
|
||||
pub fn load_messages_from_file(
|
||||
&mut self,
|
||||
locale: String,
|
||||
path: &Path,
|
||||
) -> Result<(), L10NError> {
|
||||
unimplemented!()
|
||||
fn load_messages_from_file(&mut self, locale: String) -> Result<(), FileLoadError> {
|
||||
let langid: unic_langid::LanguageIdentifier = locale.parse()?;
|
||||
|
||||
let mut path = self.messages_root.clone();
|
||||
path.push(locale);
|
||||
path.set_extension("ftl");
|
||||
println!("{:?}", path);
|
||||
|
||||
let mut buffer = Vec::new();
|
||||
let mut f = File::open(path)?;
|
||||
f.read_to_end(&mut buffer)?;
|
||||
let text = String::from_utf8(buffer).unwrap();
|
||||
match FluentResource::try_new(text) {
|
||||
Ok(resource) => {
|
||||
let mut bundle = FluentBundle::new_concurrent(vec![langid]);
|
||||
let _ = bundle.add_resource(resource);
|
||||
self.message_bundles.push(bundle);
|
||||
Ok(())
|
||||
}
|
||||
Err((_, _error)) => Err(FileLoadError::FluentParseError),
|
||||
}
|
||||
}
|
||||
|
||||
// Now, whenever the user changes the locales, the list of messages has to change. How do we
|
||||
|
@ -156,12 +195,20 @@ impl L10N {
|
|||
// parameters. In an ideal world, neither of these can be incorrect. Messages are all checked
|
||||
// at compile time, as are their parameters. That implies an enumeration, with one element per
|
||||
// message, and with each element knowing its parameters.
|
||||
pub fn messages(&self) -> FluentErgo {
|
||||
self.message_bundle.clone()
|
||||
}
|
||||
// pub fn messages(&self) -> Vec<FluentBundle<FluentResource>> {
|
||||
// self.message_bundles.clone()
|
||||
// }
|
||||
|
||||
pub fn tr(&self, message: impl Message) -> String {
|
||||
message.localize(&self.message_bundle)
|
||||
println!("message: {}", message.msgid());
|
||||
let msg = self.message_bundles[0]
|
||||
.get_message(message.msgid())
|
||||
.and_then(|msg| msg.value())
|
||||
.unwrap();
|
||||
let mut errors = vec![];
|
||||
self.message_bundles[0]
|
||||
.format_pattern(msg, message.args().as_ref(), &mut errors)
|
||||
.to_string()
|
||||
}
|
||||
|
||||
pub fn format_date_time_utc(
|
||||
|
|
Loading…
Reference in New Issue