Skip to content

Draft: Gettext Localization System #351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 154 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ libc = "0.2"
regex = "1.10"
gettext-rs = { path = "./gettext-rs" }
errno = "0.3"
tr = { version = "0.1.10", default-features = false, features = ["gettext"]}
gettext = "0.4.0"
sys-locale = "0.3.1"

[workspace.lints]

4 changes: 4 additions & 0 deletions datetime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ clap.workspace = true
gettext-rs.workspace = true
chrono.workspace = true
libc.workspace = true
tr.workspace = true
gettext = "0.4.0"
sys-locale = "0.3.1"
fluent-langneg = "0.14.1"

[lints]
workspace = true
Expand Down
45 changes: 21 additions & 24 deletions datetime/cal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,22 @@

use chrono::Datelike;
use clap::Parser;
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
use plib::PROJECT_NAME;
use tr::tr;

#[derive(Parser)]
#[command(version, about = gettext("cal - print a calendar"))]
#[command(version, about = tr!("cal - print a calendar"))]
struct Args {
#[arg(
value_parser = clap::value_parser!(u32).range(1..),
help = gettext(
help = tr!(
"Specify the month to be displayed, represented as a decimal integer from 1 (January) to 12 (December)"
)
)]
month: Option<u32>,

#[arg(
value_parser = clap::value_parser!(u32).range(1..),
help = gettext(
help = tr!(
"Specify the year for which the calendar is displayed, represented as a decimal integer from 1 to 9999"
)
)]
Expand All @@ -38,23 +37,23 @@ struct Args {

fn print_month(month: u32, year: u32) {
let month_name = match month {
1 => gettext("January"),
2 => gettext("February"),
3 => gettext("March"),
4 => gettext("April"),
5 => gettext("May"),
6 => gettext("June"),
7 => gettext("July"),
8 => gettext("August"),
9 => gettext("September"),
10 => gettext("October"),
11 => gettext("November"),
12 => gettext("December"),
1 => tr!("January"),
2 => tr!("February"),
3 => tr!("March"),
4 => tr!("April"),
5 => tr!("May"),
6 => tr!("June"),
7 => tr!("July"),
8 => tr!("August"),
9 => tr!("September"),
10 => tr!("October"),
11 => tr!("November"),
12 => tr!("December"),
_ => unreachable!(),
};

println!("{} {}", month_name, year);
println!("{}", gettext("Su Mo Tu We Th Fr Sa"));
println!("{}", tr!("Su Mo Tu We Th Fr Sa"));

let mut day = 1;
let mut weekday = 1;
Expand Down Expand Up @@ -94,13 +93,11 @@ fn print_year(year: u32) {
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
plib::initialize_i18n!()?;

// parse command line arguments
let mut args = Args::parse();

setlocale(LocaleCategory::LcAll, "");
textdomain(PROJECT_NAME)?;
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;

// If no arguments are provided, display the current month
if args.month.is_none() && args.year.is_none() {
let now = chrono::Local::now();
Expand All @@ -116,7 +113,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let year = match args.year {
Some(year) => {
if year > 9999 {
return Err(gettext("year must be between 1 and 9999").into());
return Err(tr!("year must be between 1 and 9999").into());
}
year
}
Expand All @@ -125,7 +122,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

if let Some(month) = args.month {
if month > 12 {
return Err(gettext("month must be between 1 and 12").into());
return Err(tr!("month must be between 1 and 12").into());
}
print_month(month, year);
} else {
Expand Down
4 changes: 4 additions & 0 deletions plib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ rust-version.workspace = true
cfg-if = "1.0"
libc.workspace = true
errno.workspace = true
gettext = "0.4.0"
sys-locale = "0.3.1"
fluent-langneg = "0.14.1"
tr.workspace = true

[lints]
workspace = true
Expand Down
Loading
Loading