Skip to content

Commit 0e3e559

Browse files
committed
localized project page description in cloudflare worker
1 parent 5af30eb commit 0e3e559

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

web/cloudflare/functions/projects/[slug].ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import htmlTemplate from "../../public/template.html";
55
import { fsConfig } from "@dzcode.io/utils/dist/config";
66
import { Environment, environments } from "@dzcode.io/utils/dist/config/environment";
77
import { plainLocalize } from "@dzcode.io/web/dist/components/locale/utils";
8-
import { dictionary } from "@dzcode.io/web/dist/components/locale/dictionary";
8+
import { dictionary, AllDictionaryKeys } from "@dzcode.io/web/dist/components/locale/dictionary";
99

1010
// @TODO-ZM: pass envs during deployment
1111
export interface Env {
@@ -32,15 +32,18 @@ export const onRequest: PagesFunction<Env> = async (context) => {
3232

3333
// @TODO-ZM: get language from request url
3434
const language = "en";
35-
const localize = (key: string) => plainLocalize(dictionary, language, key, "NO-TRANSLATION");
35+
const localize = (key: AllDictionaryKeys) =>
36+
plainLocalize(dictionary, language, key, "NO-TRANSLATION");
3637

3738
// @TODO-ZM: use fetchV2
3839
const projectResponse = await fetch(`${apiUrl}/Projects/${projectId}/name`);
3940
const projectData = await projectResponse.json();
4041
// @ts-expect-error @TODO-ZM: import @dzcode.io/api
4142
const pageTitle = `${localize("project-title-pre")} ${projectData.project.name} ${localize("project-title-post")}`;
4243

43-
const newData = htmlTemplate.replace(/{{template-title}}/g, pageTitle);
44+
const newData = htmlTemplate
45+
.replace(/{{template-title}}/g, pageTitle)
46+
.replace(/{{template-description}}/g, localize("projects-description"));
4447

4548
return new Response(newData, {
4649
headers: {

web/cloudflare/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"deploy:prd": "wrangler pages deploy --project-name dzcode-dot-io --directory=./public --branch main",
1717
"deploy:stg": "wrangler pages deploy --project-name stage-dot-dzcode-dot-io --directory=./public --branch main",
1818
"dev": "npm run build && wrangler pages dev . --port 8080",
19-
"generate:htmls": "cd .. && npm run bundle && npm run pre-deploy",
19+
"dev:prepare": "npm run build && cd .. && npm run bundle && npm run pre-deploy",
2020
"lint:eslint": "eslint --config ../../packages/tooling/eslint.config.mjs",
2121
"lint:prettier": "prettier --config ../../packages/tooling/.prettierrc --ignore-path ../../packages/tooling/.prettierignore --log-level warn"
2222
}

web/src/_build/pages/static-pages.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { allLanguages } from "@dzcode.io/models/dist/language";
22
import { plainLocalize } from "../../components/locale/utils";
3-
import { dictionary } from "../../components/locale/dictionary";
3+
import { dictionary, AllDictionaryKeys } from "../../components/locale/dictionary";
44

55
import { PageInfo, PageInfoWithLocalKeys } from ".";
66

7-
const localize = (key: string, language: string) =>
7+
const localize = (key: AllDictionaryKeys, language: string) =>
88
plainLocalize(dictionary, language, key, "NO-TRANSLATION");
99

1010
const staticURLs: PageInfoWithLocalKeys[] = [

web/src/components/locale/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { BaseDictionary } from "./types";
22

3-
export function plainLocalize(
4-
dictionary: BaseDictionary,
3+
export function plainLocalize<T extends BaseDictionary>(
4+
dictionary: T,
55
languageCode: string,
6-
key: string,
6+
key: keyof T,
77
fallbackText: string,
88
) {
99
return dictionary[key]?.[languageCode] || fallbackText;

0 commit comments

Comments
 (0)