feat: i18n for user profile

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2024-09-13 21:34:24 +08:00
parent f3f3cac2b4
commit d81c2b5791
16 changed files with 141 additions and 39 deletions
+15
View File
@@ -73,6 +73,16 @@
"mark_all_read_button.mark_all_as_read": "Mark all as read",
"mark_all_read_button.mark_as_read": "Mark {{which}} as read",
"mark_all_read_button.undo": "Undo",
"search.empty.no_results": "No results found.",
"search.group.entries": "Entries",
"search.group.feeds": "Feeds",
"search.options.all": "All",
"search.options.entries": "Entries",
"search.options.feeds": "Feeds",
"search.options.search_type": "Search Type",
"search.placeholder": "Search...",
"search.result_count_local_mode": "(Local mode)",
"search.tooltip.local_search": "This search covers locally available data. Try a Refetch to include the latest data.",
"sidebar.category_remove_dialog.cancel": "Cancel",
"sidebar.category_remove_dialog.continue": "Continue",
"sidebar.category_remove_dialog.description": "This operation will delete your category, but the feeds it contains will be retained and grouped by website.",
@@ -101,6 +111,11 @@
"signin.sign_in_to": "Sign in to",
"sync_indicator.offline": "Offline",
"sync_indicator.synced": "Synced with server",
"user_profile.close": "Close",
"user_profile.edit": "Edit",
"user_profile.loading": "Loading",
"user_profile.share": "Share",
"user_profile.toggle_item_style": "Toggle Item Style",
"words.add": "Add",
"words.confirm": "Confirm",
"words.discover": "Discover",
+15
View File
@@ -73,6 +73,16 @@
"mark_all_read_button.mark_all_as_read": "全部标记为已读",
"mark_all_read_button.mark_as_read": "标记{{which}}为已读",
"mark_all_read_button.undo": "撤消",
"search.empty.no_results": "未找到结果。",
"search.group.entries": "条目",
"search.group.feeds": "订阅源",
"search.options.all": "全部",
"search.options.entries": "条目",
"search.options.feeds": "订阅源",
"search.options.search_type": "搜索类型",
"search.placeholder": "搜索...",
"search.result_count_local_mode": "(本地模式)",
"search.tooltip.local_search": "此搜索仅涵盖本地可用数据。尝试重新获取以包含最新数据。",
"sidebar.category_remove_dialog.cancel": "取消",
"sidebar.category_remove_dialog.continue": "继续",
"sidebar.category_remove_dialog.description": "此操作将删除您的类别,但其中包含的 Feed 将保留并按网站分组。",
@@ -101,6 +111,11 @@
"signin.sign_in_to": "登录到",
"sync_indicator.offline": "离线",
"sync_indicator.synced": "已与服务器同步",
"user_profile.close": "关闭",
"user_profile.edit": "编辑",
"user_profile.loading": "加载中",
"user_profile.share": "分享",
"user_profile.toggle_item_style": "切换列表样式",
"words.add": "添加",
"words.confirm": "确认",
"words.discover": "发现",
+8
View File
@@ -10,10 +10,18 @@
"tips.load-lng-error": "Failed to load language pack",
"words.back": "Back",
"words.copy": "Copy",
"words.edit": "Edit",
"words.entry": "Entry",
"words.id": "ID",
"words.items_one": "Item",
"words.items_other": "Items",
"words.local": "local",
"words.record": "record",
"words.record_one": "record",
"words.record_other": "records",
"words.result": "result",
"words.result_one": "result",
"words.result_other": "results",
"words.space": " ",
"words.which.all": "All"
}
+3
View File
@@ -15,6 +15,9 @@
"words.items": "内容",
"words.items_one": "内容",
"words.items_other": "内容",
"words.local": "本地",
"words.record": "记录",
"words.result": "结果",
"words.space": "",
"words.which.all": "全部"
}
+2 -2
View File
@@ -53,8 +53,8 @@
"appearance.guess_code_language.description": "使用模型来推断未标记代码块的主要编程语言",
"appearance.guess_code_language.label": "猜测代码语言",
"appearance.misc": "杂项",
"appearance.modal_overlay.description": "显示模态叠加",
"appearance.modal_overlay.label": "显示模态叠加",
"appearance.modal_overlay.description": "显示模态遮罩,以提高可访问性",
"appearance.modal_overlay.label": "显示模态遮罩",
"appearance.opaque_sidebars.label": "不透明侧边栏",
"appearance.reader_render_inline_style.description": "允许渲染原始 HTML 的内联样式。",
"appearance.reader_render_inline_style.label": "渲染内联样式",
+1 -1
View File
@@ -90,7 +90,7 @@
"electron-updater": "^6.3.4",
"font-list": "1.5.1",
"foxact": "0.2.38",
"framer-motion": "11.0.10",
"framer-motion": "11.5.4",
"franc-min": "6.2.0",
"fuse.js": "7.0.0",
"hast-util-to-jsx-runtime": "2.3.0",
+8 -5
View File
@@ -195,8 +195,8 @@ importers:
specifier: 0.2.38
version: 0.2.38(react@18.3.1)
framer-motion:
specifier: 11.0.10
version: 11.0.10(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
specifier: 11.5.4
version: 11.5.4(@emotion/is-prop-valid@0.8.8)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
franc-min:
specifier: 6.2.0
version: 6.2.0
@@ -4999,12 +4999,15 @@ packages:
react:
optional: true
framer-motion@11.0.10:
resolution: {integrity: sha512-aaImib9FQlfno2yiZAdkvN6zLXLr8a9uGsGFlQO64hSm8NoCeiUng2//5ok7RHCW2ufzT74uuPsmWzUsrHPYDQ==}
framer-motion@11.5.4:
resolution: {integrity: sha512-E+tb3/G6SO69POkdJT+3EpdMuhmtCh9EWuK4I1DnIC23L7tFPrl8vxP+LSovwaw6uUr73rUbpb4FgK011wbRJQ==}
peerDependencies:
'@emotion/is-prop-valid': '*'
react: ^18.0.0
react-dom: ^18.0.0
peerDependenciesMeta:
'@emotion/is-prop-valid':
optional: true
react:
optional: true
react-dom:
@@ -13937,7 +13940,7 @@ snapshots:
optionalDependencies:
react: 18.3.1
framer-motion@11.0.10(react-dom@18.3.1(react@18.3.1))(react@18.3.1):
framer-motion@11.5.4(@emotion/is-prop-valid@0.8.8)(react-dom@18.3.1(react@18.3.1))(react@18.3.1):
dependencies:
tslib: 2.7.0
optionalDependencies:
@@ -113,7 +113,7 @@ export const ModalInternal = memo(
})
const animateController = useAnimationControls()
useEffect(() => {
requestAnimationFrame(() => {
nextFrame(() => {
animateController.start(modalMontionConfig.animate)
})
}, [animateController])
@@ -259,6 +259,7 @@ export const ModalInternal = memo(
)}
onPointerUp={handleDetectSelectEnd}
onClick={handleClickOutsideToDismiss}
onFocus={stopPropagation}
style={zIndexStyle}
>
{DragBar}
@@ -296,6 +297,7 @@ export const ModalInternal = memo(
modalContainerClassName,
!isResizeable && "center",
)}
onFocus={stopPropagation}
onPointerUp={handleDetectSelectEnd}
onClick={handleClickOutsideToDismiss}
>
+1
View File
@@ -1,5 +1,6 @@
export * from "./useBizQuery"
export * from "./useDark"
export * from "./useI18n"
export * from "./useInputComposition"
export * from "./useInterval"
export * from "./useIsOnline"
+23
View File
@@ -0,0 +1,23 @@
import { ns } from "@renderer/@types/constants"
import { useMemo } from "react"
import type { FallbackNs, UseTranslationResponse } from "react-i18next"
import { useTranslation } from "react-i18next"
const allNameSpaces = ns
export function useI18n() {
const { t } = useTranslation()
return useMemo(() => {
const clonedT = t.bind(t)
for (const ns of allNameSpaces) {
clonedT[ns] = (key: any, options: Omit<Parameters<typeof t>[1], "ns"> = {}) => {
return t(key, { ns: ns as any, ...options })
}
}
return clonedT as typeof t & {
[K in (typeof allNameSpaces)[number]]: UseTranslationResponse<FallbackNs<K>, undefined>["t"]
}
}, [t])
}
+3
View File
@@ -145,6 +145,9 @@ export const sleep = (ms: number) => new Promise<void>((resolve) => setTimeout(r
export const capitalizeFirstLetter = (string: string) =>
string.charAt(0).toUpperCase() + string.slice(1)
/**
* @deprecated
*/
export const pluralize = (
noun: string,
count: number,
@@ -5,6 +5,7 @@ import { Popover, PopoverContent, PopoverTrigger } from "@renderer/components/ui
import { RootPortal } from "@renderer/components/ui/portal"
import { HotKeyScopeMap } from "@renderer/constants"
import { shortcuts } from "@renderer/constants/shortcuts"
import { useI18n } from "@renderer/hooks/common"
import { cn } from "@renderer/lib/utils"
import { AnimatePresence, m } from "framer-motion"
import type { FC, ReactNode } from "react"
@@ -235,8 +236,8 @@ export const MarkAllReadPopover = forwardRef<HTMLButtonElement, MarkAllButtonPro
)
export const FlatMarkAllReadButton: FC<MarkAllButtonProps> = (props) => {
const { t } = useTranslation()
const { t: commonT } = useTranslation("common")
const t = useI18n()
const { className, filter, which } = props
const [status, setStatus] = useState<"initial" | "confirm" | "done">("initial")
const handleMarkAll = useMarkAllByRoute(filter)
@@ -278,9 +279,7 @@ export const FlatMarkAllReadButton: FC<MarkAllButtonProps> = (props) => {
<Trans
i18nKey="mark_all_read_button.mark_as_read"
values={{
// @ts-expect-error https://www.i18next.com/overview/typescript#type-error-template-literal
// should be fixed by using `as const` but it's not working
which: commonT(`words.which.${which}`),
which: typeof which === "string" ? t.common(`words.which.${which}` as any) : which,
}}
/>
</span>
+31 -16
View File
@@ -15,8 +15,9 @@ import {
import { Tooltip, TooltipContent, TooltipTrigger } from "@renderer/components/ui/tooltip"
import { ROUTE_ENTRY_PENDING } from "@renderer/constants"
import { useNavigateEntry } from "@renderer/hooks/biz/useNavigateEntry"
import { useI18n } from "@renderer/hooks/common"
import type { FeedViewType } from "@renderer/lib/enum"
import { cn, pluralize } from "@renderer/lib/utils"
import { cn } from "@renderer/lib/utils"
import { getFeedById } from "@renderer/store/feed"
import { searchActions, useSearchStore } from "@renderer/store/search"
import { SearchType } from "@renderer/store/search/constants"
@@ -28,11 +29,13 @@ import { Command } from "cmdk"
import type { FC } from "react"
import * as React from "react"
import { memo, useMemo } from "react"
import { useTranslation } from "react-i18next"
import styles from "./cmdk.module.css"
const SearchCmdKContext = React.createContext<Promise<SearchInstance> | null>(null)
export const SearchCmdK: React.FC = () => {
const { t } = useTranslation()
const open = useAppSearchOpen()
const [searchInstance, setSearchInstance] = React.useState(() =>
@@ -134,7 +137,7 @@ export const SearchCmdK: React.FC = () => {
<Command.Input
className="w-full shrink-0 border-b border-zinc-200 bg-transparent p-4 px-5 text-lg leading-4 dark:border-neutral-700"
ref={inputRef}
placeholder={searchActions.getCurrentKeyword() || "Search..."}
placeholder={searchActions.getCurrentKeyword() || t("search.placeholder")}
onValueChange={handleSearch}
/>
<div className={cn(styles["status-bar"], isPending && styles["loading"])} />
@@ -150,7 +153,12 @@ export const SearchCmdK: React.FC = () => {
{renderedEntries.length > 0 && (
<Command.Group
heading={<SearchGroupHeading icon="i-mgc-paper-cute-fi size-4" title="Entries" />}
heading={
<SearchGroupHeading
icon="i-mgc-paper-cute-fi size-4"
title={t("search.group.entries")}
/>
}
className="flex w-full min-w-0 flex-col py-2"
>
{renderedEntries.map((entry) => {
@@ -173,7 +181,10 @@ export const SearchCmdK: React.FC = () => {
{renderedFeeds.length > 0 && (
<Command.Group
heading={
<SearchGroupHeading icon="i-mgc-rss-cute-fi size-4 text-accent" title="Feeds" />
<SearchGroupHeading
icon="i-mgc-rss-cute-fi size-4 text-accent"
title={t("search.group.feeds")}
/>
}
className="py-2"
>
@@ -264,10 +275,11 @@ const SearchGroupHeading: FC<{ icon: string; title: string }> = ({ icon, title }
const SearchResultCount: FC<{
count?: number
}> = ({ count }) => {
const t = useI18n()
const searchInstance = React.useContext(SearchCmdKContext)
const hasKeyword = useSearchStore((s) => !!s.keyword)
const searchType = useSearchStore((s) => s.searchType)
const recordCountPromise = useMemo(async () => {
let count = 0
const counts = await searchInstance?.then((s) => s.counts)
@@ -290,35 +302,37 @@ const SearchResultCount: FC<{
<small className="center absolute bottom-3 right-3 shrink-0 gap-1 opacity-80">
{hasKeyword ? (
<span>
{count} {pluralize("result", count || 0)}
{count} {t.common("words.result", { count })}
</span>
) : (
<ExPromise promise={recordCountPromise}>
{(count) => (
<>
{count} local {pluralize("record", count)}
{count} {t.common("quantifier.piece")}
{t.common("words.local")}
{t.common("words.space")}
{t.common("words.record", { count })}
</>
)}
</ExPromise>
)}{" "}
(Local mode)
{t("search.result_count_local_mode")}
<i className="i-mingcute-question-line" />
</small>
</TooltipTrigger>
<TooltipContent>
This search covers locally available data. Try a Refetch to include the latest data.
</TooltipContent>
<TooltipContent>{t("search.tooltip.local_search")}</TooltipContent>
</Tooltip>
)
}
const SearchOptions: Component = memo(({ children }) => {
const { t } = useTranslation()
const searchType = useSearchStore((s) => s.searchType)
const searchInstance = React.useContext(SearchCmdKContext)
return (
<div className="absolute bottom-2 left-4 flex items-center gap-2 text-sm text-theme-foreground/80">
<span className="shrink-0">Search Type</span>
<span className="shrink-0">{t("search.options.search_type")}</span>
<Select
onValueChange={async (value) => {
@@ -340,21 +354,21 @@ const SearchOptions: Component = memo(({ children }) => {
value={`${SearchType.All}`}
disabled={searchType === SearchType.All}
>
All
{t("search.options.all")}
</SelectItem>
<SelectItem
className="hover:bg-theme-item-hover"
value={`${SearchType.Entry}`}
disabled={searchType === SearchType.Entry}
>
Entries
{t("search.options.entries")}
</SelectItem>
<SelectItem
className="hover:bg-theme-item-hover"
value={`${SearchType.Feed}`}
disabled={searchType === SearchType.Feed}
>
Feeds
{t("search.options.feeds")}
</SelectItem>
</SelectContent>
</Select>
@@ -365,13 +379,14 @@ const SearchOptions: Component = memo(({ children }) => {
})
const SearchPlaceholder = () => {
const { t } = useTranslation()
const hasKeyword = useSearchStore((s) => !!s.keyword)
return (
<Command.Empty className="center absolute inset-0">
{hasKeyword ? (
<div className="flex flex-col items-center justify-center gap-2 opacity-80">
<EmptyIcon />
No results found.
{t("search.empty.no_results")}
</div>
) : (
<Logo className="size-12 opacity-80 grayscale" />
@@ -7,7 +7,7 @@ import { ActionButton, Button } from "@renderer/components/ui/button"
import { LoadingCircle, LoadingWithIcon } from "@renderer/components/ui/loading"
import { useCurrentModal, useModalStack } from "@renderer/components/ui/modal"
import { ScrollArea } from "@renderer/components/ui/scroll-area"
import { useAuthQuery } from "@renderer/hooks/common"
import { useAuthQuery, useI18n } from "@renderer/hooks/common"
import { apiClient } from "@renderer/lib/api-fetch"
import { defineQuery } from "@renderer/lib/defineQuery"
import { nextFrame, stopPropagation } from "@renderer/lib/dom"
@@ -23,6 +23,7 @@ import { atomWithStorage } from "jotai/utils"
import { throttle } from "lodash-es"
import type { FC } from "react"
import { Fragment, useEffect, useMemo, useRef, useState } from "react"
import { useTranslation } from "react-i18next"
import { FeedForm } from "../discover/feed-form"
@@ -40,6 +41,7 @@ export const UserProfileModalContent: FC<{
variant: "drawer" | "dialog"
}> = ({ userId, variant }) => {
const { t } = useTranslation()
const user = useAuthQuery(
defineQuery(["profiles", userId], async () => {
const res = await apiClient.profiles.$get({
@@ -171,7 +173,6 @@ export const UserProfileModalContent: FC<{
>
<m.div
onPointerDown={stopPropagation}
// onPointerDownCapture={stopPropagation}
tabIndex={-1}
initial="initial"
animate={controller}
@@ -193,7 +194,7 @@ export const UserProfileModalContent: FC<{
>
<div className="absolute right-2 top-2 z-10 flex items-center gap-2 text-[20px] opacity-80">
<ActionButton
tooltip="Toggle Item Style"
tooltip={t("user_profile.toggle_item_style")}
onClick={() => {
const currentVisible = currentVisibleRef.current
const topOfCurrent = currentVisible?.values().next().value
@@ -213,14 +214,14 @@ export const UserProfileModalContent: FC<{
/>
</ActionButton>
<ActionButton
tooltip="Share"
tooltip={t("user_profile.share")}
onClick={() => {
window.open(`${env.VITE_WEB_URL}/profile/${user.data?.id}`)
}}
>
<i className="i-mgc-share-3-cute-re" />
</ActionButton>
<ActionButton tooltip="Close" onClick={modal.dismiss}>
<ActionButton tooltip={t("user_profile.close")} onClick={modal.dismiss}>
<i className="i-mgc-close-cute-re" />
</ActionButton>
</div>
@@ -267,7 +268,7 @@ export const UserProfileModalContent: FC<{
<m.div
className={cn(
"mb-0 text-sm text-zinc-500",
userInfo.handle ? "visible" : "invisible select-none",
userInfo.handle ? "visible" : "hidden select-none",
)}
layout
>
@@ -325,6 +326,7 @@ const SubscriptionItem: FC<{
variant: ItemVariant
}> = ({ subscription, variant }) => {
const t = useI18n()
const isFollowed = !!useSubscriptionStore((state) => state.data[subscription.feedId])
const { present } = useModalStack()
const isLoose = variant === "loose"
@@ -354,10 +356,11 @@ const SubscriptionItem: FC<{
onClick={(e) => {
e.stopPropagation()
e.preventDefault()
const defaultView = subscription.view
present({
title: `${isFollowed ? "Edit " : ""}${APP_NAME} - ${subscription.feeds.title}`,
title: `${isFollowed ? `${t.common("words.edit")} ` : ""}${APP_NAME} - ${subscription.feeds.title}`,
clickOutsideToDismiss: true,
content: ({ dismiss }) => (
<FeedForm
@@ -375,7 +378,7 @@ const SubscriptionItem: FC<{
}}
>
{isFollowed ? (
"Edit"
t("user_profile.edit")
) : (
<>
<FollowIcon className="mr-1 size-3" />
+11 -1
View File
@@ -8,6 +8,7 @@ import { getGeneralSettings } from "@renderer/atoms/settings/general"
import { i18nAtom } from "@renderer/i18n"
import { EventBus } from "@renderer/lib/event-bus"
import { jotaiStore } from "@renderer/lib/jotai"
import { isEmptyObject } from "@renderer/lib/utils"
import dayjs from "dayjs"
import i18next from "i18next"
import { useAtom } from "jotai"
@@ -67,8 +68,17 @@ const langChangedHandler = async (lang: string) => {
}
}
} else {
const res = await eval(`import('/locales/${lang}.js').then((res) => res?.default || res)`)
const res = await eval(
`import('/locales/${lang}.js').then((res) => res?.default || res)`,
).catch(() => {
toast.error(`${t("common:tips.load-lng-error")}: ${lang}`)
loadingLangLock.delete(lang)
return {}
})
if (isEmptyObject(res)) {
return
}
for (const namespace in res) {
i18next.addResourceBundle(lang, namespace, res[namespace], true, true)
}
+2
View File
@@ -17,6 +17,8 @@
"types": ["electron-vite/node"],
"moduleResolution": "Bundler",
"noImplicitReturns": false,
"noUnusedLocals": false,
"noUnusedParameters": false,
"baseUrl": ".",
"paths": {
"@shared/*": ["src/shared/src/*"],