Skip to main content

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