-
Notifications
You must be signed in to change notification settings - Fork 24
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
[WJ-1191] Implement fallback locales #1669
Merged
+340
−54
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
2d43187
Add rustdoc to get_message().
emmiegit c15b393
Refactor translate() to accept multiple locales.
emmiegit d4144b2
Implement iterate_locale_fallbacks().
emmiegit 603f582
Deduplicate locale iteration for easier test cases.
emmiegit 043dae0
Remove locale deduplication in test.
emmiegit 2e138a2
Update docs for new skip usage.
emmiegit 3386835
Update tests.
emmiegit 16d07ef
Add for-loop comments.
emmiegit d09ae7c
Add error for no locales listed.
emmiegit 44d5c5e
Add TODO for later investigation.
emmiegit ef41f7f
Note discarding of locale in translate().
emmiegit 8ccf5cb
Pass in list of fallback locales for each use.
emmiegit c1d9f5d
Use more specific empty locale error.
emmiegit cba8641
Remove stray parenthese.
emmiegit ba7ebef
Suppress unused items in prelude modules.
emmiegit afdd819
Revert "Suppress unused items in prelude modules."
emmiegit 253ca95
Create clippy config.
emmiegit d452760
Suppress unused_imports warnings in clippy.
emmiegit 3d40343
Improve comment readability.
emmiegit fb626c1
Fix clippy invocation.
emmiegit 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
There are no files selected for viewing
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,2 @@ | ||
# Code smells | ||
disallowed-names = ["foo", "bar", "baz", "todo"] |
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
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,145 @@ | ||
/* | ||
* locales/fallback.rs | ||
* | ||
* DEEPWELL - Wikijump API provider and database manager | ||
* Copyright (C) 2019-2023 Wikijump Team | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
//! Module to implement locale fallbacks. | ||
//! | ||
//! This is different than having a list of locales and simply trying each one. | ||
//! Beyond that, this is another important component to finding a proper locale. | ||
//! | ||
//! Given some locale, it iterates through increasingly generic forms of it | ||
//! until a match can be found (or not). | ||
//! | ||
//! The order followed is: | ||
//! * Language, script, region, and variant (unmodified) | ||
//! * Language, script, and region | ||
//! * Language and script | ||
//! * Language, region, and variant | ||
//! * Language and region | ||
//! * Language only | ||
//! | ||
//! The logic here will skip a locale variant if it's already been outputted. | ||
//! So for a locale like `ko`, it will only emit one item, `ko`. For something like `en-CA`, | ||
//! it will emit `en-CA` then `en`. | ||
//! | ||
//! If `Some(_)` or `Err(_)` is returned, then iteration will end prematurely. | ||
|
||
use unic_langid::LanguageIdentifier; | ||
|
||
pub fn iterate_locale_fallbacks<F, T>( | ||
mut locale: LanguageIdentifier, | ||
mut f: F, | ||
) -> Option<(LanguageIdentifier, T)> | ||
where | ||
F: FnMut(&LanguageIdentifier) -> Option<T>, | ||
{ | ||
debug!("Iterating through locale fallbacks for {locale}"); | ||
|
||
macro_rules! try_iter { | ||
() => { | ||
if let Some(result) = f(&locale) { | ||
return Some((locale, result)); | ||
} | ||
}; | ||
} | ||
|
||
// Storage of temporarily removed fields. | ||
let variants: Vec<_> = locale.variants().cloned().collect(); | ||
|
||
// Unmodified locale | ||
// Language, script, region, variant | ||
try_iter!(); | ||
|
||
if !variants.is_empty() { | ||
// Remove variant | ||
// Language, script, region | ||
locale.clear_variants(); | ||
try_iter!(); | ||
} | ||
|
||
// Remove region | ||
// Language, script | ||
let region = locale.region.take(); | ||
if region.is_some() { | ||
try_iter!(); | ||
} | ||
|
||
if locale.script.take().is_some() { | ||
// Re-add region and variant, remove script | ||
// Language, region, variant | ||
locale.region = region; | ||
locale.set_variants(&variants); | ||
try_iter!(); | ||
|
||
if !variants.is_empty() { | ||
// Remove variant | ||
// Language, region | ||
locale.clear_variants(); | ||
try_iter!(); | ||
} | ||
|
||
if locale.region.is_some() { | ||
// Remove region | ||
// Language only | ||
locale.region = None; | ||
try_iter!(); | ||
} | ||
} | ||
|
||
// No results | ||
None | ||
} | ||
|
||
#[test] | ||
fn fallbacks() { | ||
fn check(locale: &str, expected: &[&str]) { | ||
let locale = locale.parse().expect("Unable to parse locale"); | ||
let mut actual = Vec::new(); | ||
|
||
iterate_locale_fallbacks::<_, ()>(locale, |locale| { | ||
actual.push(str!(locale)); | ||
None | ||
}); | ||
|
||
assert!( | ||
actual.iter().eq(expected), | ||
"Actual fallback locale list doesn't match expected\nactual: {:?}\nexpected: {:?}", | ||
actual, | ||
expected, | ||
); | ||
} | ||
|
||
check("en", &["en"]); | ||
check("fr-be", &["fr-BE", "fr"]); | ||
check("es-Latn", &["es-Latn", "es"]); | ||
check("en-Latn-US", &["en-Latn-US", "en-Latn", "en-US", "en"]); | ||
check("en-Valencia", &["en-valencia", "en"]); | ||
check("en_CA_valencia", &["en-CA-valencia", "en-CA", "en"]); | ||
check( | ||
"en_Latn-CA_valencia", | ||
&[ | ||
"en-Latn-CA-valencia", | ||
"en-Latn-CA", | ||
"en-Latn", | ||
"en-CA-valencia", | ||
"en-CA", | ||
"en", | ||
], | ||
); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
Forgot this was used here, I should move the helper function below out somewhere public so it can get imported and used here too.