Skip to content

Commit b1511b2

Browse files
committed
fix: std i18n translator
1 parent f8ac776 commit b1511b2

File tree

46 files changed

+14091
-13912
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+14091
-13912
lines changed

crates/emmylua_code_analysis/src/resources/mod.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,20 @@ pub fn load_resource_std(
6060
}
6161

6262
fn remove_jit_resource(files: &mut Vec<LuaFileInfo>) {
63+
const JIT_FILES_TO_REMOVE: &[&str] = &[
64+
"jit.lua",
65+
"jit/profile.lua",
66+
"jit/util.lua",
67+
"string/buffer.lua",
68+
"table/clear.lua",
69+
"table/new.lua",
70+
"ffi.lua",
71+
];
6372
files.retain(|file| {
6473
let path = Path::new(&file.path);
65-
let should_remove = path.ends_with("jit.lua")
66-
|| path.ends_with("jit/profile.lua")
67-
|| path.ends_with("jit/util.lua")
68-
|| path.ends_with("string/buffer.lua")
69-
|| path.ends_with("table/clear.lua")
70-
|| path.ends_with("table/new.lua")
71-
|| path.ends_with("ffi.lua");
72-
73-
!should_remove
74+
!JIT_FILES_TO_REMOVE
75+
.iter()
76+
.any(|suffix| path.ends_with(suffix))
7477
});
7578
}
7679

@@ -177,7 +180,11 @@ fn walk_resource_dir(dir: &Dir, files: &mut Vec<LuaFileInfo>) {
177180
// 优先使用当前语言环境的 std-{locale} 目录, 否则回退到默认的 std 目录
178181
fn get_std_dir(resources_dir: &Path) -> PathBuf {
179182
let locale = get_locale_code(&rust_i18n::locale());
180-
Some(resources_dir.join(format!("std-{locale}")))
181-
.filter(|p| locale != "en" && p.exists())
182-
.unwrap_or_else(|| resources_dir.join("std"))
183+
if locale != "en" {
184+
let locale_dir = resources_dir.join(format!("std-{locale}"));
185+
if locale_dir.exists() {
186+
return locale_dir;
187+
}
188+
}
189+
resources_dir.join("std")
183190
}

crates/emmylua_ls/src/handlers/initialized/std_i18n.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ fn check_need_dump_std(resources_dir: &Path, locale: &str) -> bool {
118118
/// Params:
119119
/// - `locale` - 语言
120120
/// - `out_parent_dir` - 输出目录的父目录
121-
fn generate(locale: &str, out_parent_dir: &Path) -> (PathBuf, Vec<LuaFileInfo>) {
121+
fn generate(locale: &str, out_parent_dir: &Path) -> Vec<LuaFileInfo> {
122122
let origin_std_files = emmylua_code_analysis::load_resource_from_include_dir();
123123
let translate_std_root = out_parent_dir.join(format!("std-{locale}"));
124124
log::info!("Creating std-{locale} dir: {:?}", translate_std_root);
@@ -145,11 +145,7 @@ fn generate(locale: &str, out_parent_dir: &Path) -> (PathBuf, Vec<LuaFileInfo>)
145145
});
146146
}
147147

148-
// 写入版本文件,用于下次启动时判断是否需要重新生成
149-
let version_path = translate_std_root.join("version");
150-
let _ = std::fs::write(&version_path, VERSION);
151-
152-
(translate_std_root, out_files)
148+
out_files
153149
}
154150

155151
fn translate_one_std_file(locale: &str, rel_lua_path: &Path, content: &str) -> Option<String> {
@@ -385,14 +381,14 @@ mod tests {
385381
use super::generate;
386382

387383
#[test]
384+
#[ignore]
388385
fn test_generate_translated() {
389386
let test_output_dir = Path::new(env!("CARGO_MANIFEST_DIR"))
390387
.parent()
391388
.unwrap()
392389
.join("emmylua_code_analysis")
393390
.join("resources");
394-
let (root, files) = generate("zh_CN", &test_output_dir);
395-
assert!(root.exists());
391+
let files = generate("zh_CN", &test_output_dir);
396392
assert!(!files.is_empty());
397393
}
398394
}

0 commit comments

Comments
 (0)