@astrojs/starlight 0.0.13 → 0.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @astrojs/starlight
2
2
 
3
+ ## 0.0.14
4
+
5
+ ### Patch Changes
6
+
7
+ - [#95](https://github.com/withastro/starlight/pull/95) [`de24b54`](https://github.com/withastro/starlight/commit/de24b54971577912979a3fb67570f4c95efe27a6) Thanks [@delucis](https://github.com/delucis)! - Support translations in sidebar config
8
+
9
+ - [#97](https://github.com/withastro/starlight/pull/97) [`2d51762`](https://github.com/withastro/starlight/commit/2d517623fb8670d4e7f2656eacff0d5beb27d95a) Thanks [@morinokami](https://github.com/morinokami)! - Add Japanese language support
10
+
3
11
  ## 0.0.13
4
12
 
5
13
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/starlight",
3
- "version": "0.0.13",
3
+ "version": "0.0.14",
4
4
  "description": "Build beautiful, high-performance documentation websites with Astro",
5
5
  "keywords": [
6
6
  "docs",
@@ -2,9 +2,10 @@ import { i18nSchema } from '../schemas/i18n';
2
2
  import en from './en.json';
3
3
  import es from './es.json';
4
4
  import de from './de.json';
5
+ import ja from './ja.json';
5
6
 
6
7
  const parse = i18nSchema().required().strict().parse;
7
8
 
8
9
  export default Object.fromEntries(
9
- Object.entries({ en, es, de }).map(([key, dict]) => [key, parse(dict)])
10
+ Object.entries({ en, es, de, ja }).map(([key, dict]) => [key, parse(dict)])
10
11
  );
@@ -0,0 +1,20 @@
1
+ {
2
+ "skipLink.label": "コンテンツにスキップ",
3
+ "search.label": "検索",
4
+ "search.shortcutLabel": "(/を押して検索)",
5
+ "search.cancelLabel": "キャンセル",
6
+ "themeSelect.accessibleLabel": "テーマの選択",
7
+ "themeSelect.dark": "ダーク",
8
+ "themeSelect.light": "ライト",
9
+ "themeSelect.auto": "自動",
10
+ "languageSelect.accessibleLabel": "言語の選択",
11
+ "menuButton.accessibleLabel": "メニュー",
12
+ "sidebarNav.accessibleLabel": "メイン",
13
+ "tableOfContents.onThisPage": "目次",
14
+ "tableOfContents.overview": "概要",
15
+ "i18n.untranslatedContent": "このコンテンツはまだ日本語訳がありません。",
16
+ "page.editLink": "ページを編集",
17
+ "page.lastUpdated": "最終更新日:",
18
+ "page.previousLink": "前へ",
19
+ "page.nextLink": "次へ"
20
+ }
package/utils/i18n.ts ADDED
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Get the string for the passed language from a dictionary object.
3
+ *
4
+ * TODO: Make this clever. Currently a simple key look-up, but should use
5
+ * BCP-47 mapping so that e.g. `en-US` returns `en` strings, and use the
6
+ * site’s default locale as a last resort.
7
+ *
8
+ * @example
9
+ * pickLang({ en: 'Hello', fr: 'Bonjour' }, 'en'); // => 'Hello'
10
+ */
11
+ export function pickLang<T extends Record<string, string>>(
12
+ dictionary: T,
13
+ lang: keyof T
14
+ ): string | undefined {
15
+ return dictionary[lang];
16
+ }
@@ -1,8 +1,9 @@
1
1
  import { basename, dirname } from 'node:path';
2
2
  import config from 'virtual:starlight/user-config';
3
3
  import { withBase } from './base';
4
+ import { pickLang } from './i18n';
4
5
  import { Route, getLocaleRoutes, routes } from './routing';
5
- import { slugToPathname } from './slugs';
6
+ import { localeToLang, slugToPathname } from './slugs';
6
7
  import type {
7
8
  AutoSidebarGroup,
8
9
  SidebarItem,
@@ -48,7 +49,7 @@ function configItemToEntry(
48
49
  } else {
49
50
  return {
50
51
  type: 'group',
51
- label: item.label,
52
+ label: pickLang(item.translations, localeToLang(locale)) || item.label,
52
53
  entries: item.items.map((i) =>
53
54
  configItemToEntry(i, currentPathname, locale, routes)
54
55
  ),
@@ -75,7 +76,7 @@ function groupFromAutogenerateConfig(
75
76
  const tree = treeify(dirDocs, localeDir);
76
77
  return {
77
78
  type: 'group',
78
- label: item.label,
79
+ label: pickLang(item.translations, localeToLang(locale)) || item.label,
79
80
  entries: sidebarFromDir(tree, currentPathname, locale),
80
81
  };
81
82
  }
@@ -102,7 +103,8 @@ function linkFromConfig(
102
103
  // Inject current locale into link.
103
104
  if (locale) href = '/' + locale + href;
104
105
  }
105
- return makeLink(href, item.label, currentPathname);
106
+ const label = pickLang(item.translations, localeToLang(locale)) || item.label;
107
+ return makeLink(href, label, currentPathname);
106
108
  }
107
109
 
108
110
  /** Create a link entry. */
@@ -27,17 +27,20 @@ const LocaleSchema = z.object({
27
27
  ),
28
28
  });
29
29
 
30
- const SidebarLinkItemSchema = z.object({
30
+ const SidebarBaseSchema = z.object({
31
31
  /** The visible label for this item in the sidebar. */
32
32
  label: z.string(),
33
+ /** Translations of the `label` for each supported language. */
34
+ translations: z.record(z.string()).default({}),
35
+ });
36
+
37
+ const SidebarLinkItemSchema = SidebarBaseSchema.extend({
33
38
  /** The link to this item’s content. Can be a relative link to local files or the full URL of an external page. */
34
39
  link: z.string(),
35
40
  });
36
41
  export type SidebarLinkItem = z.infer<typeof SidebarLinkItemSchema>;
37
42
 
38
- const AutoSidebarGroupSchema = z.object({
39
- /** The visible label for this item in the sidebar. */
40
- label: z.string(),
43
+ const AutoSidebarGroupSchema = SidebarBaseSchema.extend({
41
44
  /** Enable autogenerating a sidebar category from a specific docs directory. */
42
45
  autogenerate: z.object({
43
46
  /** The directory to generate sidebar items for. */
@@ -49,20 +52,29 @@ const AutoSidebarGroupSchema = z.object({
49
52
  });
50
53
  export type AutoSidebarGroup = z.infer<typeof AutoSidebarGroupSchema>;
51
54
 
52
- type ManualSidebarGroup = {
53
- /** The visible label for this item in the sidebar. */
54
- label: string;
55
+ type ManualSidebarGroupInput = z.input<typeof SidebarBaseSchema> & {
55
56
  /** Array of links and subcategories to display in this category. */
56
57
  items: Array<
57
- | SidebarLinkItem
58
- | z.infer<typeof AutoSidebarGroupSchema>
59
- | ManualSidebarGroup
58
+ | z.input<typeof SidebarLinkItemSchema>
59
+ | z.input<typeof AutoSidebarGroupSchema>
60
+ | ManualSidebarGroupInput
60
61
  >;
61
62
  };
62
63
 
63
- const ManualSidebarGroupSchema: z.ZodType<ManualSidebarGroup> = z.object({
64
- /** The visible label for this item in the sidebar. */
65
- label: z.string(),
64
+ type ManualSidebarGroupOutput = z.output<typeof SidebarBaseSchema> & {
65
+ /** Array of links and subcategories to display in this category. */
66
+ items: Array<
67
+ | z.output<typeof SidebarLinkItemSchema>
68
+ | z.output<typeof AutoSidebarGroupSchema>
69
+ | ManualSidebarGroupOutput
70
+ >;
71
+ };
72
+
73
+ const ManualSidebarGroupSchema: z.ZodType<
74
+ ManualSidebarGroupOutput,
75
+ z.ZodTypeDef,
76
+ ManualSidebarGroupInput
77
+ > = SidebarBaseSchema.extend({
66
78
  /** Array of links and subcategories to display in this category. */
67
79
  items: z.lazy(() =>
68
80
  z
@@ -83,7 +95,11 @@ const SidebarItemSchema = z.union([
83
95
  export type SidebarItem = z.infer<typeof SidebarItemSchema>;
84
96
 
85
97
  const SidebarGroupSchema: z.ZodType<
86
- ManualSidebarGroup | z.infer<typeof AutoSidebarGroupSchema>
98
+ | z.output<typeof ManualSidebarGroupSchema>
99
+ | z.output<typeof AutoSidebarGroupSchema>,
100
+ z.ZodTypeDef,
101
+ | z.input<typeof ManualSidebarGroupSchema>
102
+ | z.input<typeof AutoSidebarGroupSchema>
87
103
  > = z.union([ManualSidebarGroupSchema, AutoSidebarGroupSchema]);
88
104
 
89
105
  const UserConfigSchema = z.object({