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

feat: support setLocale api #5253

Merged
merged 2 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion examples/basic-i18n/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { runApp, IAppConfig, useLocale, getDefaultLocale } from 'ice';
import { runApp, IAppConfig, useLocale, getDefaultLocale, setLocale } from 'ice';
import { IntlProvider as ReactIntlProvider } from 'react-intl';
import { messages } from './locales';

Expand Down Expand Up @@ -27,6 +27,9 @@ const appConfig: IAppConfig = {
return (
<IntlProvider>{children}</IntlProvider>
);
},
async getInitialData() {
setLocale('zh-CN');
}
}
};
Expand Down
4 changes: 4 additions & 0 deletions packages/plugin-i18n/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# build-plugin-ice-i18n

## 0.2.0

- feat: provide `setLocale` API which can update locale value to cookie

## 0.1.1

- fix: fail to redirect to another route in CSR
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-i18n/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "build-plugin-ice-i18n",
"version": "0.1.1",
"version": "0.2.0",
"description": "ICE i18n build-scripts plugin",
"author": "ice-admin@alibaba-inc.com",
"homepage": "https://github.com/alibaba/ice#readme",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-i18n/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default async function (
{
source: './plugins/i18n',
importSource: '$$ice/plugins/i18n',
exportMembers: ['useLocale', 'getAllLocales', 'getDefaultLocale', 'getLocale']
exportMembers: ['useLocale', 'getAllLocales', 'getDefaultLocale', 'getLocale', 'setLocale']
});
}

Expand Down
14 changes: 11 additions & 3 deletions packages/plugin-i18n/src/templates/index.tsx.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import getDetectedLocaleFromPathname from './utils/getDetectedLocaleFromPathname
import { getAppConfig } from '../../core/appConfig';
<% } %>

const cookies = new Cookies();

const LOCALE_COOKIE_KEY = '<%= LOCALE_COOKIE_KEY %>';
const i18nConfig = <%- JSON.stringify(i18nConfig) %>;

Expand Down Expand Up @@ -75,16 +77,22 @@ export function getLocale(): string {
<% if (i18nRouting !== false) {%>
getDetectedLocaleFromPath(locales, defaultLocale) ||
<% } %>
new Cookies().get(LOCALE_COOKIE_KEY) ||
cookies.get(LOCALE_COOKIE_KEY) ||
defaultLocale
)
}
/**
* 设置当前国际化语言,修改 cookie 的 <%= LOCALE_COOKIE_KEY %> 值
*/
export function setLocale(locale: string) {
setLocaleToCookies(locale);
}
function setLocaleToCookies(locale: string) {
const cookies = new Cookies();
cookies.set(LOCALE_COOKIE_KEY, locale, { path: '/' });
}
export function getLocaleFromCookies() {
return new Cookies().get(LOCALE_COOKIE_KEY);
return cookies.get(LOCALE_COOKIE_KEY);
}