1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//! Internationalization (i18n) module for Pacsea.
//!
//! This module provides locale detection, resolution, loading, and translation lookup.
//!
//! # Overview
//!
//! The i18n system supports:
//! - **Locale Detection**: Auto-detects system locale from environment variables (`LANG`, `LC_ALL`, `LC_MESSAGES`)
//! - **Locale Resolution**: Resolves locale with fallback chain (settings -> system -> default)
//! - **Fallback Chain**: Supports locale fallbacks (e.g., `de-CH` -> `de-DE` -> `en-US`)
//! - **Translation Loading**: Loads YAML locale files from `locales/` directory
//! - **Translation Lookup**: Provides `t()`, `t_fmt()`, and `t_fmt1()` helpers for translation access
//!
//! # Locale Files
//!
//! Locale files are stored in `locales/{locale}.yml` (e.g., `locales/en-US.yml`, `locales/de-DE.yml`).
//! Each file contains a nested YAML structure that is flattened into dot-notation keys:
//!
//! ```yaml
//! app:
//! titles:
//! search: "Search"
//! ```
//!
//! This becomes accessible as `app.titles.search`.
//!
//! # Configuration
//!
//! The i18n system is configured via `config/i18n.yml`:
//! - `default_locale`: Default locale if auto-detection fails (usually `en-US`)
//! - `fallbacks`: Map of locale codes to their fallback locales
//!
//! # Usage
//!
//! ```rust,no_run
//! use pacsea::i18n;
//! use pacsea::state::AppState;
//!
//! # let mut app = AppState::default();
//! // Simple translation lookup
//! let text = i18n::t(&app, "app.titles.search");
//!
//! // Translation with format arguments
//! let file_path = "/path/to/file";
//! let text = i18n::t_fmt1(&app, "app.toasts.exported_to", file_path);
//! ```
//!
//! # Adding a New Locale
//!
//! 1. Create `locales/{locale}.yml` (e.g., `locales/fr-FR.yml`)
//! 2. Copy structure from `locales/en-US.yml` and translate all strings
//! 3. Optionally add fallback in `config/i18n.yml` if needed (e.g., `fr: fr-FR`)
//! 4. Users can set `locale = fr-FR` in `settings.conf` or leave empty for auto-detection
//!
//! # Error Handling
//!
//! - Missing locale files fall back to English automatically
//! - Invalid locale codes in `settings.conf` trigger warnings and fallback to system/default
//! - Missing translation keys return the key itself (for debugging) and log debug messages
//! - All errors are logged but do not crash the application
pub use detect_system_locale;
pub use ;
pub use ;
pub use ;
use PathBuf;
/// What: Find a config file in development and installed locations.
///
/// Inputs:
/// - `relative_path`: Relative path from config directory (e.g., "i18n.yml")
///
/// Output:
/// - `Some(PathBuf)` pointing to the first existing file found, or `None` if not found
///
/// Details:
/// - Tries locations in order:
/// 1. Development location: `CARGO_MANIFEST_DIR/config/{relative_path}` (prioritized when running from source)
/// 2. Installed location: `/usr/share/pacsea/config/{relative_path}`
/// - Development location is checked first to allow working with repo files during development
/// What: Find the locales directory in development and installed locations.
///
/// Output:
/// - `Some(PathBuf)` pointing to the first existing locales directory found, or `None` if not found
///
/// Details:
/// - Tries locations in order:
/// 1. Development location: `CARGO_MANIFEST_DIR/config/locales` (prioritized when running from source)
/// 2. Installed location: `/usr/share/pacsea/locales`
/// - Development location is checked first to allow working with repo files during development
/// What: Get a translation for a given key from `AppState`.
///
/// Inputs:
/// - `app`: `AppState` containing translation maps
/// - `key`: Dot-notation key (e.g., "app.titles.search")
///
/// Output:
/// - Translated string, or the key itself if translation not found
///
/// Details:
/// - Uses translations from `AppState`
/// - Falls back to English if translation missing
/// What: Get a translation with format arguments.
///
/// Inputs:
/// - `app`: `AppState` containing translation maps
/// - `key`: Dot-notation key
/// - `args`: Format arguments (as Display trait objects)
///
/// Output:
/// - Formatted translated string
///
/// Details:
/// - Replaces placeholders in order: first {} gets first arg, etc.
/// - Supports multiple placeholders: "{} and {}" -> "arg1 and arg2"
/// What: Get a translation with a single format argument (convenience function).
///
/// Inputs:
/// - `app`: `AppState` containing translation maps
/// - `key`: Dot-notation key
/// - `arg`: Single format argument
///
/// Output:
/// - Formatted translated string
/// What: Format translated string with two format arguments.
///
/// Inputs:
/// - `app`: Application state (for locale access)
/// - `key`: Translation key
/// - `arg1`: First format argument
/// - `arg2`: Second format argument
///
/// Output:
/// - Formatted translated string