silverbulletmd / silverbullet

The hackable notebook
https://silverbullet.md
MIT License
2.06k stars 146 forks source link

Content language in SETTINGS #730

Open towerberlin opened 4 months ago

towerberlin commented 4 months ago

Is it possible to define the content language in SETTINGS?

grafik

I write my notes in German and in SB, for example, the spell check only works for English content.

joekrill commented 4 months ago

I think this could be done pretty easily with a plug. Maybe have a default language and allow it to be overridden with a Frontmatter tag? I wonder how this would play into potential plans (if any) for localizing the app?

For now, I think I have a workaround for you:

First, create this custom function somewhere in your space (I created a page called SetLanguageFunction, but you can put it on any page, really). Replace "en" with whatever you want the default language to be.

```space-script
silverbullet.registerFunction("setLang", (lang = "en") => {
  document.documentElement.lang = lang;
});

Then create a page (I called it "SetLanguageWidget") as a widget  that runs it on every page and sets the language:

tags: template hooks.top.where: 'true'

{{#if setLang(@page.language)}}{{/if}}


Then you can override the language for a specific page using Frontmatter:

language: de

This page should have the <html> tag's lang attribute set to "de"!



There may be a more elegant way to handle it - I'm still pretty new to SilverBullet.
inos-github commented 2 months ago

This workaround doesn't work for me (Silverbullet Version 0.7.6)
Console-log from browser: Error loading space-script: Error evaluating script: expected expression, got ';' for script: silverbullet.registerFunction("setLang", (lang = "en") => { document.documentElement.lang = lang ||; });

Any idea how to fix this?

joekrill commented 2 months ago

@inos-github sorry that should be:

```space-script
silverbullet.registerFunction("setLang", (lang = "en") => {
  document.documentElement.lang = lang;
});
```

(I fixed the original comment, too)

inos-github commented 2 months ago

@joekrill thank you for the replay an the fix! Now I have another error :

Remote syscall error document is not defined
An exception was thrown as a result of invoking function renderTemplateWidgetsTop error: document is not defined
Error: document is not defined
    onMessage worker_sandbox.ts:93
    onmessage worker_sandbox.ts:59

I'm using the actual Firefox Browser on Linux

joekrill commented 2 months ago

@inos-github hmm, I guess SilverBullet also runs this code on the server? You can try something like this:

```space-script
silverbullet.registerFunction("setLang", (lang = "en") => {
  if (typeof document !== "undefined") {
   document.documentElement.lang = lang;
  }
});
```
inos-github commented 2 months ago

Now it works, thank you very much!