-
Notifications
You must be signed in to change notification settings - Fork 183
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
System locale #5081
Merged
Merged
System locale #5081
Changes from 1 commit
Commits
Show all changes
79 commits
Select commit
Hold shift + click to select a range
1c7b634
Linux support added
ashu26jha f6b96a9
improved interface of linux
ashu26jha df361b8
calendar for linux
ashu26jha a3d8154
added readme
ashu26jha bb95d79
apple locale and calendar
ashu26jha ee23fd2
windows locale retrieval
ashu26jha 1b1b514
windows calendar added
ashu26jha 9b3d8e5
Update Readme
ashu26jha 0adf4b9
Fixed linux
ashu26jha fe54b79
apple fi
ashu26jha d7ca69e
Minor fix
ashu26jha 1c10d33
Change module name
ashu26jha c243451
rustdocs
ashu26jha 0aa332e
readme license name fix
ashu26jha e96b2d3
Rename Readme.md to README.md
ashu26jha f7212b5
Change crate name
ashu26jha aba5c24
Added comment for calendar in linux.rs
ashu26jha 72c0404
Re-export modules
ashu26jha 6dc1973
Fix bug in locale retrieval
ashu26jha 7cc7c65
Added enum in linux.rs
ashu26jha 4c594be
tools/graveyard/Cargo.lock removed
ashu26jha 514f4a0
fmt
ashu26jha 6fd9d43
Added safety comments
ashu26jha 3534091
Refactor linux calendar
ashu26jha 62fe2c5
Fixed macos suffixes
ashu26jha efd5263
Added safety comments, changed function name to plural
ashu26jha f59808e
fmt
ashu26jha ad1a293
Windows fix
ashu26jha 68c8775
removed cfg_if
ashu26jha e072f7f
added ergonomic names in enum
ashu26jha 1a786f9
one unsafe function call per block
ashu26jha d73f432
added safety comments for apple.rs
ashu26jha 489c6e6
added safety comments for linux.rs
ashu26jha 52a6bc9
remove .clone()
ashu26jha 87dcefc
Release memory
ashu26jha 00efc98
cargo fmt
ashu26jha 3f41cd9
suggestion: remove mod wrapper
ashu26jha 6be1eae
suggestion: added result type in linux.rs
ashu26jha de9e217
suggestion: refactor to remove .clone()
ashu26jha daa9ddb
remove test, will be added in further commits
ashu26jha f1c9367
improved error format
ashu26jha c3f5da0
windows error propogation fixed
ashu26jha b5d5a38
remove unnecessary enum in linux.rs
ashu26jha 8a4eea4
suggestion: improve error propagation macos
ashu26jha c31695f
updated linux.rs to new error enum
ashu26jha 66eacc6
remove unnecessary return in linux.rs
ashu26jha 60c4c58
impl from UTF8Error
ashu26jha a3d7393
updated apple.rs error propagation
ashu26jha d348fc4
updated linux.rs error propagation
ashu26jha 3a07b75
linux.rs test covering get_locales
ashu26jha 8ace9df
corrected license
ashu26jha 8b71b4d
added calendar test for linux
ashu26jha bc2987a
clippy fix
ashu26jha af6c742
fix: linux test assert statements
ashu26jha ae87b00
added apple tests
ashu26jha 835cf07
linux test changes
ashu26jha 439de02
change error enum to test in CI
ashu26jha fdf3b45
error log ci
ashu26jha c86f7b4
remove return
ashu26jha a1fe396
handle lang_str NULL case
ashu26jha 668245f
added safety comments and remove null chars
ashu26jha e0451f9
fix apple calendar
ashu26jha a40e2c5
refactor apple.rs
ashu26jha 549b9a5
add timezone for apple
ashu26jha b45216f
added safety comments
ashu26jha 52a491a
added timezone windows.rs
ashu26jha 6865a0b
added windows rs tests
ashu26jha fdbd74f
optimise: get_string()
ashu26jha f51575c
error propagation improve
ashu26jha 5586b47
change order of enum
ashu26jha f4bd006
added comments for get_string()
ashu26jha 1e3a9fb
refactor: get_string()
ashu26jha 82aaf5a
error.rs and variable name changes
ashu26jha 5d4482b
update comment
ashu26jha feed347
suggestion: use NullLocale instead of NullPointer
ashu26jha edc9855
remove nullptr error enum
ashu26jha d9f65d7
updated tests
ashu26jha de77db0
ci fix: remove into_iter()
ashu26jha 8f3a6c0
nit: fixed ascii assertion in macos test
ashu26jha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Linux support added
- Loading branch information
commit 1c7b634cbe7824c1aabfcf0b89511347166e2f17
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "system_locale" | ||
version.workspace = true | ||
rust-version.workspace = true | ||
authors.workspace = true | ||
edition.workspace = true | ||
repository.workspace = true | ||
homepage.workspace = true | ||
license.workspace = true | ||
categories.workspace = true | ||
include.workspace = true | ||
|
||
[dependencies] | ||
locale = "0.2.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#[cfg(target_os = "linux")] | ||
mod linux_locale { | ||
use locale::{ | ||
self, | ||
linux::ffi::{ | ||
LC_ADDRESS, LC_ALL, LC_COLLATE, LC_CTYPE, LC_IDENTIFICATION, LC_MEASUREMENT, | ||
LC_MESSAGES, LC_MONETARY, LC_NAME, LC_NUMERIC, LC_PAPER, LC_TELEPHONE, LC_TIME, | ||
}, | ||
}; | ||
use std::{collections::HashMap, ffi::CStr, ptr::null}; | ||
|
||
pub unsafe fn fetch_locale_settings() -> HashMap<i32, String> { | ||
let locale_ptr = locale::linux::ffi::setlocale(LC_ALL, null()); | ||
let c_str: &CStr = CStr::from_ptr(locale_ptr as *const u8); | ||
let mut locale_map = HashMap::new(); | ||
|
||
if let Ok(str_slice) = c_str.to_str() { | ||
if str_slice.contains(';') { | ||
for part in str_slice.split(';') { | ||
let mut splitted = part.split('='); | ||
if let (Some(key), Some(value)) = (splitted.next(), splitted.next()) { | ||
let key_constant = match key { | ||
"LC_CTYPE" => LC_CTYPE, | ||
"LC_NUMERIC" => LC_NUMERIC, | ||
"LC_TIME" => LC_TIME, | ||
"LC_COLLATE" => LC_COLLATE, | ||
"LC_MONETARY" => LC_MONETARY, | ||
"LC_MESSAGES" => LC_MESSAGES, | ||
"LC_PAPER" => LC_PAPER, | ||
"LC_NAME" => LC_NAME, | ||
"LC_ADDRESS" => LC_ADDRESS, | ||
"LC_TELEPHONE" => LC_TELEPHONE, | ||
"LC_MEASUREMENT" => LC_MEASUREMENT, | ||
"LC_IDENTIFICATION" => LC_IDENTIFICATION, | ||
_ => LC_ALL, | ||
}; | ||
locale_map.insert(key_constant, value.to_string()); | ||
} | ||
} | ||
} else { | ||
locale_map.insert(LC_ALL, str_slice.to_string()); | ||
} | ||
} | ||
|
||
locale_map | ||
} | ||
} | ||
|
||
#[cfg(target_os = "linux")] | ||
pub use linux_locale::fetch_locale_settings; | ||
|
||
#[cfg(not(target_os = "linux"))] | ||
pub fn fetch_locale_settings() -> ! { | ||
panic!("Only linux support for now"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// tests/locale_parser.rs | ||
#[cfg(target_os = "linux")] | ||
mod tests { | ||
use system_locale::fetch_locale_settings; | ||
|
||
#[test] | ||
fn test_fetch_locale_settings() { | ||
let locale_map = unsafe { fetch_locale_settings() }; | ||
// More meaningful test need to be added | ||
assert!(locale_map.contains_key(&locale::linux::ffi::LC_ALL)); | ||
} | ||
} | ||
|
||
#[cfg(not(target_os = "linux"))] | ||
mod tests { | ||
#[test] | ||
#[should_panic(expected = "This function is only supported on Linux.")] | ||
fn test_fetch_locale_settings_not_supported() { | ||
locale_parser::fetch_locale_settings(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This name suggests that the crate only retrieves "system" "locale". My understanding is that your crate aims to retrieve environment information from the runtime. I'd use a term like "environment", "preferences" etc.
My concern about the "system" is that it indicates that what you get is "OS Locale" - but you may not. Increasingly OSes sandbox applications, so OS may be in English, but the application may only see that the language of its own environment is French.
My concern about "locale" is that it indicates that all you get is a Unicode Locale - while what you actually get are things beyond that. For example, short date pattern is not part of a Locale but should be retrievable by this crate.
Maybe "utils/env_preferences"?