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