Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: locale cannot apply for independent demo routes #1891

Merged
merged 4 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/features/locales.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SP_ROUTE_PREFIX } from '@/constants';
import type { IApi } from '@/types';
import path from 'path';
import { winPath } from 'umi/plugin-utils';
Expand Down Expand Up @@ -112,7 +113,15 @@ const LocalesContainer: FC<{ children: ReactNode }> = (props) => {
: history.location.pathname.replace(/([^/])\\/$/, '$1')
.startsWith("${api.config.base!.replace(/\/$/, '')}" + locale.base)
));
const locale = matched ? matched.id : locales[0].id;
let locale = matched ? matched.id : locales[0].id;
// using query on demos
if(history.location.pathname.startsWith('/${SP_ROUTE_PREFIX}demos')){
PeachScript marked this conversation as resolved.
Show resolved Hide resolved
const params = new URLSearchParams(history.location.search);
// match the locale of the query
if (params.get('locale')){
locale = params.get('locale');
}
}
const localeMessages = messages[locale] || {};

// append internal message, for use intl as string template util
Expand Down
4 changes: 3 additions & 1 deletion suites/theme-mobile/src/builtins/Previewer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IPreviewerProps, useRouteMeta, useSiteData } from 'dumi';
import { IPreviewerProps, useLocale, useRouteMeta, useSiteData } from 'dumi';
import Previewer from 'dumi/theme-default/builtins/Previewer';
import Device from 'dumi/theme/slots/Device';
import React, { useCallback, useEffect, useState, type FC } from 'react';
Expand All @@ -9,12 +9,14 @@ const MobilePreviewer: FC<IPreviewerProps> = (props) => {
frontmatter: { mobile = true },
} = useRouteMeta();
const { themeConfig } = useSiteData();
const locale = useLocale();
const generateUrl = useCallback((p: typeof props) => {
const [pathname, search] = p.demoUrl.split('?');
const params = new URLSearchParams(search);

if (p.compact) params.set('compact', '');
if (p.background) params.set('background', p.background);
if (locale.id) params.set('locale', locale.id);

return `${pathname}?${params.toString()}`.replace(/\?$/, '');
}, []);
Expand Down