gettext-mcp 0.5.0

Model Context Protocol (MCP) server for managing GNU gettext .po/.pot translation files, with CRUD tools and an optional web UI.
Documentation
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
//! Glossary tool handlers.
//!
//! Tools:
//!
//! * `get_glossary` — return all preferred-translation terms for a
//!   `(source_locale, target_locale)` pair, optionally filtered by a
//!   case-insensitive substring matched against either the source term
//!   or the translation.
//! * `update_glossary` — upsert and/or delete terms for a locale pair.
//!   Reads the existing file (if any), applies the patch, and writes
//!   back through the [`FileStore`] so the write is atomic.
//!
//! The glossary file lives at `$GETTEXT_GLOSSARY_PATH`, falling back to
//! `glossary.json` in the current working directory. The path is
//! resolved at handler call time so a `cd` between tool invocations does
//! the obvious thing.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use tokio::sync::Mutex;

use crate::error::GettextError;
use crate::io::FileStore;
use crate::service::glossary as glossary_svc;
use crate::service::glossary::Glossary;
use crate::service::GettextStoreManager;

/// Environment variable that overrides the default glossary file path.
pub(crate) const GLOSSARY_PATH_ENV: &str = "GETTEXT_GLOSSARY_PATH";
/// Default glossary filename, relative to the process working directory.
pub(crate) const DEFAULT_GLOSSARY_FILENAME: &str = "glossary.json";

/// Resolve the glossary file path: `$GETTEXT_GLOSSARY_PATH` wins,
/// otherwise `./glossary.json` relative to the current working dir.
pub(crate) fn resolve_glossary_path() -> PathBuf {
    match std::env::var(GLOSSARY_PATH_ENV) {
        Ok(p) if !p.trim().is_empty() => PathBuf::from(p),
        _ => PathBuf::from(DEFAULT_GLOSSARY_FILENAME),
    }
}

/// Load the glossary at `path` via `store`. A missing file yields an
/// empty glossary (callers update_glossary then create it on write).
fn load_glossary(store: &dyn FileStore, path: &Path) -> Result<Glossary, GettextError> {
    if !store.exists(path) {
        return glossary_svc::parse_glossary(None);
    }
    let raw = store.read(path)?;
    glossary_svc::parse_glossary(Some(&raw))
}

/// Write the glossary back through `store`. Creates parent directories
/// if they are missing (mirrors what `FsFileStore` would need anyway).
fn save_glossary(
    store: &Arc<dyn FileStore>,
    path: &Path,
    data: &Glossary,
) -> Result<(), GettextError> {
    if let Some(parent) = path.parent() {
        if !parent.as_os_str().is_empty() && !store.exists(parent) {
            std::fs::create_dir_all(parent)?;
        }
    }
    let json = glossary_svc::serialize_glossary(data)?;
    store.write(path, &json)
}

#[derive(Debug, Clone, Default, Deserialize, Serialize, JsonSchema)]
pub struct GetGlossaryParams {
    /// Source locale (e.g. `"en"`).
    pub source_locale: String,
    /// Target locale (e.g. `"fr"`).
    pub target_locale: String,
    /// Optional case-insensitive substring filter. Matches either the
    /// source term or its translation.
    #[serde(default)]
    pub filter: Option<String>,
}

#[derive(Debug, Clone, Default, Deserialize, Serialize, JsonSchema)]
pub struct UpdateGlossaryParams {
    /// Source locale (e.g. `"en"`).
    pub source_locale: String,
    /// Target locale (e.g. `"fr"`).
    pub target_locale: String,
    /// Term → translation pairs to insert or overwrite. Pass an empty
    /// map to perform only deletions.
    #[serde(default)]
    pub entries: BTreeMap<String, String>,
    /// Source terms to remove from the locale pair. Missing terms are
    /// silently ignored.
    #[serde(default)]
    pub delete: Option<Vec<String>>,
}

pub(crate) async fn handle_get_glossary(
    manager: &GettextStoreManager,
    params: GetGlossaryParams,
) -> Result<Value, GettextError> {
    if params.source_locale.trim().is_empty() {
        return Err(GettextError::InvalidInput(
            "source_locale must not be empty".into(),
        ));
    }
    if params.target_locale.trim().is_empty() {
        return Err(GettextError::InvalidInput(
            "target_locale must not be empty".into(),
        ));
    }

    let path = resolve_glossary_path();
    let file_store = manager.file_store().clone();
    let path_for_load = path.clone();
    let glossary =
        tokio::task::spawn_blocking(move || load_glossary(file_store.as_ref(), &path_for_load))
            .await
            .map_err(|e| GettextError::Io(std::io::Error::other(e)))??;

    let entries = glossary_svc::get_entries(
        &glossary,
        &params.source_locale,
        &params.target_locale,
        params.filter.as_deref(),
    );
    let total = entries.len();

    Ok(json!({
        "source_locale": params.source_locale,
        "target_locale": params.target_locale,
        "entries": entries,
        "total": total,
        "path": path.to_string_lossy(),
    }))
}

pub(crate) async fn handle_update_glossary(
    manager: &GettextStoreManager,
    write_lock: &Mutex<()>,
    params: UpdateGlossaryParams,
) -> Result<Value, GettextError> {
    if params.source_locale.trim().is_empty() {
        return Err(GettextError::InvalidInput(
            "source_locale must not be empty".into(),
        ));
    }
    if params.target_locale.trim().is_empty() {
        return Err(GettextError::InvalidInput(
            "target_locale must not be empty".into(),
        ));
    }

    let path = resolve_glossary_path();
    let delete_list = params.delete.unwrap_or_default();
    let entries = params.entries;

    // Serialize all writes through the manager-owned lock so two MCP
    // clients can't race on a read-modify-write cycle.
    let _guard = write_lock.lock().await;

    let file_store = manager.file_store().clone();
    let source = params.source_locale.clone();
    let target = params.target_locale.clone();
    let path_for_task = path.clone();

    let (updated, deleted, total_entries_in_pair) =
        tokio::task::spawn_blocking(move || -> Result<(usize, usize, usize), GettextError> {
            let mut glossary = load_glossary(file_store.as_ref(), &path_for_task)?;
            let updated = glossary_svc::update_entries(&mut glossary, &source, &target, entries);
            let deleted = if delete_list.is_empty() {
                0
            } else {
                glossary_svc::delete_entries(&mut glossary, &source, &target, &delete_list)
            };
            save_glossary(&file_store, &path_for_task, &glossary)?;
            let total = glossary_svc::pair_total(&glossary, &source, &target);
            Ok((updated, deleted, total))
        })
        .await
        .map_err(|e| GettextError::Io(std::io::Error::other(e)))??;

    Ok(json!({
        "updated": updated,
        "deleted": deleted,
        "total_entries_in_pair": total_entries_in_pair,
        "source_locale": params.source_locale,
        "target_locale": params.target_locale,
        "path": path.to_string_lossy(),
    }))
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::sync::{Arc, Mutex as StdMutex};

    /// `GETTEXT_GLOSSARY_PATH` is process-global state. Hold this mutex
    /// for the lifetime of any test that mutates it so parallel tests
    /// don't read each other's overrides.
    static ENV_LOCK: StdMutex<()> = StdMutex::new(());

    struct EnvGuard {
        _inner: std::sync::MutexGuard<'static, ()>,
        previous: Option<String>,
    }

    impl EnvGuard {
        fn set(path: &Path) -> Self {
            let guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
            let previous = std::env::var(GLOSSARY_PATH_ENV).ok();
            std::env::set_var(GLOSSARY_PATH_ENV, path);
            Self {
                _inner: guard,
                previous,
            }
        }
    }

    impl Drop for EnvGuard {
        fn drop(&mut self) {
            match self.previous.take() {
                Some(v) => std::env::set_var(GLOSSARY_PATH_ENV, v),
                None => std::env::remove_var(GLOSSARY_PATH_ENV),
            }
        }
    }

    fn make_manager(base: &Path) -> Arc<GettextStoreManager> {
        // Use a directory as the base so absolute paths under it are
        // accepted by `validate_path` (the glossary tool doesn't actually
        // call validate_path, but using a real manager is closer to
        // production wiring than a hand-rolled mock).
        Arc::new(GettextStoreManager::new(Some(base.to_path_buf())))
    }

    #[tokio::test]
    async fn get_glossary_missing_file_returns_empty() {
        let dir = tempfile::TempDir::new().unwrap();
        let glossary_path = dir.path().join("glossary.json");
        let _env = EnvGuard::set(&glossary_path);

        let manager = make_manager(dir.path());
        let result = handle_get_glossary(
            &manager,
            GetGlossaryParams {
                source_locale: "en".into(),
                target_locale: "fr".into(),
                filter: None,
            },
        )
        .await
        .unwrap();

        assert_eq!(result["total"], 0);
        assert!(result["entries"].as_object().unwrap().is_empty());
        assert_eq!(result["source_locale"], "en");
        assert_eq!(result["target_locale"], "fr");
    }

    #[tokio::test]
    async fn update_then_get_roundtrip() {
        let dir = tempfile::TempDir::new().unwrap();
        let glossary_path = dir.path().join("glossary.json");
        let _env = EnvGuard::set(&glossary_path);

        let manager = make_manager(dir.path());
        let lock = Mutex::new(());

        let mut entries = BTreeMap::new();
        entries.insert("Settings".into(), "Paramètres".into());
        entries.insert("Cancel".into(), "Annuler".into());

        let update_result = handle_update_glossary(
            &manager,
            &lock,
            UpdateGlossaryParams {
                source_locale: "en".into(),
                target_locale: "fr".into(),
                entries,
                delete: None,
            },
        )
        .await
        .unwrap();

        assert_eq!(update_result["updated"], 2);
        assert_eq!(update_result["deleted"], 0);
        assert_eq!(update_result["total_entries_in_pair"], 2);
        assert!(glossary_path.exists(), "file must be written");

        let get_result = handle_get_glossary(
            &manager,
            GetGlossaryParams {
                source_locale: "en".into(),
                target_locale: "fr".into(),
                filter: None,
            },
        )
        .await
        .unwrap();

        assert_eq!(get_result["total"], 2);
        assert_eq!(get_result["entries"]["Settings"], "Paramètres");
        assert_eq!(get_result["entries"]["Cancel"], "Annuler");
    }

    #[tokio::test]
    async fn get_glossary_filter_matches_term_or_translation() {
        let dir = tempfile::TempDir::new().unwrap();
        let glossary_path = dir.path().join("glossary.json");
        let _env = EnvGuard::set(&glossary_path);

        let manager = make_manager(dir.path());
        let lock = Mutex::new(());

        let mut entries = BTreeMap::new();
        entries.insert("Settings".into(), "Einstellungen".into());
        entries.insert("Cancel".into(), "Abbrechen".into());
        entries.insert("Save".into(), "Speichern".into());

        handle_update_glossary(
            &manager,
            &lock,
            UpdateGlossaryParams {
                source_locale: "en".into(),
                target_locale: "de".into(),
                entries,
                delete: None,
            },
        )
        .await
        .unwrap();

        // Term match.
        let res = handle_get_glossary(
            &manager,
            GetGlossaryParams {
                source_locale: "en".into(),
                target_locale: "de".into(),
                filter: Some("set".into()),
            },
        )
        .await
        .unwrap();
        assert_eq!(res["total"], 1);
        assert!(res["entries"]["Settings"].as_str().is_some());

        // Translation match (case-insensitive).
        let res = handle_get_glossary(
            &manager,
            GetGlossaryParams {
                source_locale: "en".into(),
                target_locale: "de".into(),
                filter: Some("ABBRECH".into()),
            },
        )
        .await
        .unwrap();
        assert_eq!(res["total"], 1);
        assert!(res["entries"]["Cancel"].as_str().is_some());
    }

    #[tokio::test]
    async fn update_glossary_deletes_terms() {
        let dir = tempfile::TempDir::new().unwrap();
        let glossary_path = dir.path().join("glossary.json");
        let _env = EnvGuard::set(&glossary_path);

        let manager = make_manager(dir.path());
        let lock = Mutex::new(());

        let mut entries = BTreeMap::new();
        entries.insert("A".into(), "a".into());
        entries.insert("B".into(), "b".into());
        entries.insert("C".into(), "c".into());
        handle_update_glossary(
            &manager,
            &lock,
            UpdateGlossaryParams {
                source_locale: "en".into(),
                target_locale: "fr".into(),
                entries,
                delete: None,
            },
        )
        .await
        .unwrap();

        let result = handle_update_glossary(
            &manager,
            &lock,
            UpdateGlossaryParams {
                source_locale: "en".into(),
                target_locale: "fr".into(),
                entries: BTreeMap::new(),
                delete: Some(vec!["A".into(), "Missing".into()]),
            },
        )
        .await
        .unwrap();
        assert_eq!(result["updated"], 0);
        assert_eq!(result["deleted"], 1);
        assert_eq!(result["total_entries_in_pair"], 2);

        let get = handle_get_glossary(
            &manager,
            GetGlossaryParams {
                source_locale: "en".into(),
                target_locale: "fr".into(),
                filter: None,
            },
        )
        .await
        .unwrap();
        assert_eq!(get["total"], 2);
        assert!(get["entries"].get("A").is_none());
    }

    #[tokio::test]
    async fn locale_pairs_are_isolated() {
        let dir = tempfile::TempDir::new().unwrap();
        let glossary_path = dir.path().join("glossary.json");
        let _env = EnvGuard::set(&glossary_path);

        let manager = make_manager(dir.path());
        let lock = Mutex::new(());

        let mut en_fr = BTreeMap::new();
        en_fr.insert("Settings".into(), "Paramètres".into());
        handle_update_glossary(
            &manager,
            &lock,
            UpdateGlossaryParams {
                source_locale: "en".into(),
                target_locale: "fr".into(),
                entries: en_fr,
                delete: None,
            },
        )
        .await
        .unwrap();

        let mut en_de = BTreeMap::new();
        en_de.insert("Settings".into(), "Einstellungen".into());
        handle_update_glossary(
            &manager,
            &lock,
            UpdateGlossaryParams {
                source_locale: "en".into(),
                target_locale: "de".into(),
                entries: en_de,
                delete: None,
            },
        )
        .await
        .unwrap();

        let fr = handle_get_glossary(
            &manager,
            GetGlossaryParams {
                source_locale: "en".into(),
                target_locale: "fr".into(),
                filter: None,
            },
        )
        .await
        .unwrap();
        let de = handle_get_glossary(
            &manager,
            GetGlossaryParams {
                source_locale: "en".into(),
                target_locale: "de".into(),
                filter: None,
            },
        )
        .await
        .unwrap();
        assert_eq!(fr["entries"]["Settings"], "Paramètres");
        assert_eq!(de["entries"]["Settings"], "Einstellungen");
        assert_eq!(fr["total"], 1);
        assert_eq!(de["total"], 1);
    }

    #[tokio::test]
    async fn corrupt_glossary_json_surfaces_error() {
        let dir = tempfile::TempDir::new().unwrap();
        let glossary_path = dir.path().join("glossary.json");
        std::fs::write(&glossary_path, "{not valid json").unwrap();
        let _env = EnvGuard::set(&glossary_path);

        let manager = make_manager(dir.path());
        let res = handle_get_glossary(
            &manager,
            GetGlossaryParams {
                source_locale: "en".into(),
                target_locale: "fr".into(),
                filter: None,
            },
        )
        .await;
        assert!(res.is_err(), "corrupt JSON must surface as an error");
    }

    #[tokio::test]
    async fn rejects_empty_locale() {
        let dir = tempfile::TempDir::new().unwrap();
        let glossary_path = dir.path().join("glossary.json");
        let _env = EnvGuard::set(&glossary_path);
        let manager = make_manager(dir.path());
        let lock = Mutex::new(());

        let res = handle_get_glossary(
            &manager,
            GetGlossaryParams {
                source_locale: "  ".into(),
                target_locale: "fr".into(),
                filter: None,
            },
        )
        .await;
        assert!(res.is_err());

        let res = handle_update_glossary(
            &manager,
            &lock,
            UpdateGlossaryParams {
                source_locale: "en".into(),
                target_locale: "".into(),
                entries: BTreeMap::new(),
                delete: None,
            },
        )
        .await;
        assert!(res.is_err());
    }
}