Skip to content

Commit

Permalink
chore: playground
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Dec 23, 2024
1 parent a4d2f66 commit 9f159a1
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 49 deletions.
9 changes: 7 additions & 2 deletions playground/remix-ssr/app/entry.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ import { legacyLogicalPropertiesTransformer, StyleProvider } from '@ant-design/c
import { RemixBrowser } from '@remix-run/react'
import i18next from 'i18next'
import { hydrateRoot } from 'react-dom/client'
import { getInitialNamespaces } from 'remix-i18next/client'
import { i18nAlly } from 'vite-plugin-i18n-ally/client'
import { i18nOptions } from '@/i18n/i18n'
import { resolveNamespace } from './i18n/namespace.client'

const i18nChangeLanguage = i18next.changeLanguage

const initialNamespaces = () => {
return getInitialNamespaces().concat(i18nOptions.defaultNS)
}

async function hydrate() {
const { asyncLoadResource } = i18nAlly({
namespaces: [...(await resolveNamespace())],
namespaces: initialNamespaces(),
fallbackLng: i18nOptions.fallbackLng,
async onInit({ language }) {
await i18next.use(initReactI18next).init({
Expand All @@ -21,7 +26,7 @@ async function hydrate() {
fallbackLng: i18nOptions.fallbackLng,
keySeparator: i18nOptions.keySeparator,
nsSeparator: i18nOptions.nsSeparator,
ns: [...(await resolveNamespace())] as string[],
ns: initialNamespaces(),
debug: import.meta.env.DEV,
})
},
Expand Down
37 changes: 22 additions & 15 deletions playground/remix-ssr/app/i18n/namespace.client.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import { type AgnosticRouteObject, matchRoutes } from '@remix-run/router'
import { routes } from 'virtual:remix-flat-routes'
import { matchRoutes } from '@remix-run/router'
import { i18nOptions } from './i18n'

export async function resolveNamespace(pathname = window.location.pathname) {
const res = await Promise.all(
matchRoutes(routes as AgnosticRouteObject[], pathname)?.map(async (route) => {
const { handle } = route.route
if (typeof handle === 'function') {
return await handle()
}
return handle
export async function resolveNamespace(pathname: string = window.location.pathname) {
const res = await Promise.all<string[]>(
matchRoutes(window.__remixRouter.routes, pathname)?.map(async (_route) => {
const { route } = _route
await route.lazy?.()
return new Promise<string[]>((resolve) => {
function r(): string[] {
if (typeof route?.handle !== 'object') return []
if (!route.handle) return []
if (!('i18n' in route.handle)) return []
if (typeof route.handle.i18n === 'string') return [route.handle.i18n]
if (Array.isArray(route.handle.i18n) && route.handle.i18n.every((value) => typeof value === 'string')) {
return route.handle.i18n as string[]
}
return []
}
setTimeout(() => {
resolve(r())
})
})
}) || [],
)

return res
.filter((t) => t?.i18n)
.map((t) => t.i18n)
.flat()
.concat(i18nOptions.defaultNS)
return res.flatMap((t) => t).concat(i18nOptions.defaultNS)
}
3 changes: 1 addition & 2 deletions playground/remix-ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@
"vite": "^5.4.10",
"vite-plugin-i18n-ally": "workspace:*",
"vite-plugin-istanbul-widget": "^1.9.1",
"vite-plugin-public-typescript": "^4.1.2",
"vite-plugin-remix-flat-routes": "^4.1.2"
"vite-plugin-public-typescript": "^4.1.2"
},
"simple-git-hooks": {
"commit-msg": "npm run commitlint",
Expand Down
3 changes: 1 addition & 2 deletions playground/remix-ssr/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
"@remix-run/node",
"vite/client",
"vite-plugin-i18n-ally/virtual",
"vite-plugin-public-typescript/manifest",
"vite-plugin-remix-flat-routes/virtual"
"vite-plugin-public-typescript/manifest"
]
}
}
7 changes: 0 additions & 7 deletions playground/remix-ssr/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { preset } from 'vite-config-preset'
import { envOnlyMacros } from 'vite-env-only'
import { i18nAlly } from 'vite-plugin-i18n-ally'
import { publicTypescript } from 'vite-plugin-public-typescript'
import { remixFlatRoutes } from 'vite-plugin-remix-flat-routes'

installGlobals()

Expand All @@ -32,12 +31,6 @@ export default defineConfig((env) => {
})
},
}),
remixFlatRoutes({
flatRoutesOptions: {
ignoredRouteFiles,
},
handleAsync: true,
}),
i18nAlly({
localesPaths: ['./app/i18n/locales'],
namespace: true,
Expand Down
21 changes: 0 additions & 21 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9f159a1

Please sign in to comment.