Skip to content

Commit

Permalink
Localization support (#820)
Browse files Browse the repository at this point in the history
find locale set with [sys-locale](https://crates.io/crates/sys-locale),
then format with locales.
  • Loading branch information
scarf005 authored Mar 13, 2023
1 parent ea56a7c commit 7b9290c
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 41 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add `--system-protected` to include files with the Windows `system` flag set,
on other platform the same as `--all` [#752](https://github.com/Peltoche/lsd/issues/752)
- Add many icons from https://github.com/Peltoche/lsd/issues/764 [@TruncatedDinosour](https://ari-web.xyz/gh)
- Add support for localization from [scarf](https://github.com/scarf005)

### Fixed
- Do not quote filename when piping into another program from [TeamTamoad](https://github.com/TeamTamoad)
Expand Down
171 changes: 148 additions & 23 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ human-sort = "0.2.2"
term_grid = "0.1.*"
terminal_size = "0.1.*"
thiserror = "1.0"
chrono = "0.4.*"
sys-locale = "0.2.4"
once_cell = "1.17.1"
chrono = { version = "0.4.*", features = ["unstable-locales"] }
chrono-humanize = "0.1.*"
unicode-width = "0.1.*"
lscolors = "0.9.0"
Expand Down
16 changes: 10 additions & 6 deletions src/meta/date.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::locale::current_locale;
use crate::color::{ColoredString, Colors, Elem};
use crate::flags::{DateFlag, Flags};
use chrono::{DateTime, Duration, Local};
Expand Down Expand Up @@ -42,9 +43,11 @@ impl Date {
}

fn date_string(&self, flags: &Flags) -> String {
let locale = current_locale();

if let Date::Date(val) = self {
match &flags.date {
DateFlag::Date => val.format("%c").to_string(),
DateFlag::Date => val.format_localized("%c", locale).to_string(),
DateFlag::Relative => HumanTime::from(*val - Local::now()).to_string(),
DateFlag::Iso => {
// 365.2425 * 24 * 60 * 60 = 31556952 seconds per year
Expand All @@ -55,7 +58,7 @@ impl Date {
val.format("%F").to_string()
}
}
DateFlag::Formatted(format) => val.format(format).to_string(),
DateFlag::Formatted(format) => val.format_localized(format, locale).to_string(),
}
} else {
String::from('-')
Expand All @@ -68,6 +71,7 @@ mod test {
use super::Date;
use crate::color::{Colors, ThemeOption};
use crate::flags::{DateFlag, Flags};
use crate::meta::locale::current_locale;
use chrono::{DateTime, Duration, Local};
use crossterm::style::{Color, Stylize};
use std::io;
Expand All @@ -80,7 +84,7 @@ mod test {
Command::new("touch")
.arg("-t")
.arg(date.format("%Y%m%d%H%M.%S").to_string())
.arg(&path)
.arg(path)
.status()
}

Expand Down Expand Up @@ -127,7 +131,7 @@ mod test {

assert_eq!(
creation_date
.format("%c")
.format_localized("%c", current_locale())
.to_string()
.with(Color::AnsiValue(40)),
date.render(&colors, &flags)
Expand All @@ -154,7 +158,7 @@ mod test {

assert_eq!(
creation_date
.format("%c")
.format_localized("%c", current_locale())
.to_string()
.with(Color::AnsiValue(42)),
date.render(&colors, &flags)
Expand All @@ -181,7 +185,7 @@ mod test {

assert_eq!(
creation_date
.format("%c")
.format_localized("%c", current_locale())
.to_string()
.with(Color::AnsiValue(36)),
date.render(&colors, &flags)
Expand Down
Loading

0 comments on commit 7b9290c

Please sign in to comment.