From 0ed8258e89f2c941bc21ba92b949773d397a8d87 Mon Sep 17 00:00:00 2001 From: polyecho <160427240+polyecho@users.noreply.github.com> Date: Wed, 21 Feb 2024 21:33:13 +0900 Subject: [PATCH] Add translations for Rust error messages --- package-lock.json | 11 ++- src/lang/getLocale.ts | 5 ++ src/lang/locale/en.json | 2 +- src/lang/locale/hu.json | 2 +- src/lang/locale/ko.json | 2 +- src/utils.ts | 5 +- wasm/src/lib.rs | 39 ++++++--- wasm/src/setting_schema.rs | 2 +- wasm/src/testing.rs | 32 ++++++-- wasm/src/testing/formatting/code_blocks.rs | 12 +-- .../formatting/headings/alternate_headings.rs | 12 +-- .../formatting/headings/hash_headings.rs | 12 +-- wasm/src/testing/formatting/properties.rs | 12 +-- .../parsing/get_sections/code_blocks.rs | 6 +- .../testing/parsing/get_sections/contents.rs | 6 +- .../testing/parsing/get_sections/general.rs | 4 +- .../headings/alternate_headings.rs | 14 ++-- .../get_sections/headings/hash_headings.rs | 18 ++--- .../parsing/get_sections/properties.rs | 8 +- wasm/src/testing/utils.rs | 1 + wasm/src/testing/utils/get_locale_string.rs | 60 ++++++++++++++ wasm/src/tools/formatting.rs | 81 ++++++++++++++----- wasm/src/tools/parsing.rs | 34 ++++++-- wasm/src/utils.rs | 23 ++++++ 24 files changed, 301 insertions(+), 102 deletions(-) create mode 100644 wasm/src/testing/utils.rs create mode 100644 wasm/src/testing/utils/get_locale_string.rs diff --git a/package-lock.json b/package-lock.json index 9ec07b7..c46cb22 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,12 +8,10 @@ "name": "formatto", "version": "1.2.1", "license": "MIT", - "dependencies": { - "@rollup/plugin-json": "^6.1.0" - }, "devDependencies": { "@rollup/plugin-alias": "^5.0.1", "@rollup/plugin-commonjs": "^25.0.7", + "@rollup/plugin-json": "^6.1.0", "@rollup/plugin-node-resolve": "^15.2.3", "@rollup/plugin-typescript": "^11.1.5", "@rollup/plugin-wasm": "^6.2.2", @@ -633,6 +631,7 @@ "version": "6.1.0", "resolved": "https://registry.npmjs.org/@rollup/plugin-json/-/plugin-json-6.1.0.tgz", "integrity": "sha512-EGI2te5ENk1coGeADSIwZ7G2Q8CJS2sF120T7jLw4xFw9n7wIOXHo+kIYRAoVpJAN+kmqZSoO3Fp4JtoNF4ReA==", + "dev": true, "dependencies": { "@rollup/pluginutils": "^5.1.0" }, @@ -720,6 +719,7 @@ "version": "5.1.0", "resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-5.1.0.tgz", "integrity": "sha512-XTIWOPPcpvyKI6L1NHo0lFlCyznUEyPmPY1mc3KpPVDYulHSTvyeLNVW00QTLIAFNhR3kYnJTQHeGqU4M3n09g==", + "dev": true, "dependencies": { "@types/estree": "^1.0.0", "estree-walker": "^2.0.2", @@ -921,6 +921,7 @@ }, "node_modules/@types/estree": { "version": "1.0.4", + "dev": true, "license": "MIT" }, "node_modules/@types/json-schema": { @@ -1797,6 +1798,7 @@ }, "node_modules/estree-walker": { "version": "2.0.2", + "dev": true, "license": "MIT" }, "node_modules/esutils": { @@ -2687,6 +2689,7 @@ }, "node_modules/picomatch": { "version": "2.3.1", + "dev": true, "license": "MIT", "engines": { "node": ">=8.6" @@ -2853,7 +2856,7 @@ "version": "4.3.0", "resolved": "https://registry.npmjs.org/rollup/-/rollup-4.3.0.tgz", "integrity": "sha512-scIi1NrKLDIYSPK66jjECtII7vIgdAMFmFo8h6qm++I6nN9qDSV35Ku6erzGVqYjx+lj+j5wkusRMr++8SyDZg==", - "devOptional": true, + "dev": true, "bin": { "rollup": "dist/bin/rollup" }, diff --git a/src/lang/getLocale.ts b/src/lang/getLocale.ts index 866c361..0c78a91 100644 --- a/src/lang/getLocale.ts +++ b/src/lang/getLocale.ts @@ -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; +}; diff --git a/src/lang/locale/en.json b/src/lang/locale/en.json index e35ef9a..9f653fb 100644 --- a/src/lang/locale/en.json +++ b/src/lang/locale/en.json @@ -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": { diff --git a/src/lang/locale/hu.json b/src/lang/locale/hu.json index 932bf70..d43c49b 100644 --- a/src/lang/locale/hu.json +++ b/src/lang/locale/hu.json @@ -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": { diff --git a/src/lang/locale/ko.json b/src/lang/locale/ko.json index b74689b..8b48ebe 100644 --- a/src/lang/locale/ko.json +++ b/src/lang/locale/ko.json @@ -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": { diff --git a/src/utils.ts b/src/utils.ts index 48ed465..7925e3e 100644 --- a/src/utils.ts +++ b/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 { @@ -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); diff --git a/wasm/src/lib.rs b/wasm/src/lib.rs index 1231b1b..1e64e06 100644 --- a/wasm/src/lib.rs +++ b/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; @@ -41,10 +41,10 @@ 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(); @@ -52,12 +52,20 @@ pub fn format_document(input: &str, js_settings: JsValue) -> String { } }; + 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(); @@ -66,13 +74,26 @@ pub fn format_document(input: &str, js_settings: JsValue) -> String { } } -fn read_settings(settings: JsValue) -> Result> { - Ok(serde_wasm_bindgen::from_value(settings)?) +fn read_settings(input: JsValue) -> Result> { + Ok(serde_wasm_bindgen::from_value(input)?) +} + +use serde_json::Value; +fn read_js_value(js_value: JsValue) -> Result> { + 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> { - 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> { + let sections = tools::parsing::get_sections(input, &settings, locales)?; + let output = tools::formatting::get_formatted_string(sections, &settings, locales)?; Ok(output) } diff --git a/wasm/src/setting_schema.rs b/wasm/src/setting_schema.rs index 614401d..080ea44 100644 --- a/wasm/src/setting_schema.rs +++ b/wasm/src/setting_schema.rs @@ -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, diff --git a/wasm/src/testing.rs b/wasm/src/testing.rs index 6586123..384ee24 100644 --- a/wasm/src/testing.rs +++ b/wasm/src/testing.rs @@ -1,10 +1,11 @@ -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() { @@ -12,8 +13,8 @@ fn setup() { } #[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()), @@ -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() +} diff --git a/wasm/src/testing/formatting/code_blocks.rs b/wasm/src/testing/formatting/code_blocks.rs index 048eac3..599a7d9 100644 --- a/wasm/src/testing/formatting/code_blocks.rs +++ b/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}, }; @@ -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. @@ -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( diff --git a/wasm/src/testing/formatting/headings/alternate_headings.rs b/wasm/src/testing/formatting/headings/alternate_headings.rs index f1b27ea..381a771 100644 --- a/wasm/src/testing/formatting/headings/alternate_headings.rs +++ b/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}, }; @@ -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 @@ -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 == diff --git a/wasm/src/testing/formatting/headings/hash_headings.rs b/wasm/src/testing/formatting/headings/hash_headings.rs index 7de7eb7..a75e937 100644 --- a/wasm/src/testing/formatting/headings/hash_headings.rs +++ b/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}, }; @@ -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 @@ -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"#; diff --git a/wasm/src/testing/formatting/properties.rs b/wasm/src/testing/formatting/properties.rs index 4220a18..38e6cd7 100644 --- a/wasm/src/testing/formatting/properties.rs +++ b/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}, }; @@ -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 @@ -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 diff --git a/wasm/src/testing/parsing/get_sections/code_blocks.rs b/wasm/src/testing/parsing/get_sections/code_blocks.rs index 7f6181f..7e31ee5 100644 --- a/wasm/src/testing/parsing/get_sections/code_blocks.rs +++ b/wasm/src/testing/parsing/get_sections/code_blocks.rs @@ -1,5 +1,5 @@ use crate::{ - testing::{get_example_settings, setup}, + testing::{get_example_locale, get_example_settings, setup}, tools::{ parsing::get_sections, tokens::{HeadingLevel, MarkdownSection}, @@ -38,7 +38,7 @@ println!(\"Hello World\"); ]; assert_eq!( - get_sections(input, &get_example_settings()).unwrap(), + get_sections(input, &get_example_settings(), &get_example_locale()).unwrap(), expected_output ); } @@ -50,6 +50,6 @@ fn invalid_input_1() { let input = r#"``` code SPACE```"#; - let sections = get_sections(input, &get_example_settings()); + let sections = get_sections(input, &get_example_settings(), &get_example_locale()); assert!(sections.is_err()); } diff --git a/wasm/src/testing/parsing/get_sections/contents.rs b/wasm/src/testing/parsing/get_sections/contents.rs index 0d6872d..21ccb44 100644 --- a/wasm/src/testing/parsing/get_sections/contents.rs +++ b/wasm/src/testing/parsing/get_sections/contents.rs @@ -1,5 +1,5 @@ use crate::{ - testing::{get_example_settings, setup}, + testing::{get_example_locale, get_example_settings, setup}, tools::{ parsing::get_sections, tokens::{HeadingLevel, MarkdownSection}, @@ -23,7 +23,7 @@ Lorem Ipsum is simply dummy text of the printing and typesetting industry."# )]; assert_eq!( - get_sections(input, &get_example_settings()).unwrap(), + get_sections(input, &get_example_settings(), &get_example_locale()).unwrap(), expected_output ); } @@ -60,7 +60,7 @@ Lorem Ipsum is simply dummy text of the printing and typesetting industry."# ]; assert_eq!( - get_sections(input, &get_example_settings()).unwrap(), + get_sections(input, &get_example_settings(), &get_example_locale()).unwrap(), expected_output ); } diff --git a/wasm/src/testing/parsing/get_sections/general.rs b/wasm/src/testing/parsing/get_sections/general.rs index 11ce8ac..0d03e16 100644 --- a/wasm/src/testing/parsing/get_sections/general.rs +++ b/wasm/src/testing/parsing/get_sections/general.rs @@ -1,5 +1,5 @@ use crate::{ - testing::{get_example_settings, setup}, + testing::{get_example_locale, get_example_settings, setup}, tools::parsing::get_sections, }; @@ -11,7 +11,7 @@ fn empty_input() { let expected_output = vec![]; assert_eq!( - get_sections(input, &get_example_settings()).unwrap(), + get_sections(input, &get_example_settings(), &get_example_locale()).unwrap(), expected_output ); } diff --git a/wasm/src/testing/parsing/get_sections/headings/alternate_headings.rs b/wasm/src/testing/parsing/get_sections/headings/alternate_headings.rs index 37f4968..9f9c0f8 100644 --- a/wasm/src/testing/parsing/get_sections/headings/alternate_headings.rs +++ b/wasm/src/testing/parsing/get_sections/headings/alternate_headings.rs @@ -1,5 +1,5 @@ use crate::{ - testing::{get_example_settings, setup}, + testing::{get_example_locale, get_example_settings, setup}, tools::{ parsing::get_sections, tokens::{HeadingLevel, MarkdownSection}, @@ -23,7 +23,7 @@ fn case_1() { ]; assert_eq!( - get_sections(input, &get_example_settings()).unwrap(), + get_sections(input, &get_example_settings(), &get_example_locale()).unwrap(), expected_output ); } @@ -54,7 +54,7 @@ Heading 2 ]; assert_eq!( - get_sections(input, &get_example_settings()).unwrap(), + get_sections(input, &get_example_settings(), &get_example_locale()).unwrap(), expected_output ); } @@ -99,7 +99,7 @@ Content ]; assert_eq!( - get_sections(input, &get_example_settings()).unwrap(), + get_sections(input, &get_example_settings(), &get_example_locale()).unwrap(), expected_output ); } @@ -125,7 +125,7 @@ Content ]; assert_eq!( - get_sections(input, &get_example_settings()).unwrap(), + get_sections(input, &get_example_settings(), &get_example_locale()).unwrap(), expected_output ); } @@ -148,7 +148,7 @@ Content ]; assert_eq!( - get_sections(input, &get_example_settings()).unwrap(), + get_sections(input, &get_example_settings(), &get_example_locale()).unwrap(), expected_output ); } @@ -199,7 +199,7 @@ Content ]; assert_eq!( - get_sections(input, &get_example_settings()).unwrap(), + get_sections(input, &get_example_settings(), &get_example_locale()).unwrap(), expected_output ); } diff --git a/wasm/src/testing/parsing/get_sections/headings/hash_headings.rs b/wasm/src/testing/parsing/get_sections/headings/hash_headings.rs index 087c582..00a71e4 100644 --- a/wasm/src/testing/parsing/get_sections/headings/hash_headings.rs +++ b/wasm/src/testing/parsing/get_sections/headings/hash_headings.rs @@ -1,5 +1,5 @@ use crate::{ - testing::{get_example_settings, setup}, + testing::{get_example_locale, get_example_settings, setup}, tools::{ parsing::get_sections, tokens::{HeadingLevel, MarkdownSection}, @@ -25,7 +25,7 @@ Lorem Ipsum is simply dummy text of the printing and typesetting industry. ]; assert_eq!( - get_sections(input, &get_example_settings()).unwrap(), + get_sections(input, &get_example_settings(), &get_example_locale()).unwrap(), expected_output ); } @@ -45,7 +45,7 @@ fn case_2() { ]; assert_eq!( - get_sections(input, &get_example_settings()).unwrap(), + get_sections(input, &get_example_settings(), &get_example_locale()).unwrap(), expected_output ); } @@ -65,7 +65,7 @@ fn case_3() { ]; assert_eq!( - get_sections(input, &get_example_settings()).unwrap(), + get_sections(input, &get_example_settings(), &get_example_locale()).unwrap(), expected_output ); } @@ -102,7 +102,7 @@ Lorem Ipsum is simply dummy text of the printing and typesetting industry."#; ]; assert_eq!( - get_sections(input, &get_example_settings()).unwrap(), + get_sections(input, &get_example_settings(), &get_example_locale()).unwrap(), expected_output ); } @@ -138,7 +138,7 @@ Lorem Ipsum is simply dummy text of the printing and typesetting industry. ]; assert_eq!( - get_sections(input, &get_example_settings()).unwrap(), + get_sections(input, &get_example_settings(), &get_example_locale()).unwrap(), expected_output ); } @@ -167,7 +167,7 @@ Lorem Ipsum is simply dummy text of the printing and typesetting industry."#; ]; assert_eq!( - get_sections(input, &get_example_settings()).unwrap(), + get_sections(input, &get_example_settings(), &get_example_locale()).unwrap(), expected_output ); } @@ -204,7 +204,7 @@ fn hash_headings_without_title_names() { ]; assert_eq!( - get_sections(input, &get_example_settings()).unwrap(), + get_sections(input, &get_example_settings(), &get_example_locale()).unwrap(), expected_output ); } @@ -225,7 +225,7 @@ fn invalid_input_1() { )]; assert_eq!( - get_sections(input, &get_example_settings()).unwrap(), + get_sections(input, &get_example_settings(), &get_example_locale()).unwrap(), expected_output ); } diff --git a/wasm/src/testing/parsing/get_sections/properties.rs b/wasm/src/testing/parsing/get_sections/properties.rs index 0bcc186..65181f0 100644 --- a/wasm/src/testing/parsing/get_sections/properties.rs +++ b/wasm/src/testing/parsing/get_sections/properties.rs @@ -1,5 +1,5 @@ use crate::{ - testing::{get_example_settings, setup}, + testing::{get_example_locale, get_example_settings, setup}, tools::{ parsing::get_sections, tokens::{HeadingLevel, MarkdownSection}, @@ -22,7 +22,7 @@ aliases: )]; assert_eq!( - get_sections(input, &get_example_settings()).unwrap(), + get_sections(input, &get_example_settings(), &get_example_locale()).unwrap(), expected_output ); } @@ -64,7 +64,7 @@ Lorem Ipsum is simply dummy text of the printing and typesetting industry. ]; assert_eq!( - get_sections(input, &get_example_settings()).unwrap(), + get_sections(input, &get_example_settings(), &get_example_locale()).unwrap(), expected_output ); } @@ -92,5 +92,5 @@ Lorem Ipsum is simply dummy text of the printing and typesetting industry. #### Heading 4 ## Heading 2"#; - assert!(get_sections(input, &get_example_settings()).is_err()) + assert!(get_sections(input, &get_example_settings(), &get_example_locale()).is_err()) } diff --git a/wasm/src/testing/utils.rs b/wasm/src/testing/utils.rs new file mode 100644 index 0000000..67bb301 --- /dev/null +++ b/wasm/src/testing/utils.rs @@ -0,0 +1 @@ +mod get_locale_string; diff --git a/wasm/src/testing/utils/get_locale_string.rs b/wasm/src/testing/utils/get_locale_string.rs new file mode 100644 index 0000000..b9da48d --- /dev/null +++ b/wasm/src/testing/utils/get_locale_string.rs @@ -0,0 +1,60 @@ +use super::super::get_example_locale; +use crate::utils::{get_locale_string, LocaleCategory}; + +#[test] +fn parsing_1() { + let locales = get_example_locale(); + + let mut left = get_locale_string( + &locales, + LocaleCategory::Parsing, + "Failed to parse the document. [Line: {LINE_NUMBER}]", + ); + left = left.replace("{LINE_NUMBER}", 1.to_string().as_str()); + let right = String::from("문서를 읽지 못했습니다. [줄: 1]"); + + assert_eq!(left, right); +} + +#[test] +fn parsing_2() { + let locales = get_example_locale(); + + let left = get_locale_string( + &locales, + LocaleCategory::Parsing, + "Failed to parse the document.", + ); + let right = String::from("문서를 읽지 못했습니다."); + + assert_eq!(left, right); +} + +#[test] +fn formatting_1() { + let locales = get_example_locale(); + + let left = get_locale_string( + &locales, + LocaleCategory::Formatting, + "Failed to read options. Please make sure there is no option with an empty value.", + ); + let right = + String::from("옵션을 읽지 못했습니다. 값이 비어있는 옵션이 없는지 다시 확인해주세요."); + + assert_eq!(left, right); +} + +#[test] +fn formatting_2() { + let locales = get_example_locale(); + + let left = get_locale_string( + &locales, + LocaleCategory::Formatting, + "Failed to read option properties.", + ); + let right = String::from("옵션 프로퍼티를 읽지 못했습니다."); + + assert_eq!(left, right); +} diff --git a/wasm/src/tools/formatting.rs b/wasm/src/tools/formatting.rs index 85efe70..761c054 100644 --- a/wasm/src/tools/formatting.rs +++ b/wasm/src/tools/formatting.rs @@ -1,13 +1,15 @@ +use serde_json::Value; use std::error::Error; use crate::console_error; -use crate::setting_schema::MainPluginSettings; +use crate::setting_schema::PluginSettings; use crate::tools::tokens::{HeadingLevel, MarkdownSection}; /// Return a String value that is replacing the entire document. pub fn get_formatted_string( sections: Vec, - settings: &MainPluginSettings, + settings: &PluginSettings, + locales: &Value, ) -> Result> { let mut output = String::new(); @@ -33,10 +35,14 @@ pub fn get_formatted_string( if output.is_empty() { 0 } else if right_after_properties { - parse_string_to_usize(&settings.other_gaps.after_properties)? + 1 + parse_string_to_usize( + &settings.other_gaps.after_properties, + locales, + )? + 1 } else { parse_string_to_usize( &settings.heading_gaps.before_top_level_headings, + locales, )? + 1 }, 0, @@ -48,10 +54,14 @@ pub fn get_formatted_string( if output.is_empty() { 0 } else if right_after_properties { - parse_string_to_usize(&settings.other_gaps.after_properties)? + 1 + parse_string_to_usize( + &settings.other_gaps.after_properties, + locales, + )? + 1 } else { parse_string_to_usize( &settings.heading_gaps.before_first_sub_heading, + locales, )? + 1 }, 0, @@ -64,10 +74,15 @@ pub fn get_formatted_string( if output.is_empty() { 0 } else if right_after_properties { - parse_string_to_usize(&settings.other_gaps.after_properties)? + 1 + parse_string_to_usize( + &settings.other_gaps.after_properties, + locales, + )? + 1 } else { - parse_string_to_usize(&settings.heading_gaps.before_sub_headings)? - + 1 + parse_string_to_usize( + &settings.heading_gaps.before_sub_headings, + locales, + )? + 1 }, 0, )); @@ -84,13 +99,14 @@ pub fn get_formatted_string( if output.is_empty() { 0 } else if right_after_properties { - parse_string_to_usize(&settings.other_gaps.after_properties)? + 1 + parse_string_to_usize(&settings.other_gaps.after_properties, locales)? + 1 } else if right_after_code_block { parse_string_to_usize( &settings.other_gaps.before_contents_after_code_blocks, + locales, )? + 1 } else { - parse_string_to_usize(&settings.other_gaps.before_contents)? + 1 + parse_string_to_usize(&settings.other_gaps.before_contents, locales)? + 1 }, 0, )); @@ -105,13 +121,14 @@ pub fn get_formatted_string( if output.is_empty() { 0 } else if right_after_properties { - parse_string_to_usize(&settings.other_gaps.after_properties)? + 1 + parse_string_to_usize(&settings.other_gaps.after_properties, locales)? + 1 } else if right_after_heading { parse_string_to_usize( &settings.other_gaps.before_code_blocks_after_headings, + locales, )? + 1 } else { - parse_string_to_usize(&settings.other_gaps.before_code_blocks)? + 1 + parse_string_to_usize(&settings.other_gaps.before_code_blocks, locales)? + 1 }, 0, )); @@ -140,13 +157,21 @@ pub fn insert_line_breaks(input: &str, before_count: usize, after_count: usize) /// Parse a usize value from a &str type argument. /// Also return an `Error` to handle exceptions. -pub fn parse_string_to_usize(input: &Option) -> Result> { +pub fn parse_string_to_usize( + input: &Option, + locales: &Value, +) -> Result> { + use crate::utils::{get_locale_string, LocaleCategory}; + if let Some(input) = input { if input.is_empty() { - return Err(String::from( - "Failed to read settings. Please make sure there is no option with an empty value.", - ) - .into()); + let msg = get_locale_string( + locales, + LocaleCategory::Formatting, + "Failed to read options. Please make sure there is no option with an empty value.", + ); + + return Err(msg.into()); } } @@ -155,12 +180,26 @@ pub fn parse_string_to_usize(input: &Option) -> Result Ok(num), Err(err) => { console_error!("{}", err); - Err(String::from( - "Failed to read settings. Some of them are possibly not positive number values.", - ) - .into()) + + let msg = get_locale_string( + locales, + LocaleCategory::Formatting, + "Failed to read options. Some of them are possibly not positive number values.", + ); + + #[allow(clippy::needless_return)] + return Err(msg.into()); } }, - None => Err(String::from("Failed to access setting properties.").into()), + None => { + let msg = get_locale_string( + locales, + LocaleCategory::Formatting, + "Failed to read option properties.", + ); + + #[allow(clippy::needless_return)] + return Err(msg.into()); + } } } diff --git a/wasm/src/tools/parsing.rs b/wasm/src/tools/parsing.rs index fa85e3d..2d3b168 100644 --- a/wasm/src/tools/parsing.rs +++ b/wasm/src/tools/parsing.rs @@ -1,7 +1,8 @@ +use serde_json::Value; use std::{error::Error, vec}; use crate::{ - setting_schema::MainPluginSettings, + setting_schema::PluginSettings, tools::tokens::{HeadingLevel, MarkdownSection}, }; @@ -15,7 +16,8 @@ struct ErrorInformation { pub fn get_sections( input: &str, - settings: &MainPluginSettings, + settings: &PluginSettings, + locales: &Value, ) -> Result, Box> { use super::parsing::contents::{append_line_break, finish_current_content_section}; use super::parsing::headings::{ @@ -284,6 +286,7 @@ pub fn get_sections( is_reading_property_block, settings, &error_information, + locales, )?; append_line_break(&mut temp_content_section, line); } @@ -303,6 +306,7 @@ pub fn get_sections( is_reading_property_block, settings, &error_information, + locales, )?; Ok(sections) @@ -312,18 +316,32 @@ pub fn get_sections( fn check_parsing_error( is_reading_code_block: bool, is_reading_property_block: bool, - settings: &MainPluginSettings, + settings: &PluginSettings, error_information: &ErrorInformation, + locales: &Value, ) -> Result<(), Box> { + use crate::utils::{get_locale_string, LocaleCategory}; + if is_reading_code_block || is_reading_property_block { let error_message = if let Some(true) = settings.other_options.show_more_detailed_error_messages { - format!( - "Failed to parse the document. [Reading line: {}]", - error_information.reading_section_starting_line + 1 - ) + let mut msg = get_locale_string( + locales, + LocaleCategory::Parsing, + "Failed to parse the document. [Line: {LINE_NUMBER}]", + ); + msg = msg.replace( + "{LINE_NUMBER}", + &(error_information.reading_section_starting_line + 1).to_string(), + ); + + return Err(msg.into()); } else { - String::from("Failed to parse the document.") + get_locale_string( + locales, + LocaleCategory::Parsing, + "Failed to parse the document.", + ) }; return Err(error_message.into()); diff --git a/wasm/src/utils.rs b/wasm/src/utils.rs index c9a4fa0..3c3ddcd 100644 --- a/wasm/src/utils.rs +++ b/wasm/src/utils.rs @@ -1,3 +1,5 @@ +use serde_json::Value; + // To use the feature, use the following argument. // "--features development" // ex) "wasm-pack build --target web --features development" @@ -12,3 +14,24 @@ pub fn set_panic_hook() { #[cfg(all(feature = "development", feature = "console_error_panic_hook"))] console_error_panic_hook::set_once(); } + +pub enum LocaleCategory { + Parsing, + Formatting, +} + +pub fn get_locale_string(locales: &Value, category: LocaleCategory, key: &str) -> String { + match category { + LocaleCategory::Parsing => { + if let Some(msg) = locales["parsing"][key].as_str() { + return String::from(msg); + } + } + LocaleCategory::Formatting => { + if let Some(msg) = locales["formatting"][key].as_str() { + return String::from(msg); + } + } + } + String::from(key) +}