Skip to main content

Library/Fn/NLS/
mod.rs

1//! NLS (National Language Support) processing for VSCode localization
2//!
3//! This module handles:
4//! - Extraction of localization keys from TypeScript source
5//! - Generation of localization bundle files
6//! - Replacement of localization keys with actual strings at build time
7
8#[path = "Extract.rs"]
9pub mod Extract;
10
11#[path = "Replace.rs"]
12pub mod Replace;
13
14#[path = "Bundle.rs"]
15pub mod Bundle;
16
17pub use Extract::NLSExtractor;
18pub use Replace::NLSReplacer;
19pub use Bundle::NLSBundle;
20
21/// Configuration for NLS processing
22#[derive(Debug, Clone, Default)]
23pub struct NLSConfig {
24	/// Source language for localization (default: "en")
25	pub source_lang:String,
26
27	/// Output directory for generated localization files
28	pub output_dir:String,
29
30	/// Whether to inline translations into the output
31	pub inline:bool,
32
33	/// File pattern for localization keys (default: "*.nls.*")
34	pub key_pattern:String,
35
36	/// Additional languages to generate
37	pub languages:Vec<String>,
38}
39
40impl NLSConfig {
41	pub fn new() -> Self {
42		Self {
43			source_lang:"en".to_string(),
44
45			output_dir:"out/nls".to_string(),
46
47			inline:false,
48
49			key_pattern:"*.nls.*".to_string(),
50
51			languages:vec!["en".to_string()],
52		}
53	}
54}
55
56/// Represents a localization entry
57#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
58pub struct LocalizationEntry {
59	/// The key used to identify this string
60	pub key:String,
61
62	/// The localized string value
63	pub value:String,
64
65	/// Optional comment for translators
66	pub comment:Option<String>,
67}
68
69/// A collection of localization entries for a specific language
70#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
71pub struct LocalizationBundle {
72	/// Language code (e.g., "en", "de", "fr")
73	pub language:String,
74
75	/// Hash of the source strings for cache invalidation
76	pub hash:String,
77
78	/// The localization entries
79	pub entries:Vec<LocalizationEntry>,
80}
81
82impl LocalizationBundle {
83	pub fn new(language:&str) -> Self { Self { language:language.to_string(), hash:String::new(), entries:Vec::new() } }
84
85	pub fn add_entry(&mut self, key:impl Into<String>, value:impl Into<String>) {
86		self.entries
87			.push(LocalizationEntry { key:key.into(), value:value.into(), comment:None });
88	}
89
90	/// Generate a simple hash for cache invalidation
91	pub fn compute_hash(&mut self) {
92		use std::{
93			collections::hash_map::DefaultHasher,
94			hash::{Hash, Hasher},
95		};
96
97		let mut hasher = DefaultHasher::new();
98
99		for entry in &self.entries {
100			entry.key.hash(&mut hasher);
101
102			entry.value.hash(&mut hasher);
103		}
104
105		self.hash = format!("{:x}", hasher.finish());
106	}
107}