Skip to content
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 79 commits into from
Aug 8, 2024
Merged

System locale #5081

merged 79 commits into from
Aug 8, 2024

Conversation

ashu26jha
Copy link
Contributor

@ashu26jha ashu26jha commented Jun 19, 2024

Mentors: @robertbastian @zbraniecki

Total tasks:

  • Retrieval of system locale for MacOS, Windows, Linux
  • Calendar preferences
  • Time Zone for macos and windows
  • Convert String into icu4x components

Optional:

  • Number preferences
  • Currency preferences
  • Temperature
  • First Day of week

utils/system_locale/src/lib.rs Outdated Show resolved Hide resolved
utils/system_locale/src/linux.rs Outdated Show resolved Hide resolved
utils/system_locale/src/linux.rs Outdated Show resolved Hide resolved
utils/system_locale/src/linux.rs Outdated Show resolved Hide resolved
utils/system_locale/src/linux.rs Outdated Show resolved Hide resolved
utils/system_locale/src/lib.rs Outdated Show resolved Hide resolved
utils/system_locale/src/linux.rs Outdated Show resolved Hide resolved
utils/system_locale/src/linux.rs Outdated Show resolved Hide resolved
utils/system_locale/src/apple.rs Outdated Show resolved Hide resolved
utils/system_locale/src/windows.rs Outdated Show resolved Hide resolved
Cargo.toml Outdated
@@ -66,6 +66,7 @@ members = [
"utils/litemap",
"utils/pattern",
"utils/resb",
"utils/system_locale",
Copy link
Member

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"?

utils/system_locale/src/lib.rs Outdated Show resolved Hide resolved
utils/system_locale/src/linux.rs Outdated Show resolved Hide resolved
@ashu26jha ashu26jha marked this pull request as ready for review July 7, 2024 06:18
@ashu26jha ashu26jha requested a review from a team as a code owner July 7, 2024 06:18
Copy link
Member

@robertbastian robertbastian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are still a lot of unaddressed comments from my first review

utils/env_preferences/tests/test.rs Outdated Show resolved Hide resolved
tools/graveyard/Cargo.lock Outdated Show resolved Hide resolved
utils/env_preferences/src/apple.rs Outdated Show resolved Hide resolved
utils/env_preferences/src/linux.rs Outdated Show resolved Hide resolved
utils/system_locale/src/linux.rs Outdated Show resolved Hide resolved
utils/env_preferences/src/linux.rs Outdated Show resolved Hide resolved
utils/system_locale/src/linux.rs Outdated Show resolved Hide resolved
utils/system_locale/src/lib.rs Outdated Show resolved Hide resolved
utils/system_locale/src/lib.rs Outdated Show resolved Hide resolved
utils/system_locale/src/linux.rs Outdated Show resolved Hide resolved
@ashu26jha ashu26jha requested review from zbraniecki and sffc August 5, 2024 06:59
@ashu26jha
Copy link
Contributor Author

List of changes made:

  1. Converted return of APIs to Result
  2. Added tests for macos, linux and windows
  3. Added Time Zone support for macos and windows

// that the `lang_ptr` is not NULL thus making it safe to call
let length = unsafe { CFStringGetLength(ptr) as usize };

let mut c_str_buf = vec![0; length * 4];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: why is this length * 4? I think this should be length + 1: https://developer.apple.com/documentation/corefoundation/1542721-cfstringgetcstring#parameters

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*4 is an overkill 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's underkill and maybe UB if the string is empty.

utils/env_preferences/src/apple.rs Outdated Show resolved Hide resolved
utils/env_preferences/src/apple.rs Outdated Show resolved Hide resolved
utils/env_preferences/src/apple.rs Outdated Show resolved Hide resolved
@ashu26jha ashu26jha requested a review from robertbastian August 6, 2024 11:57
utils/env_preferences/src/apple.rs Outdated Show resolved Hide resolved
utils/env_preferences/src/apple.rs Outdated Show resolved Hide resolved
utils/env_preferences/src/error.rs Outdated Show resolved Hide resolved
utils/env_preferences/src/apple.rs Outdated Show resolved Hide resolved
zbraniecki
zbraniecki previously approved these changes Aug 6, 2024
Comment on lines +37 to +38
// Skipping "C" and those ending with "UTF-8", as they cannot be converted
// into the locale
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't they be converted into a Locale? Can they be converted into something else? Can we check that the strings are in a form that we expect, even if not a BCP-47 Locale?

Copy link
Contributor Author

@ashu26jha ashu26jha Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried, doing the following:

let loc_str = "C";
let locale: Locale = loc.parse.unwrap();

It fails

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converting to locale is a whole other can of worms, there will be strings like C, de_CH.UTF-8, etc. Let's not scope-creep this PR and stick with strings for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I've been clear about the line I draw given that this is unsafe code:

  1. All code paths must be covered
  2. All bytes resulting from calls to unsafe functions must be read
  3. The shape of the output should be asserted

I am relying on @robertbastian and @zbraniecki to verify condition 1 (coverage).

I guess condition 2 is covered in std::ffi::CStr::to_str.

For condition 3, if the strings are not valid BCP-47, then let's assert what shape they are. The bare minimum would be an assertion that they are ASCII, for example. But condition 3 wouldn't rise to the level of blocking the PR from landing in experimental.

Comment on lines 103 to 106
for calendar in calendar_res {
assert!(!calendar.0.is_empty(), "Couldn't retreive calendar locale");
assert!(!calendar.1.is_empty(), "Couldn't retreive calendar");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Testing nonempty is not sufficient. Please actually read the strings. Assert something about their shape. Maybe have a list of valid values? At the very least check that they are ASCII-alphabetic, for example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue with a list is, we are not sure about the people who are contributing to the icu4x, they could have some different locale and it would display failing test.

Check for ascii can be done

@ashu26jha ashu26jha requested a review from sffc August 7, 2024 05:06
@sffc sffc dismissed their stale review August 7, 2024 17:44

Testing is better; still room for improvement

@sffc
Copy link
Member

sffc commented Aug 7, 2024

I removed my blocking review; I will let @robertbastian or @zbraniecki land the PR.

@robertbastian robertbastian merged commit e1f7863 into unicode-org:main Aug 8, 2024
28 checks passed
@ashu26jha ashu26jha deleted the system-locale branch August 8, 2024 08:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants