Skip to content

Commit

Permalink
feat: support setLocale api
Browse files Browse the repository at this point in the history
  • Loading branch information
luhc228 committed Mar 29, 2022
1 parent 4993830 commit f53eb78
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
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);
}

0 comments on commit f53eb78

Please sign in to comment.