Update i18n and add svelte-i18next package

This commit is contained in:
Roger Veciana 2024-01-21 20:26:18 +01:00
parent 9b3404d119
commit 2a4e4effa5
7 changed files with 112 additions and 37 deletions

View file

@ -7,9 +7,7 @@
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:svelte/recommended",
"prettier"
],
"plugin:svelte/recommended" ],
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2020,

55
package-lock.json generated
View file

@ -21,6 +21,7 @@
"obsidian": "0.15.9",
"svelte": "^3.49.0",
"svelte-check": "^2.8.0",
"svelte-i18next": "^2.2.2",
"svelte-preprocess": "^5.1.3",
"tslib": "2.4.0",
"typescript": "5.3.3",
@ -36,6 +37,19 @@
"node": ">=0.10.0"
}
},
"node_modules/@babel/runtime": {
"version": "7.23.8",
"resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.23.8.tgz",
"integrity": "sha512-Y7KbAP984rn1VGMbGqKmBLio9V7y5Je9GvU4rQPCPinCyNfUcToxIXl06d59URp/F3LwinvODxab5N/G6qggkw==",
"dev": true,
"peer": true,
"dependencies": {
"regenerator-runtime": "^0.14.0"
},
"engines": {
"node": ">=6.9.0"
}
},
"node_modules/@codemirror/state": {
"version": "6.4.0",
"resolved": "https://registry.npmjs.org/@codemirror/state/-/state-6.4.0.tgz",
@ -1677,6 +1691,30 @@
"node": ">=8"
}
},
"node_modules/i18next": {
"version": "23.7.18",
"resolved": "https://registry.npmjs.org/i18next/-/i18next-23.7.18.tgz",
"integrity": "sha512-b9N2KjRCYQNlUvE1Kc83g8knyUkL5NiZQOp9BsTR/v/LXk6Fzz+doOzTg2/826XK28mCgBkYLNAtixjE58qpCw==",
"dev": true,
"funding": [
{
"type": "individual",
"url": "https://locize.com"
},
{
"type": "individual",
"url": "https://locize.com/i18next.html"
},
{
"type": "individual",
"url": "https://www.i18next.com/how-to/faq#i18next-is-awesome.-how-can-i-support-the-project"
}
],
"peer": true,
"dependencies": {
"@babel/runtime": "^7.23.2"
}
},
"node_modules/ignore": {
"version": "5.3.0",
"resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.0.tgz",
@ -2333,6 +2371,13 @@
"node": ">=8.10.0"
}
},
"node_modules/regenerator-runtime": {
"version": "0.14.1",
"resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.14.1.tgz",
"integrity": "sha512-dYnhHh0nJoMfnkZs6GmmhFknAGRrLznOu5nc9ML+EJxGvrx6H7teuevqVqCuPcPK//3eDrrjQhehXVx9cnkGdw==",
"dev": true,
"peer": true
},
"node_modules/resolve": {
"version": "1.22.1",
"resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.1.tgz",
@ -2791,6 +2836,16 @@
"svelte": ">=3.19.0"
}
},
"node_modules/svelte-i18next": {
"version": "2.2.2",
"resolved": "https://registry.npmjs.org/svelte-i18next/-/svelte-i18next-2.2.2.tgz",
"integrity": "sha512-IpJDZCH5cCgKfHQHgiLmGT4j9HCdg4fqsP3oP2deLu8PxmNj0Ui6khMiDoxAxedAiYEhr0xendv2xqh3Rq+uQQ==",
"dev": true,
"peerDependencies": {
"i18next": "*",
"svelte": "*"
}
},
"node_modules/svelte-preprocess": {
"version": "5.1.3",
"resolved": "https://registry.npmjs.org/svelte-preprocess/-/svelte-preprocess-5.1.3.tgz",

View file

@ -25,6 +25,7 @@
"obsidian": "0.15.9",
"svelte": "^3.49.0",
"svelte-check": "^2.8.0",
"svelte-i18next": "^2.2.2",
"svelte-preprocess": "^5.1.3",
"tslib": "2.4.0",
"typescript": "5.3.3",

24
src/lang/i18n.ts Normal file
View file

@ -0,0 +1,24 @@
import i18next from "i18next";
import { createI18nStore } from "svelte-i18next";
i18next.init({
lng: 'en',
fallbackLng: 'en',
resources: {
en: {
translation: {
"empty": "Empty recipe. Edit it using the pencil icon.",
"method": "Method",
"ingredients": "Ingredients",
"cookware": "Cookware",
"step": "Step",
}
}
},
interpolation: {
escapeValue: false, // not needed for svelte as it escapes by default
}
});
export const i18n = createI18nStore(i18next);
export default i18n;

View file

@ -2,14 +2,17 @@ import {
App,
Plugin,
PluginSettingTab,
TFile,
TextFileView,
WorkspaceLeaf,
setIcon,
} from "obsidian";
import { DEFAULT_SETTINGS, type CookLangSettings } from "./settings";
import Edit from "./ui/Edit.svelte";
import View from "./ui/View.svelte";
import { isTFile } from "./ui/utils";
const VIEW_TYPE = "svelte-view";
@ -63,27 +66,26 @@ class CooklangSvelteView extends TextFileView {
return this.data;
}
setViewData(data: string): void {
// const images = (
// this.file.parent.children.filter(
// (f) =>
// isTFile(f) &&
// (f.basename === this.file.basename ||
// f.basename.startsWith(this.file.basename + ".")) &&
// f.name != this.file.name &&
// ["png", "jpg", "jpeg", "gif"].includes(f.extension)
// ) as TFile[]
// ).reduce((acc, f) => {
// const split = f.basename.split(".");
// if (split.length > 1) {
// const name = split[1];
// acc[name] = this.app.vault.getResourcePath(f);
// } else {
// acc["recipe"] = this.app.vault.getResourcePath(f);
// }
// return acc;
// }, {} as Record<string, string>);
// this.images = images;
const images = {};
const images = (
this.file.parent.children.filter(
(f) => isTFile(f) &&
(f.basename === this.file.basename ||
f.basename.startsWith(this.file.basename + ".")) &&
f.name != this.file.name &&
["png", "jpg", "jpeg", "gif"].includes(f.extension)
) as TFile[]
).reduce((acc, f) => {
const split = f.basename.split(".");
if (split.length > 1) {
const name = split[1];
acc[name] = this.app.vault.getResourcePath(f);
} else {
acc["recipe"] = this.app.vault.getResourcePath(f);
}
return acc;
}, {} as Record<string, string>);
this.images = images;
this.data = data;
this.view.$set({ data, images });
@ -110,7 +112,7 @@ class CooklangSvelteView extends TextFileView {
target: this.contentEl,
props: {
data: this.data,
onChange: (newData) => (this.data = newData),
onChange: (newData:string) => (this.data = newData),
},
});
}

View file

@ -1,23 +1,18 @@
<script lang="ts">
import { Parser, type ParseResult } from '@cooklang/cooklang-ts';
import { tooltip } from '@svelte-plugins/tooltips';
import i18n from '../lang/i18n';
export let data:string;
export let images: Record<string, string> = {}
let recipe: ParseResult;
$: recipe = new Parser().parse(data);
</script>
<div>
{#if data.length === 0}
<p>Empty recipe. Edit it using the pencil icon.</p>
<p>{$i18n.t('empty')}</p>
{:else}
@ -26,7 +21,7 @@
{/if}
{#if recipe.ingredients.length > 0}
<section class="section">
<h3 class="section-title">Ingredients:</h3>
<h3 class="section-title">{$i18n.t('ingredients')}</h3>
<ul class="ingredients">
{#each recipe.ingredients as ingredient}
<li>{ingredient.quantity} {ingredient.units} {ingredient.name}</li>
@ -36,7 +31,7 @@
{/if}
{#if recipe.cookwares.length > 0}
<section class="section">
<h3 class="section-title">Cookware:</h3>
<h3 class="section-title">{$i18n.t('cookware')}</h3>
<ul class="cookware">
{#each recipe.cookwares as cookware}
<li>{cookware.name}</li>
@ -44,11 +39,11 @@
</ul>
</section>
{/if}
<h2 class="H2">Method:</h2>
<h2 class="H2">{$i18n.t('method')}:</h2>
<div>
{#each recipe.steps as step, i}
<h3>Step {i+1}</h3>
<h3>{$i18n.t('step')} {i+1}</h3>
{#if images[i]}
<img src={images[i]} alt="Final result" />
{/if}

View file

@ -23,5 +23,5 @@ export const isTimer = (
};
export const isTFile = (file: TAbstractFile): file is TFile => {
return (file as TFile).path !== undefined;
return (file as TFile).basename !== undefined;
};