Skip to content

Commit 7b9290c

Browse files
authored
Localization support (lsd-rs#820)
find locale set with [sys-locale](https://crates.io/crates/sys-locale), then format with locales.
1 parent ea56a7c commit 7b9290c

File tree

12 files changed

+189
-41
lines changed

12 files changed

+189
-41
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Add `--system-protected` to include files with the Windows `system` flag set,
1313
on other platform the same as `--all` [#752](https://github.com/Peltoche/lsd/issues/752)
1414
- Add many icons from https://github.com/Peltoche/lsd/issues/764 [@TruncatedDinosour](https://ari-web.xyz/gh)
15+
- Add support for localization from [scarf](https://github.com/scarf005)
1516

1617
### Fixed
1718
- Do not quote filename when piping into another program from [TeamTamoad](https://github.com/TeamTamoad)

Cargo.lock

Lines changed: 148 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ human-sort = "0.2.2"
2828
term_grid = "0.1.*"
2929
terminal_size = "0.1.*"
3030
thiserror = "1.0"
31-
chrono = "0.4.*"
31+
sys-locale = "0.2.4"
32+
once_cell = "1.17.1"
33+
chrono = { version = "0.4.*", features = ["unstable-locales"] }
3234
chrono-humanize = "0.1.*"
3335
unicode-width = "0.1.*"
3436
lscolors = "0.9.0"

src/meta/date.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::locale::current_locale;
12
use crate::color::{ColoredString, Colors, Elem};
23
use crate::flags::{DateFlag, Flags};
34
use chrono::{DateTime, Duration, Local};
@@ -42,9 +43,11 @@ impl Date {
4243
}
4344

4445
fn date_string(&self, flags: &Flags) -> String {
46+
let locale = current_locale();
47+
4548
if let Date::Date(val) = self {
4649
match &flags.date {
47-
DateFlag::Date => val.format("%c").to_string(),
50+
DateFlag::Date => val.format_localized("%c", locale).to_string(),
4851
DateFlag::Relative => HumanTime::from(*val - Local::now()).to_string(),
4952
DateFlag::Iso => {
5053
// 365.2425 * 24 * 60 * 60 = 31556952 seconds per year
@@ -55,7 +58,7 @@ impl Date {
5558
val.format("%F").to_string()
5659
}
5760
}
58-
DateFlag::Formatted(format) => val.format(format).to_string(),
61+
DateFlag::Formatted(format) => val.format_localized(format, locale).to_string(),
5962
}
6063
} else {
6164
String::from('-')
@@ -68,6 +71,7 @@ mod test {
6871
use super::Date;
6972
use crate::color::{Colors, ThemeOption};
7073
use crate::flags::{DateFlag, Flags};
74+
use crate::meta::locale::current_locale;
7175
use chrono::{DateTime, Duration, Local};
7276
use crossterm::style::{Color, Stylize};
7377
use std::io;
@@ -80,7 +84,7 @@ mod test {
8084
Command::new("touch")
8185
.arg("-t")
8286
.arg(date.format("%Y%m%d%H%M.%S").to_string())
83-
.arg(&path)
87+
.arg(path)
8488
.status()
8589
}
8690

@@ -127,7 +131,7 @@ mod test {
127131

128132
assert_eq!(
129133
creation_date
130-
.format("%c")
134+
.format_localized("%c", current_locale())
131135
.to_string()
132136
.with(Color::AnsiValue(40)),
133137
date.render(&colors, &flags)
@@ -154,7 +158,7 @@ mod test {
154158

155159
assert_eq!(
156160
creation_date
157-
.format("%c")
161+
.format_localized("%c", current_locale())
158162
.to_string()
159163
.with(Color::AnsiValue(42)),
160164
date.render(&colors, &flags)
@@ -181,7 +185,7 @@ mod test {
181185

182186
assert_eq!(
183187
creation_date
184-
.format("%c")
188+
.format_localized("%c", current_locale())
185189
.to_string()
186190
.with(Color::AnsiValue(36)),
187191
date.render(&colors, &flags)

0 commit comments

Comments
 (0)