From 9694e1d57d400d9e7a1569ed350535463eba3b52 Mon Sep 17 00:00:00 2001 From: Yufei Huang Date: Thu, 31 Aug 2023 13:02:42 +0800 Subject: [PATCH 1/2] feat: support modern template localization --- templates/default(zh-cn)/token.json | 14 +++++++++++--- templates/default/token.json | 14 +++++++++++--- templates/modern/layout/_master.tmpl | 14 ++++++++++++-- templates/modern/src/helper.ts | 15 +++++++++++++++ templates/modern/src/markdown.ts | 4 ++-- templates/modern/src/nav.ts | 6 +++--- templates/modern/src/search.ts | 6 +++--- templates/modern/src/theme.ts | 7 ++++--- templates/modern/src/toc.ts | 10 +++++----- 9 files changed, 66 insertions(+), 24 deletions(-) diff --git a/templates/default(zh-cn)/token.json b/templates/default(zh-cn)/token.json index d208971bcf9..42e1330a7a1 100644 --- a/templates/default(zh-cn)/token.json +++ b/templates/default(zh-cn)/token.json @@ -11,7 +11,7 @@ "eventsInSubtitle": "事件", "operatorsInSubtitle": "运算符", "eiisInSubtitle": "显示接口实现", - "improveThisDoc": "改善此文档", + "improveThisDoc": "编辑本文", "viewSource": "查看源代码", "inheritance": "继承", "inheritedMembers": "继承成员", @@ -42,12 +42,20 @@ "important": "重要事项", "caution": "小心", "tocToggleButton": "显示/隐藏目录", - "tocFilter": "按标题筛选...", + "tocFilter": "按标题筛选", "search": "搜索", "searchResults": "搜索结果", + "searchResultsCount": "找到{count}个“{query}”的搜索结果", + "searchNoResults": "没有找到“{query}”的搜索结果", "pageFirst": "首页", "pagePrev": "上一页", "pageNext": "下一页", "pageLast": "尾页", - "inThisArticle": "本文内容" + "inThisArticle": "本文内容", + "nextArticle": "下一篇", + "prevArticle": "上一篇", + "themeLight": "白天", + "themeDark": "夜晚", + "themeAuto": "自动", + "copy": "复制" } diff --git a/templates/default/token.json b/templates/default/token.json index c0fc6c3dda2..6f2c71fd7f5 100644 --- a/templates/default/token.json +++ b/templates/default/token.json @@ -16,7 +16,7 @@ "variablesInSubtitle": "Variables", "typeAliasesInSubtitle": "Type Aliases", "membersInSubtitle": "Members", - "improveThisDoc": "Improve this Doc", + "improveThisDoc": "Edit this page", "viewSource": "View Source", "inheritance": "Inheritance", "derived": "Derived", @@ -51,13 +51,21 @@ "important": "Important", "caution": "Caution", "tocToggleButton": "Show / Hide Table of Contents", - "tocFilter": "Enter here to filter...", + "tocFilter": "Filter by title", "search": "Search", "searchResults": "Search Results for", + "searchResultsCount": "{count} results for \"{query}\"", + "searchNoResults": "No results for \"{query}\"", "pageFirst": "First", "pagePrev": "Previous", "pageNext": "Next", "pageLast": "Last", "inThisArticle": "In This Article", - "backToTop": "Back to top" + "nextArticle": "Next", + "prevArticle": "Previous", + "backToTop": "Back to top", + "themeLight": "Light", + "themeDark": "Dark", + "themeAuto": "Auto", + "copy": "Copy" } diff --git a/templates/modern/layout/_master.tmpl b/templates/modern/layout/_master.tmpl index acb86cc9f3f..ce60ba3adb3 100644 --- a/templates/modern/layout/_master.tmpl +++ b/templates/modern/layout/_master.tmpl @@ -25,6 +25,16 @@ {{#_disableNewTab}}{{/_disableNewTab}} {{#_disableTocFilter}}{{/_disableTocFilter}} {{#docurl}}{{/docurl}} + + + + + + + + + + {{/redirect_url}} @@ -106,10 +116,10 @@ {{^_disableContribution}}
{{#sourceurl}} - Edit this page + {{__global.improveThisDoc}} {{/sourceurl}} {{^sourceurl}}{{#docurl}} - Edit this page + {{__global.improveThisDoc}} {{/docurl}}{{/sourceurl}}
{{/_disableContribution}} diff --git a/templates/modern/src/helper.ts b/templates/modern/src/helper.ts index 78e95f79865..fffb093a98d 100644 --- a/templates/modern/src/helper.ts +++ b/templates/modern/src/helper.ts @@ -10,6 +10,21 @@ export function meta(name: string): string { return (document.querySelector(`meta[name="${name}"]`) as HTMLMetaElement)?.content } +/** + * Gets the localized text. + * @param id key in token.json + * @param args arguments to replace in the localized text + */ +export function loc(id: string, args?: any): string { + let result = meta(`loc:${id}`) || id + if (args) { + for (const key in args) { + result = result.replace(`{${key}}`, args[key]) + } + } + return result +} + /** * Add into long word. */ diff --git a/templates/modern/src/markdown.ts b/templates/modern/src/markdown.ts index 1ff54830637..4dfc0e1f565 100644 --- a/templates/modern/src/markdown.ts +++ b/templates/modern/src/markdown.ts @@ -1,7 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -import { breakWord, meta } from './helper' +import { breakWord, meta, loc } from './helper' import AnchorJs from 'anchor-js' import { html, render } from 'lit-html' import { getTheme } from './theme' @@ -186,7 +186,7 @@ function renderCodeCopy() { function renderCore() { const dom = copied ? html`` - : html`` + : html`` render(dom, code.parentElement) async function copy(e) { diff --git a/templates/modern/src/nav.ts b/templates/modern/src/nav.ts index 45ca72ba75b..4fc6267ab4a 100644 --- a/templates/modern/src/nav.ts +++ b/templates/modern/src/nav.ts @@ -2,7 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. import { render, html, TemplateResult } from 'lit-html' -import { breakWordLit, meta, isExternalHref } from './helper' +import { breakWordLit, meta, isExternalHref, loc } from './helper' import { themePicker } from './theme' import { TocNode } from './toc' @@ -105,7 +105,7 @@ function inThisArticleForConceptual() { const headings = document.querySelectorAll('article h2') if (headings.length > 0) { return html` -
In this article
+
${loc('inThisArticle')}
` } } @@ -116,7 +116,7 @@ function inThisArticleForManagedReference(): TemplateResult { if (headings.length > 0) { return html` -
In this article
+
${loc('inThisArticle')}