docs: add "setting the language" example (#1265)

This commit is contained in:
Reed von Redwitz
2023-06-11 02:38:25 +09:00
committed by GitHub
parent 503bdc384f
commit 931031f9ce
3 changed files with 57 additions and 1 deletions
+1
View File
@@ -9,3 +9,4 @@ like to see here, please open
[a GitHub discussion](https://github.com/denoland/fresh/discussions/new?category=ideas).
- [Modifying the `<head>`](./examples/modifying-the-head)
- [Setting the language](./examples/setting-the-language)
+52
View File
@@ -0,0 +1,52 @@
---
description: |
Set the lang attribute in the <html> tag.
---
When you initialize a project with `deno run -A -r https://fresh.deno.dev`,
you'll end up with a `main.ts` like the following:
```
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
import twindPlugin from "$fresh/plugins/twind.ts";
import twindConfig from "./twind.config.ts";
await start(manifest, { plugins: [twindPlugin(twindConfig)] });
```
This is a great start if your site is in English, but let's say you want to
change the language, as per the `<html lang=asdf>` tag. Then you'll need to do
something like this:
```
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
import twindPlugin from "$fresh/plugins/twind.ts";
import twindConfig from "./twind.config.ts";
await start(manifest, {
plugins: [twindPlugin(twindConfig)],
render: (ctx, render) => {
ctx.lang = "de";
render();
},
});
```
If you're curious how this works, start by checking out `TemplateOptions` in
`render.ts`.
+4 -1
View File
@@ -39,6 +39,9 @@
},
"examples": {
"title": "Examples",
"pages": [["modifying-the-head", "Modifying the <head>"]]
"pages": [
["modifying-the-head", "Modifying the <head>"],
["setting-the-language", "Setting the language"]
]
}
}