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

Suggestion: select language from set of available languages #13

Open
atezet opened this issue Aug 8, 2024 · 3 comments
Open

Suggestion: select language from set of available languages #13

atezet opened this issue Aug 8, 2024 · 3 comments

Comments

@atezet
Copy link

atezet commented Aug 8, 2024

Similar to intersection it would be useful to be able to just select one from a list of available languages. Also, I think it would be useful if it would be possible to match en-US and en-GB with en in the available languages, for projects that don't support regional versions of languages. Some sort of "lossy" select.

@mike-engel
Copy link
Owner

Hi @atezet, thanks for the suggestion! I'm curious how you think this would work. Would it be random? Just the first in a list? If you could design the API, what would it look like?

@atezet
Copy link
Author

atezet commented Aug 22, 2024

Thanks for your reply! I'll try to write it up somewhere next week if that's okay

@atezet
Copy link
Author

atezet commented Sep 1, 2024

So, I think neither of my suggestions should be random, but either quality-based or order-based. The select/select_with_quality should have the same API as the intersection, but always just return one result. So something along the lines of:

pub fn select(raw_languages: &str, supported_languages: &[&str]) -> Option<String> {
    let user_languages = parse(raw_languages);
    user_languages
        .into_iter()
        .find(|l| supported_languages.contains(&l.as_str()))
}

I'll submit it as a PR later, if this is okay.

The select_lossy is a tad more complicated, as the regions aren't split from the language in the current type. but let me try:

/// `supported_languages` is an array of languages without region
pub fn select_lossy(raw_languages: &str, supported_languages: &[&str]) -> Option<String> {
    let user_languages = parse(raw_languages);
    user_languages
        .into_iter()
        .find(|l| supported_languages.contains(&ls.split('-').next().unwrap()).as_str())
}

While writing this, I realised it's type-wise probably nicer to have the region split off and put it in its own field. That's a pretty major change, but I'd be willing to help if you're up for it. I always like it if crates provide nice types to interface with the functionality they provide.

Disclaimer: I wrote both of these in the comment field of Github. The unwrap is based on the assumption that user_languages doesn't contain any empty strings, but I don't know that for sure.

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

No branches or pull requests

2 participants