silverbullet icon indicating copy to clipboard operation
silverbullet copied to clipboard

Content language in SETTINGS

Open towerberlin opened this issue 1 year ago • 6 comments

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.

towerberlin avatar Feb 20 '24 04:02 towerberlin

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.

joekrill avatar Feb 20 '24 23:02 joekrill

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?

inos-github avatar Apr 16 '24 09:04 inos-github

@inos-github sorry that should be:

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

(I fixed the original comment, too)

joekrill avatar Apr 16 '24 10:04 joekrill

@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

inos-github avatar Apr 16 '24 16:04 inos-github

@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;
  }
});
```

joekrill avatar Apr 17 '24 00:04 joekrill

Now it works, thank you very much!

inos-github avatar Apr 17 '24 05:04 inos-github