From: Andrea Zagli Date: Fri, 29 Sep 2023 13:23:08 +0000 (+0200) Subject: Added i18n via gettext. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=21afa209cfc80e2a10a34311042ef7a055d07c1c;p=rust%2Fzakform Added i18n via gettext. --- diff --git a/.gitignore b/.gitignore index 28933b3..cee568b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target *.~*~ -examples/*.toml \ No newline at end of file +examples/*.toml +*.mo \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index c806d09..8529bff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -360,6 +360,12 @@ version = "3.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec" +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + [[package]] name = "bytes" version = "1.5.0" @@ -538,6 +544,70 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257" +[[package]] +name = "encoding" +version = "0.2.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b0d943856b990d12d3b55b359144ff341533e516d94098b1d3fc1ac666d36ec" +dependencies = [ + "encoding-index-japanese", + "encoding-index-korean", + "encoding-index-simpchinese", + "encoding-index-singlebyte", + "encoding-index-tradchinese", +] + +[[package]] +name = "encoding-index-japanese" +version = "1.20141219.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04e8b2ff42e9a05335dbf8b5c6f7567e5591d0d916ccef4e0b1710d32a0d0c91" +dependencies = [ + "encoding_index_tests", +] + +[[package]] +name = "encoding-index-korean" +version = "1.20141219.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4dc33fb8e6bcba213fe2f14275f0963fd16f0a02c878e3095ecfdf5bee529d81" +dependencies = [ + "encoding_index_tests", +] + +[[package]] +name = "encoding-index-simpchinese" +version = "1.20141219.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d87a7194909b9118fc707194baa434a4e3b0fb6a5a757c73c3adb07aa25031f7" +dependencies = [ + "encoding_index_tests", +] + +[[package]] +name = "encoding-index-singlebyte" +version = "1.20141219.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3351d5acffb224af9ca265f435b859c7c01537c0849754d3db3fdf2bfe2ae84a" +dependencies = [ + "encoding_index_tests", +] + +[[package]] +name = "encoding-index-tradchinese" +version = "1.20141219.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd0e20d5688ce3cab59eb3ef3a2083a5c77bf496cb798dc6fcdb75f323890c18" +dependencies = [ + "encoding_index_tests", +] + +[[package]] +name = "encoding_index_tests" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569" + [[package]] name = "encoding_rs" version = "0.8.33" @@ -623,6 +693,16 @@ dependencies = [ "wasi", ] +[[package]] +name = "gettext" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ebb594e753d5997e4be036e5a8cf048ab9414352870fb45c779557bbc9ba971" +dependencies = [ + "byteorder", + "encoding", +] + [[package]] name = "gimli" version = "0.28.0" @@ -1844,6 +1924,8 @@ dependencies = [ "actix-web", "chrono", "config", + "gettext", + "lazy_static", "serde", "serde_derive", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index ba6879a..ae49503 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,8 @@ serde_derive = "1" serde_json = "1" tera = "1.8.0" chrono = { version = "0.4" } +lazy_static = "1.4.0" +gettext = "0.4.0" [dev-dependencies] config = "0.13" diff --git a/po/it.po b/po/it.po new file mode 100644 index 0000000..c00c2b8 --- /dev/null +++ b/po/it.po @@ -0,0 +1,26 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2023-10-02 06:22+0000\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" + +#: src/validators.rs:53 +msgid "Mandatory field" +msgstr "Campo obbligatorio" + + +msgid "Invalid date" +msgstr "Data non valida" diff --git a/src/lib.rs b/src/lib.rs index f2ec9f0..2f5c589 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,3 +2,11 @@ pub mod filters; pub mod validators; pub mod fields; pub mod form; + +use lazy_static::lazy_static; + +use gettext::Catalog; + +lazy_static! { + static ref tr: gettext::Catalog = Catalog::parse(&include_bytes!("../po/it.mo")[..]).unwrap(); +} diff --git a/src/validators.rs b/src/validators.rs index 76f6939..b549acb 100644 --- a/src/validators.rs +++ b/src/validators.rs @@ -48,7 +48,7 @@ impl ValidatorNotEmpty { pub fn new(name: &str) -> Self { let mut v: ValidatorNotEmpty = Default::default(); v.validator.name = String::from(name); - v.validator.message = String::from("Mandatory field"); + v.validator.message = String::from(super::tr.gettext("Mandatory field")); v } @@ -128,7 +128,7 @@ impl ValidatorDate { pub fn new(name: &str) -> Self { let mut v: ValidatorDate = Default::default(); v.validator.name = String::from(name); - v.validator.message = String::from("Invalid date"); + v.validator.message = String::from(super::tr.gettext("Invalid date")); v }