Compare commits

..
1 Commits
Author SHA1 Message Date
Petar Petrov aec4b70eab Translate common YAML parse error reasons in the editor 2026-11-10 20:15:39 +02:00
3 changed files with 56 additions and 3 deletions

No files matched your search

@@ -0,0 +1,33 @@
import type { LocalizeFunc } from "./localize";
const YAML_ERROR_REASON_KEYS = new Map(
Object.entries({
"duplicated mapping key": "duplicated_mapping_key",
"bad indentation of a mapping entry": "bad_mapping_indentation",
"bad indentation of a sequence entry": "bad_sequence_indentation",
"deficient indentation": "deficient_indentation",
"tab characters must not be used in indentation": "tab_indentation",
"can not read a block mapping entry; a multiline key may not be an implicit key":
"multiline_implicit_key",
"a whitespace character is expected after the key-value separator within a block mapping":
"missing_key_value_space",
"expected ':' after a mapping key": "missing_colon",
"missed comma between flow collection entries": "missing_comma",
"unexpected end of the stream within a flow collection":
"unterminated_flow_collection",
"unexpected end of the stream within a single quoted scalar":
"unterminated_single_quote",
"unexpected end of the stream within a double quoted scalar":
"unterminated_double_quote",
"end of the stream or a document separator is expected":
"unexpected_content",
} as const)
);
export const localizeYamlErrorReason = (
localize: LocalizeFunc,
reason: string | undefined
): string | undefined => {
const key = reason ? YAML_ERROR_REASON_KEYS.get(reason) : undefined;
return key ? localize(`ui.components.yaml-editor.reasons.${key}`) : reason;
};
+8 -3
View File
@@ -6,6 +6,7 @@ import { customElement, property, query, state } from "lit/decorators";
import type { ContextType } from "@lit/context";
import { consume } from "../common/decorators/consume";
import { fireEvent } from "../common/dom/fire_event";
import { localizeYamlErrorReason } from "../common/translations/localize-yaml-error";
import { copyToClipboard } from "../common/util/copy-clipboard";
import { haStyle } from "../resources/styles";
import { showToast } from "../util/toast";
@@ -180,7 +181,7 @@ export class HaYamlEditor extends LitElement {
let errorMsg: string | undefined;
let yamlError: {
mark?: { position: number; line: number; column: number };
message?: string;
reason?: string;
} | null = null;
if (this._yaml) {
@@ -189,8 +190,12 @@ export class HaYamlEditor extends LitElement {
} catch (err: any) {
// Invalid YAML
isValid = false;
yamlError = err;
errorMsg = `${this._i18n!.localize("ui.components.yaml-editor.error", { reason: err.reason })}${err.mark ? ` (${this._i18n!.localize("ui.components.yaml-editor.error_location", { line: err.mark.line + 1, column: err.mark.column + 1 })})` : ""}`;
const reason = localizeYamlErrorReason(
this._i18n!.localize,
err.reason
);
yamlError = { mark: err.mark, reason };
errorMsg = `${this._i18n!.localize("ui.components.yaml-editor.error", { reason })}${err.mark ? ` (${this._i18n!.localize("ui.components.yaml-editor.error_location", { line: err.mark.line + 1, column: err.mark.column + 1 })})` : ""}`;
}
} else {
parsed = {};
+15
View File
@@ -1610,6 +1610,21 @@
"syntax_error": "YAML syntax error",
"error": "Error in parsing YAML: {reason}",
"error_location": "line: {line}, column: {column}",
"reasons": {
"duplicated_mapping_key": "Duplicated mapping key",
"bad_mapping_indentation": "Bad indentation of a mapping entry",
"bad_sequence_indentation": "Bad indentation of a sequence entry",
"deficient_indentation": "Deficient indentation",
"tab_indentation": "Tab characters must not be used in indentation",
"multiline_implicit_key": "Can not read a block mapping entry; a multiline key may not be an implicit key",
"missing_key_value_space": "A whitespace character is expected after the key-value separator within a block mapping",
"missing_colon": "Expected ':' after a mapping key",
"missing_comma": "Missed comma between flow collection entries",
"unterminated_flow_collection": "Unexpected end of the stream within a flow collection",
"unterminated_single_quote": "Unexpected end of the stream within a single quoted scalar",
"unterminated_double_quote": "Unexpected end of the stream within a double quoted scalar",
"unexpected_content": "End of the stream or a document separator is expected"
},
"enter_fullscreen": "Enter fullscreen",
"exit_fullscreen": "Exit fullscreen",
"find_and_replace": "Find and replace",