@@ -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 ( ) ;
@@ -58,21 +60,25 @@ pub fn load_resource_std(
5860}
5961
6062fn 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+ ] ;
6172 files. retain ( |file| {
6273 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" ) ;
70-
71- !should_remove
74+ !JIT_FILES_TO_REMOVE
75+ . iter ( )
76+ . any ( |suffix| path. ends_with ( suffix) )
7277 } ) ;
7378}
7479
7580fn load_resource_from_file_system ( resources_dir : & Path ) -> Option < Vec < LuaFileInfo > > {
81+ // lsp i18n 的资源在更早之前的 crates\emmylua_ls\src\handlers\initialized\std_i18n.rs 中写入到文件系统
7682 if check_need_dump_to_file_system ( ) {
7783 log:: info!( "Creating resources dir: {:?}" , resources_dir) ;
7884 let files = load_resource_from_include_dir ( ) ;
@@ -109,7 +115,7 @@ fn load_resource_from_file_system(resources_dir: &Path) -> Option<Vec<LuaFileInf
109115 }
110116 }
111117
112- let std_dir = resources_dir . join ( "std" ) ;
118+ let std_dir = get_std_dir ( & resources_dir ) ;
113119 let match_pattern = vec ! [ "**/*.lua" . to_string( ) ] ;
114120 let files = match load_workspace_files ( & std_dir, & match_pattern, & Vec :: new ( ) , & Vec :: new ( ) , None )
115121 {
@@ -146,7 +152,7 @@ fn check_need_dump_to_file_system() -> bool {
146152 false
147153}
148154
149- fn load_resource_from_include_dir ( ) -> Vec < LuaFileInfo > {
155+ pub fn load_resource_from_include_dir ( ) -> Vec < LuaFileInfo > {
150156 let mut files = Vec :: new ( ) ;
151157 walk_resource_dir ( & RESOURCE_DIR , & mut files) ;
152158 files
@@ -170,3 +176,15 @@ fn walk_resource_dir(dir: &Dir, files: &mut Vec<LuaFileInfo>) {
170176 }
171177 }
172178}
179+
180+ // 优先使用当前语言环境的 std-{locale} 目录, 否则回退到默认的 std 目录
181+ fn get_std_dir ( resources_dir : & Path ) -> PathBuf {
182+ let locale = get_locale_code ( & rust_i18n:: locale ( ) ) ;
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" )
190+ }
0 commit comments