sys_language
A cross-platform Rust library for detecting the system's default language with support for preferred language selection and fallback mechanisms.
Authors
- Cupnfish
- z.ai
Features
- 🌍 Cross-platform Support: Works on Windows, macOS, and Linux
- 🎯 Smart Matching: Supports exact matching and primary language matching
- ⚡ Flexible Configuration: Supports preferred language lists and fallback language settings
- 🔧 Easy to Use: Provides simple API and convenience functions
Installation
Add the dependency to your Cargo.toml:
[]
= "0.1.0"
Usage
Basic Usage
use detect_system_language;
let language = detect_system_language;
println!;
println!;
Advanced Usage
use LanguageDetector;
let detector = new
.with_preferred_languages
.with_fallback;
let language = detector.detect;
println!;
Using Convenience Functions
use detect_language_with_options;
let preferred = ;
let fallback = "en-US";
let language = detect_language_with_options;
println!;
Language Matching Rules
The library supports intelligent language matching, including:
- Exact Matching:
zh-CNexactly matcheszh-CN - Primary Language Matching:
zh-CNmatcheszh - Same Primary Language:
zh-CNmatcheszh-TW
use Language;
let lang = new;
assert!; // Exact match
assert!; // Primary language match
assert!; // Same primary language
assert!; // No match
Platform-Specific Implementations
Windows
Uses Windows API (GetUserDefaultLocaleName) to get system language settings.
macOS
Reads language settings from environment variables (LANG, LC_ALL, LC_CTYPE).
Linux
Reads language settings from environment variables (LANG, LC_ALL, LANGUAGE), with support for colon-separated language lists.
Example Program
Run the example program to see all features in action:
Example output:
=== System Language Detection Example ===
1. Detect system default language:
System language: zh-CN
Primary language: zh
Region: CN
2. Using LanguageDetector:
Detected language: zh-CN
Preferred languages: ["zh-CN", "zh-TW", "en-US"]
Fallback language: en-US
3. Test language matching logic:
Case 1 (preferred: zh-CN, en-US): zh-CN
Case 2 (preferred: zh-CN, ja-JP): zh-CN
Case 3 (no preferred languages): fr-FR
4. Using convenience functions:
Preferred: ["de-DE", "fr-FR", "es-ES"], Fallback: en-US
Result: de-DE
5. Language matching rules demonstration:
'zh-CN' matches 'zh-CN'? true
'zh-CN' matches 'zh'? true
'zh-CN' matches 'zh-TW'? true
'zh-CN' matches 'en-US'? false
=== Example End ===
API Documentation
Language Struct
Methods:
new(code: &str) -> Language- Create a new language instanceas_str(&self) -> &str- Get the language code as a stringprimary_language(&self) -> &str- Get the primary language partregion(&self) -> Option<&str>- Get the region partmatches(&self, other: &str) -> bool- Check if it matches the specified language
LanguageDetector Struct
Methods:
new() -> Self- Create a new language detectorwith_preferred_languages(self, languages: &[&str]) -> Self- Set preferred language listwith_fallback(self, fallback: &str) -> Self- Set fallback languagepreferred_languages(&self) -> &[String]- Get preferred language listfallback(&self) -> &str- Get fallback languagedetect(&self) -> Language- Detect and return the best language
Convenience Functions
detect_system_language() -> Language- Quick system language detectiondetect_language_with_options(preferred: &[&str], fallback: &str) -> Language- Detect language with options
License
MIT License
Contributing
Issues and Pull Requests are welcome!
Changelog
v0.1.0
- Initial release
- Support for Windows, macOS, and Linux platforms
- Basic language detection and matching functionality
- Flexible configuration options