@@ -2,10 +2,10 @@ mod best_resource_path;
22
33use std:: path:: { Path , PathBuf } ;
44
5- use best_resource_path:: get_best_resources_dir;
5+ pub use best_resource_path:: get_best_resources_dir;
66use include_dir:: { Dir , DirEntry , include_dir} ;
77
8- use crate :: { LuaFileInfo , load_workspace_files} ;
8+ use crate :: { LuaFileInfo , get_locale_code , load_workspace_files} ;
99
1010static RESOURCE_DIR : Dir = include_dir ! ( "$CARGO_MANIFEST_DIR/resources" ) ;
1111const VERSION : & str = env ! ( "CARGO_PKG_VERSION" ) ;
@@ -14,13 +14,15 @@ pub fn load_resource_std(
1414 create_resources_dir : Option < String > ,
1515 is_jit : bool ,
1616) -> ( PathBuf , Vec < LuaFileInfo > ) {
17+ // 指定了输出的资源目录, 目前只有 lsp 会指定
1718 if let Some ( create_resources_dir) = create_resources_dir {
1819 let resource_path = if create_resources_dir. is_empty ( ) {
1920 get_best_resources_dir ( )
2021 } else {
2122 PathBuf :: from ( & create_resources_dir)
2223 } ;
23- let std_dir = PathBuf :: from ( & resource_path) . join ( "std" ) ;
24+ // 此时会存在 i18n, 我们需要根据当前语言环境切换到对应语言的 std 目录
25+ let std_dir = get_std_dir ( & resource_path) ;
2426 let result = load_resource_from_file_system ( & resource_path) ;
2527 if let Some ( mut files) = result {
2628 if !is_jit {
@@ -29,7 +31,7 @@ pub fn load_resource_std(
2931 return ( std_dir, files) ;
3032 }
3133 }
32-
34+ // 没有指定资源目录, 那么直接使用默认的资源目录, 此时不会存在 i18n
3335 let resoucres_dir = get_best_resources_dir ( ) ;
3436 let std_dir = resoucres_dir. join ( "std" ) ;
3537 let files = load_resource_from_include_dir ( ) ;
@@ -60,19 +62,20 @@ pub fn load_resource_std(
6062fn remove_jit_resource ( files : & mut Vec < LuaFileInfo > ) {
6163 files. retain ( |file| {
6264 let path = Path :: new ( & file. path ) ;
63- let should_remove = path. ends_with ( "std/ jit.lua" )
64- || path. ends_with ( "std/ jit/profile.lua" )
65- || path. ends_with ( "std/ jit/util.lua" )
66- || path. ends_with ( "std/ string/buffer.lua" )
67- || path. ends_with ( "std/ table/clear.lua" )
68- || path. ends_with ( "std/ table/new.lua" )
69- || path. ends_with ( "std/ ffi.lua" ) ;
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" ) ;
7072
7173 !should_remove
7274 } ) ;
7375}
7476
7577fn load_resource_from_file_system ( resources_dir : & Path ) -> Option < Vec < LuaFileInfo > > {
78+ // lsp i18n 的资源在更早之前的 crates\emmylua_ls\src\handlers\initialized\std_i18n.rs 中写入到文件系统
7679 if check_need_dump_to_file_system ( ) {
7780 log:: info!( "Creating resources dir: {:?}" , resources_dir) ;
7881 let files = load_resource_from_include_dir ( ) ;
@@ -109,7 +112,7 @@ fn load_resource_from_file_system(resources_dir: &Path) -> Option<Vec<LuaFileInf
109112 }
110113 }
111114
112- let std_dir = resources_dir . join ( "std" ) ;
115+ let std_dir = get_std_dir ( & resources_dir ) ;
113116 let match_pattern = vec ! [ "**/*.lua" . to_string( ) ] ;
114117 let files = match load_workspace_files ( & std_dir, & match_pattern, & Vec :: new ( ) , & Vec :: new ( ) , None )
115118 {
@@ -146,7 +149,7 @@ fn check_need_dump_to_file_system() -> bool {
146149 false
147150}
148151
149- fn load_resource_from_include_dir ( ) -> Vec < LuaFileInfo > {
152+ pub fn load_resource_from_include_dir ( ) -> Vec < LuaFileInfo > {
150153 let mut files = Vec :: new ( ) ;
151154 walk_resource_dir ( & RESOURCE_DIR , & mut files) ;
152155 files
@@ -170,3 +173,11 @@ fn walk_resource_dir(dir: &Dir, files: &mut Vec<LuaFileInfo>) {
170173 }
171174 }
172175}
176+
177+ // 优先使用当前语言环境的 std-{locale} 目录, 否则回退到默认的 std 目录
178+ fn get_std_dir ( resources_dir : & Path ) -> PathBuf {
179+ 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+ }
0 commit comments