Compare commits
2 Commits
15b1d4bfd6
...
5c7f7766de
Author | SHA1 | Date |
---|---|---|
Savanni D'Gerinel | 5c7f7766de | |
Savanni D'Gerinel | 034bda502a |
|
@ -2734,9 +2734,11 @@ dependencies = [
|
||||||
"icu",
|
"icu",
|
||||||
"icu_locid",
|
"icu_locid",
|
||||||
"icu_provider",
|
"icu_provider",
|
||||||
|
"intl-memoizer",
|
||||||
"serde 1.0.193",
|
"serde 1.0.193",
|
||||||
"serde_yaml",
|
"serde_yaml",
|
||||||
"sys-locale",
|
"sys-locale",
|
||||||
|
"thiserror",
|
||||||
"unic-langid",
|
"unic-langid",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
// This file was autogenerated from l10n-codegen-rust. Edits will be lost
|
// This file was autogenerated from l10n-codegen-rust. Edits will be lost
|
||||||
// on next generation.
|
// on next generation.
|
||||||
use l10n::Message;
|
use l10n::Message;
|
||||||
use fluent_ergonomics::FluentErgo;
|
|
||||||
use fluent::FluentArgs;
|
use fluent::FluentArgs;
|
||||||
|
|
||||||
pub struct Hello {
|
pub struct Hello {
|
||||||
|
@ -9,24 +8,25 @@ pub struct Hello {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Message for Hello {
|
impl Message for Hello {
|
||||||
fn localize(&self, bundle: &FluentErgo) -> String {
|
fn msgid(&self) -> &str {
|
||||||
let mut args = FluentArgs::new();
|
"hello"
|
||||||
args.set("name", self.name.clone());
|
|
||||||
bundle.tr("hello", Some(&args)).unwrap()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct NothingHere;
|
fn args(&self) -> Option<FluentArgs> {
|
||||||
impl Message for NothingHere {
|
let mut args = FluentArgs::new();
|
||||||
fn localize(&self, bundle: &FluentErgo) -> String {
|
args.set("name", self.name.clone());
|
||||||
bundle.tr("nothing-here", None).unwrap()
|
Some(args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Welcome;
|
pub struct Welcome;
|
||||||
impl Message for Welcome {
|
impl Message for Welcome {
|
||||||
fn localize(&self, bundle: &FluentErgo) -> String {
|
fn msgid(&self) -> &str {
|
||||||
bundle.tr("welcome", None).unwrap()
|
"welcome"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn args(&self) -> Option<FluentArgs> {
|
||||||
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,9 +35,24 @@ pub struct GamesInDatabase {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Message for 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();
|
let mut args = FluentArgs::new();
|
||||||
args.set("count", self.count.clone());
|
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_locid = { version = "1" }
|
||||||
icu_provider = { version = "1" }
|
icu_provider = { version = "1" }
|
||||||
icu = { version = "1" }
|
icu = { version = "1" }
|
||||||
|
intl-memoizer = { version = "*" }
|
||||||
serde = { version = "1", features = [ "derive" ] }
|
serde = { version = "1", features = [ "derive" ] }
|
||||||
serde_yaml = { version = "0.9" }
|
serde_yaml = { version = "0.9" }
|
||||||
sys-locale = { version = "0.3" }
|
sys-locale = { version = "0.3" }
|
||||||
|
thiserror = { version = "1" }
|
||||||
unic-langid = { version = "*" }
|
unic-langid = { version = "*" }
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
|
|
|
@ -81,8 +81,12 @@ impl Message {
|
||||||
|
|
||||||
struct_strs.push(format!("pub struct {}; ", self.name.to_case(Case::Pascal)));
|
struct_strs.push(format!("pub struct {}; ", self.name.to_case(Case::Pascal)));
|
||||||
struct_strs.push(format!("impl Message for {} {{
|
struct_strs.push(format!("impl Message for {} {{
|
||||||
fn localize(&self, bundle: &FluentErgo) -> String {{
|
fn msgid(&self) -> &str {{
|
||||||
bundle.tr(\"{}\", None).unwrap()
|
\"{}\"
|
||||||
|
}}
|
||||||
|
|
||||||
|
fn args(&self) -> Option<FluentArgs> {{
|
||||||
|
None
|
||||||
}}
|
}}
|
||||||
}}",
|
}}",
|
||||||
self.name.to_case(Case::Pascal),
|
self.name.to_case(Case::Pascal),
|
||||||
|
@ -114,15 +118,19 @@ impl Message {
|
||||||
|
|
||||||
struct_strs.push(format!(
|
struct_strs.push(format!(
|
||||||
"impl Message for {} {{
|
"impl Message for {} {{
|
||||||
fn localize(&self, bundle: &FluentErgo) -> String {{
|
fn msgid(&self) -> &str {{
|
||||||
|
\"{}\"
|
||||||
|
}}
|
||||||
|
|
||||||
|
fn args(&self) -> Option<FluentArgs> {{
|
||||||
let mut args = FluentArgs::new();
|
let mut args = FluentArgs::new();
|
||||||
{}
|
{}
|
||||||
bundle.tr(\"{}\", Some(&args)).unwrap()
|
Some(args)
|
||||||
}}
|
}}
|
||||||
}}",
|
}}",
|
||||||
self.name.to_case(Case::Pascal),
|
self.name.to_case(Case::Pascal),
|
||||||
parameters,
|
|
||||||
self.name,
|
self.name,
|
||||||
|
parameters,
|
||||||
));
|
));
|
||||||
|
|
||||||
struct_strs.join("\n")
|
struct_strs.join("\n")
|
||||||
|
@ -144,7 +152,6 @@ fn main() {
|
||||||
println!("// This file was autogenerated from l10n-codegen-rust. Edits will be lost
|
println!("// This file was autogenerated from l10n-codegen-rust. Edits will be lost
|
||||||
// on next generation.");
|
// on next generation.");
|
||||||
println!("use l10n::Message;");
|
println!("use l10n::Message;");
|
||||||
println!("use fluent_ergonomics::FluentErgo;");
|
|
||||||
println!("use fluent::FluentArgs;");
|
println!("use fluent::FluentArgs;");
|
||||||
|
|
||||||
let messages = messages
|
let messages = messages
|
||||||
|
|
105
l10n/src/lib.rs
105
l10n/src/lib.rs
|
@ -1,12 +1,13 @@
|
||||||
use chrono::{Datelike, NaiveDate, Timelike};
|
use chrono::{Datelike, NaiveDate, Timelike};
|
||||||
use chrono_tz::Tz;
|
use chrono_tz::Tz;
|
||||||
use fixed_decimal::FixedDecimal;
|
use fixed_decimal::FixedDecimal;
|
||||||
use fluent::{FluentBundle, FluentResource};
|
use fluent::{bundle::FluentBundle, FluentResource};
|
||||||
use fluent_ergonomics::FluentErgo;
|
|
||||||
use icu::{datetime::options::length, decimal::FixedDecimalFormatter, locid::Locale};
|
use icu::{datetime::options::length, decimal::FixedDecimalFormatter, locid::Locale};
|
||||||
use icu_provider::DataLocale;
|
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 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
|
// Re-exports. I'm doing these so that clients of this library don't have to go tracking down
|
||||||
// additional structures
|
// additional structures
|
||||||
|
@ -46,7 +47,9 @@ impl<A> Deref for NonEmptyList<A> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Error)]
|
||||||
pub enum L10NError {
|
pub enum L10NError {
|
||||||
|
#[error("Unparsable Locale")]
|
||||||
UnparsableLocale,
|
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.
|
// Potential Message structure.
|
||||||
//
|
//
|
||||||
// Let's assume the application has an enumeration that implements Message. For each element of the
|
// 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
|
// 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.
|
// message, so I'm not even sure that I can automate this code generation.
|
||||||
pub trait Message {
|
pub trait Message {
|
||||||
fn localize(&self, bundle: &FluentErgo) -> String;
|
fn msgid(&self) -> &str;
|
||||||
|
fn args(&self) -> Option<FluentArgs>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct L10N {
|
pub struct L10N {
|
||||||
messages_root: std::path::PathBuf,
|
messages_root: std::path::PathBuf,
|
||||||
message_bundle: FluentErgo,
|
message_bundles: Vec<FluentBundle<FluentResource, intl_memoizer::concurrent::IntlLangMemoizer>>,
|
||||||
|
|
||||||
locales: NonEmptyList<Locale>,
|
locales: NonEmptyList<Locale>,
|
||||||
zone: chrono_tz::Tz,
|
zone: chrono_tz::Tz,
|
||||||
|
@ -94,31 +125,39 @@ impl L10N {
|
||||||
let english_phrases = FluentResource::try_new
|
let english_phrases = FluentResource::try_new
|
||||||
*/
|
*/
|
||||||
|
|
||||||
let message_bundle = {
|
let mut s = Self {
|
||||||
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 {
|
|
||||||
messages_root,
|
messages_root,
|
||||||
message_bundle,
|
message_bundles: vec![],
|
||||||
locales,
|
locales,
|
||||||
zone,
|
zone,
|
||||||
}
|
};
|
||||||
|
|
||||||
|
s.load_messages_from_file("en-US".to_owned()).unwrap();
|
||||||
|
|
||||||
|
s
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_messages_from_file(
|
fn load_messages_from_file(&mut self, locale: String) -> Result<(), FileLoadError> {
|
||||||
&mut self,
|
let langid: unic_langid::LanguageIdentifier = locale.parse()?;
|
||||||
locale: String,
|
|
||||||
path: &Path,
|
let mut path = self.messages_root.clone();
|
||||||
) -> Result<(), L10NError> {
|
path.push(locale);
|
||||||
unimplemented!()
|
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
|
// 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
|
// 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
|
// at compile time, as are their parameters. That implies an enumeration, with one element per
|
||||||
// message, and with each element knowing its parameters.
|
// message, and with each element knowing its parameters.
|
||||||
pub fn messages(&self) -> FluentErgo {
|
// pub fn messages(&self) -> Vec<FluentBundle<FluentResource>> {
|
||||||
self.message_bundle.clone()
|
// self.message_bundles.clone()
|
||||||
}
|
// }
|
||||||
|
|
||||||
pub fn tr(&self, message: impl Message) -> String {
|
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(
|
pub fn format_date_time_utc(
|
||||||
|
|
Loading…
Reference in New Issue