Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(i18n):-yaml-to-toml-&-json-to-yaml #234

Open
wants to merge 1 commit into
base: developing/2.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/composable/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type ValidatorReturnType = unknown;

export interface UseValidationRule<T> {
validator: (value: T) => ValidatorReturnType
message: string
message: any
}

export function isFalsyOrHasThrown(cb: () => ValidatorReturnType): boolean {
Expand Down Expand Up @@ -56,7 +56,6 @@ export function useValidation<T>({
watch(
[source, ...watchRefs],
() => {
state.message = '';
state.status = undefined;

for (const rule of get(rules)) {
Expand Down
9 changes: 5 additions & 4 deletions src/tools/json-to-yaml-converter/json-to-yaml.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ import type { UseValidationRule } from '@/composable/validation';
import { isNotThrowing } from '@/utils/boolean';
import { withDefaultOnError } from '@/utils/defaults';

const { t } = useI18n();
const transformer = (value: string) => withDefaultOnError(() => stringify(JSON5.parse(value)), '');

const rules: UseValidationRule<string>[] = [
{
validator: (value: string) => value === '' || isNotThrowing(() => stringify(JSON5.parse(value))),
message: 'Provided JSON is not valid.',
message: computed(() => t('tools.json-to-yaml-converter.invalid')),
},
];
</script>

<template>
<format-transformer
input-label="Your JSON"
input-placeholder="Paste your JSON here..."
output-label="YAML from your JSON"
:input-label="t('tools.json-to-yaml-converter.your-json')"
:input-placeholder="t('tools.json-to-yaml-converter.placeholder.your-json')"
:output-label="t('tools.json-to-yaml-converter.yaml-from-json')"
output-language="yaml"
:input-validation-rules="rules"
:transformer="transformer"
Expand Down
9 changes: 9 additions & 0 deletions src/tools/json-to-yaml-converter/locales/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
tools:
json-to-yaml-converter:
title: 'JSON to YAML converter'
description: 'Simply convert JSON to YAML with this live online converter.'
invalid: 'Provided JSON is not valid.'
your-json: 'Your JSON'
yaml-from-json: 'YAML from your JSON'
placeholder:
your-json: 'Paste your JSON here...'
9 changes: 9 additions & 0 deletions src/tools/json-to-yaml-converter/locales/zh.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
tools:
json-to-yaml-converter:
title: 'JSON to YAML'
description: '使用这个在线转换工具,简单地将JSON转换为YAML。'
invalid: '提供的JSON有问题'
your-json: '原始JSON'
yaml-from-json: '转换后的YAML'
placeholder:
your-json: '在这粘贴你的JSON...'
9 changes: 9 additions & 0 deletions src/tools/yaml-to-toml/locales/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
tools:
yaml-to-toml:
title: 'YAML to TOML converter'
description: 'Parse and convert YAML to TOML.'
invalid: 'Provided JSON is not valid.'
your-yaml: 'Your YAML'
toml-from-yaml: 'TOML from your YAML'
placeholder:
your-yaml: 'Paste your YAML here...'
9 changes: 9 additions & 0 deletions src/tools/yaml-to-toml/locales/zh.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
tools:
yaml-to-toml:
title: 'YAML to TOML'
description: '解析并转换 YAML 到 TOML.'
invalid: '提供的YAML有问题'
your-yaml: '原始YAML'
toml-from-yaml: '转换后的TOML'
placeholder:
your-yaml: '在这粘贴你的YAML...'
11 changes: 6 additions & 5 deletions src/tools/yaml-to-toml/yaml-to-toml.vue
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
<script setup lang="ts">
import { stringify as stringifyToml } from 'iarna-toml-esm';
import { parse as parseYaml } from 'yaml';
import { withDefaultOnError } from '../../utils/defaults';
import { withDefaultOnError } from '@/utils/defaults';
import type { UseValidationRule } from '@/composable/validation';

const { t } = useI18n();
const convertYamlToToml = (value: string) => [stringifyToml(parseYaml(value))].flat().join('\n').trim();

const transformer = (value: string) => value.trim() === '' ? '' : withDefaultOnError(() => convertYamlToToml(value), '');

const rules: UseValidationRule<string>[] = [
{
validator: (v: string) => v === '' || parseYaml(v),
message: 'Provided JSON is not valid.',
message: computed(() => t('tools.yaml-to-toml.invalid')),
},
];
</script>

<template>
<format-transformer
input-label="Your YAML"
input-placeholder="Paste your YAML here..."
output-label="TOML from your YAML"
:input-label="t('tools.yaml-to-toml.your-yaml')"
:input-placeholder="t('tools.yaml-to-toml.placeholder.your-yaml')"
:output-label="t('tools.yaml-to-toml.toml-from-yaml')"
output-language="toml"
:input-validation-rules="rules"
:transformer="transformer"
Expand Down