Repository: kellpossible/cargo-i18n (language: rust)

Issue:
[ERROR i18n_embed::requester] Unable to parse your locale when $LANG is not set
As reported in a project that depends on `i18n_embed` (https://github.com/str4d/rage/issues/156), running `DesktopLanguageRequester::requested_languages()` in a minimalist Docker environment (on Linux) where `$LANG` is not set causes the following error message to be logged.

```
[ERROR i18n_embed::requester] Unable to parse your locale: ParserError(InvalidLanguage)
```

This is in particular visible for all dependent programs of `i18n_embed` that use the `env_logger` mechanism for example.

However, this error message is somewhat misleading because it isn't actually fatal to the program (if I understood correctly, `requested_languages()` would simply return an empty `Vec`). I have various ideas of how this could be improved.

- Could `DesktopLanguageRequester` be extended with some option parameter (e.g. in a `new_with_options` constructor) to explicitly choose to gracefully fallback (i.e. without error message) if no locale could be found/parsed?
- Could the logged error message be more specific about which language was invalid and couldn't be parsed, and/or specify the source where a language was searched (e.g. which environment variable was read)?
- Could the case where `$LANG` is unset be handled in a more graceful manner - given that it's not really a "parsing" error from an "invalid language", and that this variable cannot always be expected to be set?

What do you think?
[ERROR i18n_embed::requester] Unable to parse your locale when $LANG is not set
As reported in a project that depends on `i18n_embed` (https://github.com/str4d/rage/issues/156), running `DesktopLanguageRequester::requested_languages()` in a minimalist Docker environment (on Linux) where `$LANG` is not set causes the following error message to be logged.

```
[ERROR i18n_embed::requester] Unable to parse your locale: ParserError(InvalidLanguage)
```

This is in particular visible for all dependent programs of `i18n_embed` that use the `env_logger` mechanism for example.

However, this error message is somewhat misleading because it isn't actually fatal to the program (if I understood correctly, `requested_languages()` would simply return an empty `Vec`). I have various ideas of how this could be improved.

- Could `DesktopLanguageRequester` be extended with some option parameter (e.g. in a `new_with_options` constructor) to explicitly choose to gracefully fallback (i.e. without error message) if no locale could be found/parsed?
- Could the logged error message be more specific about which language was invalid and couldn't be parsed, and/or specify the source where a language was searched (e.g. which environment variable was read)?
- Could the case where `$LANG` is unset be handled in a more graceful manner - given that it's not really a "parsing" error from an "invalid language", and that this variable cannot always be expected to be set?

What do you think?


Discussion / hints:
Hi @gendx thanks for reporting! I'll take a look at this over the next few days and hopefully get back to you on it soon
Hi @gendx thanks for reporting! I'll take a look at this over the next few days and hopefully get back to you on it soon

Fix patch:
diff --git a/i18n-embed/src/requester.rs b/i18n-embed/src/requester.rs
index d2f2c27..cc021e5 100644
--- a/i18n-embed/src/requester.rs
+++ b/i18n-embed/src/requester.rs
@@ -252,16 +252,33 @@ impl DesktopLanguageRequester {
 
         let ids: Vec<unic_langid::LanguageIdentifier> = current_locale
             .tags_for("messages")
-            .filter_map(|tag: LanguageRange<'_>| match tag.to_string().parse() {
-                Ok(tag) => Some(tag),
-                Err(err) => {
-                    log::error!("Unable to parse your locale: {:?}", err);
+            .filter_map(|tag: LanguageRange<'_>| {
+                let tag_string = tag.to_string();
+
+                if tag_string.is_empty() {
+                    log::debug!(
+                        "System has an empty requested locale for messages. \
+                    In this situation on Linux the `LANG` environment variable \
+                    may be empty."
+                    );
                     None
+                } else {
+                    match tag_string.parse() {
+                        Ok(tag) => Some(tag),
+                        Err(err) => {
+                            log::error!(
+                                "Unable to parse system's currently requested locale `{}`: {}",
+                                tag_string,
+                                err
+                            );
+                            None
+                        }
+                    }
                 }
             })
             .collect();
 
-        log::info!("Current Locale: {:?}", ids);
+        log::info!("System requested locales: {:?}", ids);
 
         ids
     }


Test patch: