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

Add config file option to set preferred language #318

Closed
wants to merge 2 commits into from

Conversation

cjwcommuny
Copy link

This pull request enable users to write the following config:

[display]
language = "zh_CN"

to override the language.

Closes #251.

Copy link
Owner

@dbrgn dbrgn left a comment

Choose a reason for hiding this comment

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

Thanks! I left a few comments.

src/main.rs Outdated
Comment on lines 33 to 34
use std::iter::once;
use std::{env, process};
Copy link
Owner

Choose a reason for hiding this comment

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

Nit: Can you merge these imports for consistency?

use std::{env, iter::once, process};

Copy link
Author

Choose a reason for hiding this comment

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

Merged.

src/main.rs Show resolved Hide resolved
env_language.chain(once(env_lang)).collect()
} else {
env_language.collect()
};
Copy link
Owner

Choose a reason for hiding this comment

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

I don't think this logic conforms to https://github.com/tldr-pages/tldr/blob/main/CLIENT-SPECIFICATION.md#language anymore, because the fast-path (if env_lang.is_none() { return ... }) was removed.

Copy link
Author

Choose a reason for hiding this comment

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

Fixed.

Comment on lines +429 to +433
#[test]
fn cli_preference_order() {
let lang_list = get_languages(Some("it"), Some("cz"), Some("de"), Some("fr:cn"));
assert_eq!(lang_list, ["it", "en"])
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This deviates from current behavior: Running tldr --language de git-checkout does not display the English page (and there is no German page currently). However, this test here says that English pages should be used. This boils down to the changes in get_languages - when I first wrote this method, it was meant to exactly follow the spec regarding the whole $LANG and $LANGUAGE business. I will make another comment there to suggest how to structure it going forward

Comment on lines +205 to +210
/// Return language(s) with the following precedence:
///
/// 1. CLI language parameter (`-L`, `--language`)
/// 2. Language as set in the config file
/// 3. If the `$LANG` env var is set: `$LANGUAGE` (list separated by `:`) followed by `$LANG` (single language)
/// 4. Fallback: English
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is not exactly what we want - the "Fallback: English" should only apply to the environment variables, not to the CLI or config file setting. Hence I suggest the following:

  • Leave get_languages as is, maybe only rename it to get_languages_from_env or so and remove the current get_languages_from_env
  • Make a new function that takes args and config and tries args, then config and finally uses get_languages_from_env.
  • To allow for testing without setting environment variables, the new function should only redirect to another function with a signature like your current get_languages:
fn the_new_function_impl(
    command_line_lang: Option<&str>,
    config_lang: Option<&str>,
    env_lang: Option<&str>,
    env_language: Option<&str>,
) -> Vec<String> {

@niklasmohrin
Copy link
Collaborator

@cjwcommuny Do you want to continue working on this, or should we close this PR for now?

@cjwcommuny cjwcommuny closed this Jul 6, 2023
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.

Add config file option to set prefered language
3 participants