Skip to content

Commit

Permalink
Add translations for Rust error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmostellar committed Feb 21, 2024
1 parent f20ae58 commit 0ed8258
Show file tree
Hide file tree
Showing 24 changed files with 301 additions and 102 deletions.
11 changes: 7 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/lang/getLocale.ts
Expand Up @@ -36,3 +36,8 @@ export const getLocale = (category: LocaleCategory, key: string) => {

return usingLocale[category][key];
};

export const getWasmLocale = () => {
const usingLocale = locales[detectedLanguage] ?? locales.en;
return usingLocale.wasm;
};
2 changes: 1 addition & 1 deletion src/lang/locale/en.json
Expand Up @@ -51,7 +51,7 @@
},
"wasm": {
"parsing": {
"Failed to parse the document. [Line: {VALUE}]": "Failed to parse the document. [Line: {VALUE}]",
"Failed to parse the document. [Line: {LINE_NUMBER}]": "Failed to parse the document. [Line: {LINE_NUMBER}]",
"Failed to parse the document.": "Failed to parse the document."
},
"formatting": {
Expand Down
2 changes: 1 addition & 1 deletion src/lang/locale/hu.json
Expand Up @@ -51,7 +51,7 @@
},
"wasm": {
"parsing": {
"Failed to parse the document. [Line: {VALUE}]": "",
"Failed to parse the document. [Line: {LINE_NUMBER}]": "",
"Failed to parse the document.": ""
},
"formatting": {
Expand Down
2 changes: 1 addition & 1 deletion src/lang/locale/ko.json
Expand Up @@ -51,7 +51,7 @@
},
"wasm": {
"parsing": {
"Failed to parse the document. [Line: {VALUE}]": "문서를 읽지 못했습니다. [줄: {VALUE}]",
"Failed to parse the document. [Line: {LINE_NUMBER}]": "문서를 읽지 못했습니다. [줄: {LINE_NUMBER}]",
"Failed to parse the document.": "문서를 읽지 못했습니다."
},
"formatting": {
Expand Down
5 changes: 3 additions & 2 deletions src/utils.ts
@@ -1,7 +1,7 @@
import { Editor, Notice } from "obsidian";

import { format_document } from "../wasm/pkg/formatto_wasm";
import { getLocale, LOCALE_CATEGORY } from "./lang/getLocale";
import { getLocale, getWasmLocale, LOCALE_CATEGORY } from "./lang/getLocale";
import FormattoPlugin from "./main";

export class FormattoUtils {
Expand All @@ -19,7 +19,8 @@ export class FormattoUtils {
try {
formattedDocument = format_document(
originalDocument,
this.plugin.settings
this.plugin.settings,
JSON.stringify(getWasmLocale())
);
} catch (error) {
new Notice(error);
Expand Down
39 changes: 30 additions & 9 deletions wasm/src/lib.rs
@@ -1,7 +1,7 @@
use std::error::Error;
use wasm_bindgen::prelude::*;

use crate::setting_schema::MainPluginSettings;
use crate::setting_schema::PluginSettings;

mod setting_schema;
mod tools;
Expand Down Expand Up @@ -41,23 +41,31 @@ mod macro_rules {

#[wasm_bindgen]
/// This function is called from the TypeScript side.
pub fn format_document(input: &str, js_settings: JsValue) -> String {
pub fn format_document(input: &str, js_settings: JsValue, js_locales: JsValue) -> String {
utils::set_panic_hook();

let settings = match read_settings(js_settings) {
let settings: PluginSettings = match read_settings(js_settings) {
Ok(settings) => settings,
Err(e) => {
let error_message = e.to_string();
wasm_bindgen::throw_str(&error_message);
}
};

let locales = match read_js_value(js_locales) {
Ok(locales) => locales,
Err(e) => {
let error_message = e.to_string();
wasm_bindgen::throw_str(&error_message);
}
};

if input.is_empty() {
return input.to_string();
}

// Return value to the TypeScript side or throw an error.
match parse_input(input, settings) {
match parse_input(input, settings, &locales) {
Ok(sections) => sections,
Err(e) => {
let error_message = e.to_string();
Expand All @@ -66,13 +74,26 @@ pub fn format_document(input: &str, js_settings: JsValue) -> String {
}
}

fn read_settings(settings: JsValue) -> Result<MainPluginSettings, Box<dyn Error>> {
Ok(serde_wasm_bindgen::from_value(settings)?)
fn read_settings<T: serde::de::DeserializeOwned>(input: JsValue) -> Result<T, Box<dyn Error>> {
Ok(serde_wasm_bindgen::from_value(input)?)
}

use serde_json::Value;
fn read_js_value(js_value: JsValue) -> Result<Value, Box<dyn Error>> {
if let Some(a) = &js_value.as_string() {
Ok(serde_json::from_str(a)?)
} else {
Err("Failed to read locale file.".into())
}
}

fn parse_input(input: &str, settings: MainPluginSettings) -> Result<String, Box<dyn Error>> {
let sections = tools::parsing::get_sections(input, &settings)?;
let output = tools::formatting::get_formatted_string(sections, &settings)?;
fn parse_input(
input: &str,
settings: PluginSettings,
locales: &Value,
) -> Result<String, Box<dyn Error>> {
let sections = tools::parsing::get_sections(input, &settings, locales)?;
let output = tools::formatting::get_formatted_string(sections, &settings, locales)?;

Ok(output)
}
2 changes: 1 addition & 1 deletion wasm/src/setting_schema.rs
Expand Up @@ -44,7 +44,7 @@ pub struct OtherOptions {

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MainPluginSettings {
pub struct PluginSettings {
pub heading_gaps: HeadingGaps,
pub other_gaps: OtherGaps,
pub format_options: FormatOptions,
Expand Down
32 changes: 26 additions & 6 deletions wasm/src/testing.rs
@@ -1,19 +1,20 @@
use crate::{
setting_schema::{FormatOptions, HeadingGaps, MainPluginSettings, OtherGaps, OtherOptions},
utils::set_panic_hook,
};
use crate::utils::set_panic_hook;
use serde_json::Value;

use crate::setting_schema::{FormatOptions, HeadingGaps, OtherGaps, OtherOptions, PluginSettings};

mod formatting;
mod parsing;
mod utils;

#[allow(dead_code)]
fn setup() {
set_panic_hook();
}

#[allow(dead_code)]
fn get_example_settings() -> MainPluginSettings {
MainPluginSettings {
fn get_example_settings() -> PluginSettings {
PluginSettings {
heading_gaps: HeadingGaps {
before_top_level_headings: Some("3".to_string()),
before_first_sub_heading: Some("1".to_string()),
Expand All @@ -35,3 +36,22 @@ fn get_example_settings() -> MainPluginSettings {
},
}
}

#[allow(dead_code)]
fn get_example_locale() -> Value {
let val = r#"
{
"parsing": {
"Failed to parse the document. [Line: {LINE_NUMBER}]": "문서를 읽지 못했습니다. [줄: {LINE_NUMBER}]",
"Failed to parse the document.": "문서를 읽지 못했습니다."
},
"formatting": {
"Failed to read options. Please make sure there is no option with an empty value.": "옵션을 읽지 못했습니다. 값이 비어있는 옵션이 없는지 다시 확인해주세요.",
"Failed to read options. Some of them are possibly not positive number values.": "설정을 읽지 못했습니다. 양수가 아닌 값이 있을수도 있습니다.",
"Failed to read option properties.": "옵션 프로퍼티를 읽지 못했습니다."
}
}
"#;

serde_json::from_str(val).unwrap()
}
12 changes: 7 additions & 5 deletions wasm/src/testing/formatting/code_blocks.rs
@@ -1,5 +1,5 @@
use crate::{
testing::{get_example_settings, setup},
testing::{get_example_locale, get_example_settings, setup},
tools::{formatting::get_formatted_string, parsing::get_sections},
};

Expand All @@ -17,8 +17,9 @@ fn main(
) {}
```"#;

let sections = get_sections(input, &get_example_settings()).unwrap();
let output = get_formatted_string(sections, &get_example_settings()).unwrap();
let sections = get_sections(input, &get_example_settings(), &get_example_locale()).unwrap();
let output =
get_formatted_string(sections, &get_example_settings(), &get_example_locale()).unwrap();
let expected_output = r#"## Heading 2
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Expand Down Expand Up @@ -48,8 +49,9 @@ fn main(
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
"#;

let sections = get_sections(input, &get_example_settings()).unwrap();
let output = get_formatted_string(sections, &get_example_settings()).unwrap();
let sections = get_sections(input, &get_example_settings(), &get_example_locale()).unwrap();
let output =
get_formatted_string(sections, &get_example_settings(), &get_example_locale()).unwrap();
let expected_output = r#"## Heading 2
```rust
fn main(
Expand Down
12 changes: 7 additions & 5 deletions wasm/src/testing/formatting/headings/alternate_headings.rs
@@ -1,5 +1,5 @@
use crate::{
testing::{get_example_settings, setup},
testing::{get_example_locale, get_example_settings, setup},
tools::{formatting::get_formatted_string, parsing::get_sections},
};

Expand All @@ -17,8 +17,9 @@ console.log("Hello World");
Heading 1
==="#;

let sections = get_sections(input, &get_example_settings()).unwrap();
let output = get_formatted_string(sections, &get_example_settings()).unwrap();
let sections = get_sections(input, &get_example_settings(), &get_example_locale()).unwrap();
let output =
get_formatted_string(sections, &get_example_settings(), &get_example_locale()).unwrap();
let expected_output = r#"## Heading 2
### heading 3
Expand Down Expand Up @@ -46,8 +47,9 @@ Heading 1
### Heading 3"#;

let sections = get_sections(input, &get_example_settings()).unwrap();
let output = get_formatted_string(sections, &get_example_settings()).unwrap();
let sections = get_sections(input, &get_example_settings(), &get_example_locale()).unwrap();
let output =
get_formatted_string(sections, &get_example_settings(), &get_example_locale()).unwrap();
let expected_output = r#"Heading 1
==
Expand Down
12 changes: 7 additions & 5 deletions wasm/src/testing/formatting/headings/hash_headings.rs
@@ -1,5 +1,5 @@
use crate::{
testing::{get_example_settings, setup},
testing::{get_example_locale, get_example_settings, setup},
tools::{formatting::get_formatted_string, parsing::get_sections},
};

Expand All @@ -11,8 +11,9 @@ fn case_1() {
let input = r#"## Heading 2
### Heading 3
#### Heading 4"#;
let sections = get_sections(input, &get_example_settings()).unwrap();
let output = get_formatted_string(sections, &get_example_settings()).unwrap();
let sections = get_sections(input, &get_example_settings(), &get_example_locale()).unwrap();
let output =
get_formatted_string(sections, &get_example_settings(), &get_example_locale()).unwrap();
let expected_output = r#"## Heading 2
### Heading 3
Expand All @@ -29,8 +30,9 @@ fn invalid_input_1() {
let input = r#"##Heading 2
###Heading 3
####Heading 4"#;
let sections = get_sections(input, &get_example_settings()).unwrap();
let output = get_formatted_string(sections, &get_example_settings()).unwrap();
let sections = get_sections(input, &get_example_settings(), &get_example_locale()).unwrap();
let output =
get_formatted_string(sections, &get_example_settings(), &get_example_locale()).unwrap();
let expected_output = r#"##Heading 2
###Heading 3
####Heading 4"#;
Expand Down
12 changes: 7 additions & 5 deletions wasm/src/testing/formatting/properties.rs
@@ -1,5 +1,5 @@
use crate::{
testing::{get_example_settings, setup},
testing::{get_example_locale, get_example_settings, setup},
tools::{formatting::get_formatted_string, parsing::get_sections},
};

Expand All @@ -22,8 +22,9 @@ Lorem Ipsum is simply dummy text of the printing and typesetting industry.
#### Heading 4
## Heading 2"#;

let sections = get_sections(input, &get_example_settings()).unwrap();
let output = get_formatted_string(sections, &get_example_settings()).unwrap();
let sections = get_sections(input, &get_example_settings(), &get_example_locale()).unwrap();
let output =
get_formatted_string(sections, &get_example_settings(), &get_example_locale()).unwrap();
let expected_output = r#"---
aliases:
- Test
Expand Down Expand Up @@ -54,8 +55,9 @@ aliases:
- Test
---"#;

let sections = get_sections(input, &get_example_settings()).unwrap();
let output = get_formatted_string(sections, &get_example_settings()).unwrap();
let sections = get_sections(input, &get_example_settings(), &get_example_locale()).unwrap();
let output =
get_formatted_string(sections, &get_example_settings(), &get_example_locale()).unwrap();
let expected_output = r#"---
aliases:
- Test
Expand Down

0 comments on commit 0ed8258

Please sign in to comment.