xcstrings-mcp 0.4.0

MCP server for iOS/macOS .xcstrings localization file management
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
use crate::model::plural::required_plural_forms;
use crate::model::specifier::{FormatSpecifier, extract_specifiers};

use crate::model::translation::{CompletedTranslation, RejectedTranslation};
use crate::model::xcstrings::XcStringsFile;

/// Validate a batch of translations against the source file.
/// Returns a list of rejected translations with reasons.
pub fn validate_translations(
    file: &XcStringsFile,
    translations: &[CompletedTranslation],
) -> Vec<RejectedTranslation> {
    let mut rejected = Vec::new();

    for translation in translations {
        // Check 1: Key exists in file
        let entry = match file.strings.get(&translation.key) {
            Some(e) => e,
            None => {
                rejected.push(RejectedTranslation {
                    key: translation.key.clone(),
                    reason: "key not found in file".into(),
                });
                continue;
            }
        };

        // Check 2: shouldTranslate is true
        if !entry.should_translate {
            rejected.push(RejectedTranslation {
                key: translation.key.clone(),
                reason: "key is marked as shouldTranslate=false".into(),
            });
            continue;
        }

        // Check 3: Non-empty value (for simple translations)
        if translation.value.is_empty() && translation.plural_forms.is_none() {
            rejected.push(RejectedTranslation {
                key: translation.key.clone(),
                reason: "translation value is empty".into(),
            });
            continue;
        }

        // Check 4: Format specifier validation
        // Get source localization for specifier extraction
        let source_loc = entry
            .localizations
            .as_ref()
            .and_then(|locs| locs.get(&file.source_language));

        // Get source text — fall back to the key itself if no source localization exists
        let source_text = source_loc
            .and_then(|loc| loc.string_unit.as_ref())
            .map(|su| su.value.as_str())
            .unwrap_or(&translation.key);

        let source_specs = extract_specifiers(source_text);

        if let Some(plural_forms) = &translation.plural_forms {
            // Validate required plural forms are present
            let required = required_plural_forms(&translation.locale);
            for req in &required {
                let form_name = req.as_str().to_string();
                if !plural_forms.contains_key(&form_name) {
                    rejected.push(RejectedTranslation {
                        key: translation.key.clone(),
                        reason: format!("missing required plural form: {form_name}"),
                    });
                }
            }

            // Substitution plural forms use %arg placeholders, not format specifiers
            // from the parent string_unit (which contains %#@VAR@ markers).
            // Skip specifier validation for substitution translations.
            if translation.substitution_name.is_none() {
                // For plural keys where source has no string_unit but has plural variations,
                // extract specifiers from the first source plural form instead.
                let effective_source_specs = if source_specs.is_empty() {
                    source_loc
                        .and_then(|loc| loc.variations.as_ref())
                        .and_then(|v| v.plural.as_ref())
                        .and_then(|p| p.values().next())
                        .map(|var| extract_specifiers(&var.string_unit.value))
                        .unwrap_or_default()
                } else {
                    source_specs.clone()
                };

                // Validate specifiers in each plural form value
                for (form, value) in plural_forms {
                    let target_specs = extract_specifiers(value);
                    if let Some(reason) = check_specifier_mismatch(
                        &effective_source_specs,
                        &target_specs,
                        &translation.key,
                        Some(form),
                    ) {
                        rejected.push(reason);
                    }
                }
            }
        } else {
            // Simple translation — validate specifiers
            let target_specs = extract_specifiers(&translation.value);
            if let Some(reason) =
                check_specifier_mismatch(&source_specs, &target_specs, &translation.key, None)
            {
                rejected.push(reason);
            }
        }
    }

    rejected
}

fn check_specifier_mismatch(
    source_specs: &[FormatSpecifier],
    target_specs: &[FormatSpecifier],
    key: &str,
    plural_form: Option<&str>,
) -> Option<RejectedTranslation> {
    if source_specs.len() != target_specs.len() {
        let context = plural_form
            .map(|f| format!(" (plural form: {f})"))
            .unwrap_or_default();
        return Some(RejectedTranslation {
            key: key.to_string(),
            reason: format!(
                "format specifier count mismatch{context}: source has {}, translation has {}",
                source_specs.len(),
                target_specs.len()
            ),
        });
    }

    for (src, tgt) in source_specs.iter().zip(target_specs.iter()) {
        if !src.is_compatible_with(tgt) {
            let context = plural_form
                .map(|f| format!(" (plural form: {f})"))
                .unwrap_or_default();
            return Some(RejectedTranslation {
                key: key.to_string(),
                reason: format!(
                    "format specifier type mismatch{context}: source has {}, translation has {}",
                    src.raw, tgt.raw
                ),
            });
        }
    }

    None
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;

    use indexmap::IndexMap;

    use super::*;
    use crate::model::xcstrings::{
        Localization, StringEntry, StringUnit, TranslationState, XcStringsFile,
    };

    fn make_file(entries: Vec<(&str, StringEntry)>) -> XcStringsFile {
        XcStringsFile {
            source_language: "en".to_string(),
            strings: entries
                .into_iter()
                .map(|(k, v)| (k.to_string(), v))
                .collect(),
            version: "1.0".to_string(),
        }
    }

    fn simple_entry(source_value: &str) -> StringEntry {
        let mut localizations = IndexMap::new();
        localizations.insert(
            "en".to_string(),
            Localization {
                string_unit: Some(StringUnit {
                    state: TranslationState::Translated,
                    value: source_value.to_string(),
                }),
                variations: None,
                substitutions: None,
            },
        );
        StringEntry {
            extraction_state: None,
            should_translate: true,
            comment: None,
            localizations: Some(localizations),
        }
    }

    fn simple_translation(key: &str, locale: &str, value: &str) -> CompletedTranslation {
        CompletedTranslation {
            key: key.to_string(),
            locale: locale.to_string(),
            value: value.to_string(),
            plural_forms: None,
            substitution_name: None,
        }
    }

    #[test]
    fn test_valid_translation() {
        let file = make_file(vec![("greeting", simple_entry("Hello %@"))]);
        let translations = vec![simple_translation("greeting", "uk", "Привіт %@")];
        let rejected = validate_translations(&file, &translations);
        assert!(rejected.is_empty());
    }

    #[test]
    fn test_key_not_found() {
        let file = make_file(vec![("greeting", simple_entry("Hello"))]);
        let translations = vec![simple_translation("missing_key", "uk", "Щось")];
        let rejected = validate_translations(&file, &translations);
        assert_eq!(rejected.len(), 1);
        assert!(rejected[0].reason.contains("key not found"));
    }

    #[test]
    fn test_should_not_translate() {
        let entry = StringEntry {
            extraction_state: None,
            should_translate: false,
            comment: None,
            localizations: None,
        };
        let file = make_file(vec![("api_key", entry)]);
        let translations = vec![simple_translation("api_key", "uk", "ключ")];
        let rejected = validate_translations(&file, &translations);
        assert_eq!(rejected.len(), 1);
        assert!(rejected[0].reason.contains("shouldTranslate=false"));
    }

    #[test]
    fn test_empty_value() {
        let file = make_file(vec![("greeting", simple_entry("Hello"))]);
        let translations = vec![simple_translation("greeting", "uk", "")];
        let rejected = validate_translations(&file, &translations);
        assert_eq!(rejected.len(), 1);
        assert!(rejected[0].reason.contains("empty"));
    }

    #[test]
    fn test_specifier_count_mismatch() {
        let file = make_file(vec![("msg", simple_entry("%@ has %d items"))]);
        let translations = vec![simple_translation("msg", "uk", "%@ має елементи")];
        let rejected = validate_translations(&file, &translations);
        assert_eq!(rejected.len(), 1);
        assert!(rejected[0].reason.contains("count mismatch"));
    }

    #[test]
    fn test_specifier_type_mismatch() {
        let file = make_file(vec![("msg", simple_entry("Hello %@"))]);
        let translations = vec![simple_translation("msg", "uk", "Привіт %d")];
        let rejected = validate_translations(&file, &translations);
        assert_eq!(rejected.len(), 1);
        assert!(rejected[0].reason.contains("type mismatch"));
    }

    #[test]
    fn test_missing_plural_form() {
        let file = make_file(vec![("items", simple_entry("%lld items"))]);
        let mut plural_forms = BTreeMap::new();
        plural_forms.insert("one".to_string(), "%lld елемент".to_string());
        plural_forms.insert("other".to_string(), "%lld елементів".to_string());
        // Missing "few" and "many" for Ukrainian

        let translations = vec![CompletedTranslation {
            key: "items".to_string(),
            locale: "uk".to_string(),
            value: String::new(),
            plural_forms: Some(plural_forms),
            substitution_name: None,
        }];

        let rejected = validate_translations(&file, &translations);
        assert!(rejected.iter().any(|r| r.reason.contains("few")));
        assert!(rejected.iter().any(|r| r.reason.contains("many")));
    }

    #[test]
    fn test_plural_only_key_specifier_validation() {
        // Source key has only plural variations (no string_unit) — specifiers
        // should be extracted from the first plural form value
        let mut localizations = IndexMap::new();
        localizations.insert(
            "en".to_string(),
            Localization {
                string_unit: None,
                variations: Some(crate::model::xcstrings::Variations {
                    plural: Some({
                        let mut plural = std::collections::BTreeMap::new();
                        plural.insert(
                            "one".to_string(),
                            crate::model::xcstrings::PluralVariation {
                                string_unit: StringUnit {
                                    state: TranslationState::Translated,
                                    value: "%lld item".to_string(),
                                },
                            },
                        );
                        plural.insert(
                            "other".to_string(),
                            crate::model::xcstrings::PluralVariation {
                                string_unit: StringUnit {
                                    state: TranslationState::Translated,
                                    value: "%lld items".to_string(),
                                },
                            },
                        );
                        plural
                    }),
                    device: None,
                }),
                substitutions: None,
            },
        );
        let entry = StringEntry {
            extraction_state: None,
            should_translate: true,
            comment: None,
            localizations: Some(localizations),
        };
        let file = make_file(vec![("items", entry)]);

        // Submit plural forms WITH correct specifier (%lld) — should pass
        let mut plural_forms_ok = std::collections::BTreeMap::new();
        plural_forms_ok.insert("one".to_string(), "%lld Artikel".to_string());
        plural_forms_ok.insert("other".to_string(), "%lld Artikel".to_string());

        let translations_ok = vec![CompletedTranslation {
            key: "items".to_string(),
            locale: "de".to_string(),
            value: String::new(),
            plural_forms: Some(plural_forms_ok),
            substitution_name: None,
        }];

        let rejected = validate_translations(&file, &translations_ok);
        assert!(
            rejected.is_empty(),
            "valid plural translation for plural-only source should not be rejected: {:?}",
            rejected
        );

        // Submit plural forms WITHOUT specifier — should be rejected
        let mut plural_forms_bad = std::collections::BTreeMap::new();
        plural_forms_bad.insert("one".to_string(), "Ein Artikel".to_string());
        plural_forms_bad.insert("other".to_string(), "Artikel".to_string());

        let translations_bad = vec![CompletedTranslation {
            key: "items".to_string(),
            locale: "de".to_string(),
            value: String::new(),
            plural_forms: Some(plural_forms_bad),
            substitution_name: None,
        }];

        let rejected = validate_translations(&file, &translations_bad);
        assert!(
            !rejected.is_empty(),
            "missing specifier in plural form should be rejected"
        );
        assert!(
            rejected.iter().any(|r| r.reason.contains("specifier")),
            "rejection should mention specifier mismatch"
        );
    }

    #[test]
    fn test_extra_plural_forms_ok() {
        let file = make_file(vec![("items", simple_entry("%lld items"))]);
        let mut plural_forms = BTreeMap::new();
        plural_forms.insert("one".to_string(), "%lld item".to_string());
        plural_forms.insert("other".to_string(), "%lld items".to_string());
        plural_forms.insert("zero".to_string(), "no items".to_string()); // extra for "en"

        let translations = vec![CompletedTranslation {
            key: "items".to_string(),
            locale: "en".to_string(),
            value: String::new(),
            plural_forms: Some(plural_forms),
            substitution_name: None,
        }];

        let rejected = validate_translations(&file, &translations);
        // "zero" has no specifier but source has %lld — that's a specifier mismatch, not a plural form issue
        // Filter to only plural-form rejections
        let plural_rejections: Vec<_> = rejected
            .iter()
            .filter(|r| r.reason.contains("missing required plural form"))
            .collect();
        assert!(plural_rejections.is_empty());
    }

    #[test]
    fn test_substitution_skips_specifier_validation() {
        // Source has %#@BIRDS@ substitution marker — NOT a format specifier.
        // Substitution plural forms use %arg, which is different.
        // Validator must skip specifier check when substitution_name is set.
        let file = make_file(vec![("bird", simple_entry("I saw %#@BIRDS@ in the park"))]);

        let mut plural_forms = BTreeMap::new();
        plural_forms.insert("one".to_string(), "%arg bird".to_string());
        plural_forms.insert("other".to_string(), "%arg birds".to_string());

        let translations = vec![CompletedTranslation {
            key: "bird".to_string(),
            locale: "de".to_string(),
            value: String::new(),
            plural_forms: Some(plural_forms),
            substitution_name: Some("BIRDS".to_string()),
        }];

        let rejected = validate_translations(&file, &translations);
        assert!(
            rejected.is_empty(),
            "substitution plural forms should not be rejected for specifier mismatch: {:?}",
            rejected
        );
    }
}