locale-match
A small library for selecting the best match for user's preferred locales from available locales.
The library consists of two modules:
Both modules provide the best_matching_locale function.
Usage
To use locale-match in your project, add it to your Cargo.toml:
# Both modules are included by default
= "x.y.z"
# Include only the `bcp47` module
= { = "x.y.z", = false, = ["bcp47"] }
# Include only the `posix` module
= { = "x.y.z", = false, = ["posix"] }
Examples
BCP 47
use best_matching_locale;
let available_locales = ;
let user_locales = ;
let best_match = best_matching_locale;
// "ru-UA" is the best match for the highest-priority user locale "ru-RU"
assert_eq!;
let available_locales = ;
let user_locales = ;
let best_match = best_matching_locale;
// "pt-BR" is the first best match for the highest-priority user locale "pt"
assert_eq!;
let available_locales = ;
let user_locales = ;
let best_match = best_matching_locale;
// Empty extended language subtag in "zh-Hans" matches any extended language, e.g. "cmn"
assert_eq!;
POSIX
use best_matching_locale;
let available_locales = ;
let user_locales = ;
let best_match = best_matching_locale;
// "ru_UA" is the best match for the highest-priority user locale "ru_RU"
assert_eq!;
let available_locales = ;
let user_locales = ;
let best_match = best_matching_locale;
// "pt_BR" is the first best match for the highest-priority user locale "pt"
assert_eq!;
let available_locales = ;
let user_locales = ;
let best_match = best_matching_locale;
// Empty territory in "fr.UTF-8" matches any territory, e.g. "CA"
assert_eq!;
License
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this program. If not, see https://www.gnu.org/licenses/.
Copyright
© 2024 Petr Aleksandrovich Sabanov
Metrics