summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2025-12-08 21:20:19 +0800
committer魏曹先生 <1992414357@qq.com>2025-12-08 21:20:19 +0800
commit1d1a1009ba795d70c0c06a2ffcc607c5704bf675 (patch)
tree2952d9a77b659b4b73088e94263a22c8126ad573
parent45ab6b03fd5e5140e0f2df4c6834d219c94ac2b6 (diff)
Add i18n support for jvii editor messages
- Add error, message, and status translations for English and Chinese - Replace hardcoded strings with t!() macro calls - Update file precheck to return errors instead of creating files
-rw-r--r--locales/help_docs/en.yml20
-rw-r--r--locales/help_docs/zh-CN.yml20
-rw-r--r--src/bin/jvii.rs54
3 files changed, 70 insertions, 24 deletions
diff --git a/locales/help_docs/en.yml b/locales/help_docs/en.yml
index ab175ef..80facb0 100644
--- a/locales/help_docs/en.yml
+++ b/locales/help_docs/en.yml
@@ -943,6 +943,26 @@ jv:
jvii:
hints: |
[^S WRITE] [ESC EXIT]
+
+ errors:
+ no_file_path: "Error: No file path provided"
+ editor_error: "Editor error: %{error}"
+ file_error: "File error: %{error}"
+ file_not_found: "File does not exist: %{path}"
+ not_a_file: "Path is not a file: %{path}"
+ init_error: "Failed to initialize editor: %{error}"
+ raw_mode_error: "Failed to enable raw mode: %{error}"
+ alternate_screen_error: "Failed to enter alternate screen: %{error}"
+ save_error: "Failed to save file: %{error}"
+ render_error: "Render error: %{error}"
+
+ messages:
+ file_saved: "File saved successfully"
+ unsaved_changes: "Unsaved changes! Press Ctrl+S to save or Esc again to exit"
+ modified: " *"
+
+ status:
+ lines: " lines"
editor:
update_editor: |
diff --git a/locales/help_docs/zh-CN.yml b/locales/help_docs/zh-CN.yml
index 86051a0..3059283 100644
--- a/locales/help_docs/zh-CN.yml
+++ b/locales/help_docs/zh-CN.yml
@@ -943,6 +943,26 @@ jvii:
hints: |
[^S 保存] [ESC 退出]
+ errors:
+ no_file_path: "错误:未提供文件路径"
+ editor_error: "编辑器错误:%{error}"
+ file_error: "文件错误:%{error}"
+ file_not_found: "文件不存在:%{path}"
+ not_a_file: "路径不是文件:%{path}"
+ init_error: "初始化编辑器失败:%{error}"
+ raw_mode_error: "启用原始模式失败:%{error}"
+ alternate_screen_error: "进入备用屏幕失败:%{error}"
+ save_error: "保存文件失败:%{error}"
+ render_error: "渲染错误:%{error}"
+
+ messages:
+ file_saved: "文件保存成功"
+ unsaved_changes: "有未保存的修改!按 Ctrl+S 保存或再次按 Esc 退出"
+ modified: " *"
+
+ status:
+ lines: " 行"
+
editor:
update_editor: |
# 您正在使用编辑器模式追踪和提交文件
diff --git a/src/bin/jvii.rs b/src/bin/jvii.rs
index 0d6a584..aacd371 100644
--- a/src/bin/jvii.rs
+++ b/src/bin/jvii.rs
@@ -287,10 +287,15 @@ impl Editor {
stdout.queue(Clear(ClearType::CurrentLine))?;
let status = format!(
- "{} - {} lines{} {}",
+ "{} - {}{}{} {}",
self.file_path.display(),
self.content.len(),
- if self.modified { " *" } else { "" },
+ t!("jvii.status.lines"),
+ if self.modified {
+ t!("jvii.messages.modified").to_string()
+ } else {
+ "".to_string()
+ },
md(t!("jvii.hints"))
);
@@ -346,13 +351,19 @@ impl Editor {
// Setup terminal with error handling
if let Err(e) = enable_raw_mode() {
- eprintln!("Failed to enable raw mode: {}", e);
+ eprintln!(
+ "{}",
+ t!("jvii.errors.raw_mode_error", error = e.to_string())
+ );
return Err(e);
}
if let Err(e) = execute!(stdout, EnterAlternateScreen) {
disable_raw_mode().ok();
- eprintln!("Failed to enter alternate screen: {}", e);
+ eprintln!(
+ "{}",
+ t!("jvii.errors.alternate_screen_error", error = e.to_string())
+ );
return Err(e);
}
@@ -402,10 +413,10 @@ impl Editor {
match key_event.code {
KeyCode::Char('s') if key_event.modifiers.contains(KeyModifiers::CONTROL) => {
if let Err(e) = self.save() {
- eprintln!("Failed to save file: {}", e);
+ eprintln!("{}", t!("jvii.errors.save_error", error = e.to_string()));
// Continue editing even if save fails
} else {
- self.show_message("File saved successfully", stdout)?;
+ self.show_message(&t!("jvii.messages.file_saved"), stdout)?;
}
}
KeyCode::Char(c) => {
@@ -485,10 +496,7 @@ impl Editor {
}
KeyCode::Esc => {
if self.modified {
- self.show_message(
- "Unsaved changes! Press Ctrl+S to save or Esc again to exit",
- stdout,
- )?;
+ self.show_message(&t!("jvii.messages.unsaved_changes"), stdout)?;
// Don't exit immediately, wait for second Esc
} else {
self.should_exit = true;
@@ -498,7 +506,7 @@ impl Editor {
}
if let Err(e) = self.render(stdout) {
- eprintln!("Render error: {}", e);
+ eprintln!("{}", t!("jvii.errors.render_error", error = e.to_string()));
return Err(e);
}
Ok(())
@@ -516,7 +524,7 @@ async fn main() {
let file_path = match args.file {
Some(path) => path,
None => {
- eprintln!("Error: No file path provided");
+ eprintln!("{}", t!("jvii.errors.no_file_path"));
std::process::exit(1);
}
};
@@ -525,12 +533,12 @@ async fn main() {
match precheck(file_path) {
Ok(full_path) => {
if let Err(e) = open_editor(full_path).await {
- eprintln!("Editor error: {}", e);
+ eprintln!("{}", t!("jvii.errors.editor_error", error = e.to_string()));
std::process::exit(1);
}
}
Err(e) => {
- eprintln!("File error: {}", e);
+ eprintln!("{}", t!("jvii.errors.file_error", error = e.to_string()));
std::process::exit(1);
}
}
@@ -547,17 +555,15 @@ fn precheck(file_path: PathBuf) -> Result<PathBuf, std::io::Error> {
current_dir.join(&file_path)
};
- // Create file if it doesn't exist
+ // Check if the file exists
if !full_path.exists() {
- // Create parent directories if needed
- if let Some(parent) = full_path.parent() {
- fs::create_dir_all(parent)?;
- }
- // Create empty file
- fs::write(&full_path, "")?;
+ return Err(std::io::Error::new(
+ std::io::ErrorKind::NotFound,
+ format!("File does not exist: {}", full_path.display()),
+ ));
}
- // Check if it's a file (or we just created it as a file)
+ // Check if it's a file
if !full_path.is_file() {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
@@ -575,14 +581,14 @@ async fn open_editor(file: PathBuf) -> io::Result<()> {
// Always try to cleanup terminal even if there was an error
if let Err(e) = result {
- eprintln!("Editor error: {}", e);
+ eprintln!("{}", t!("jvii.errors.editor_error", error = e.to_string()));
return Err(e);
}
Ok(())
}
Err(e) => {
- eprintln!("Failed to initialize editor: {}", e);
+ eprintln!("{}", t!("jvii.errors.init_error", error = e.to_string()));
Err(e)
}
}