mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-04 05:10:19 +08:00
Improve language code.
This commit is contained in:
@ -72,7 +72,6 @@ class App extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
UNSAFE_componentWillMount() {
|
UNSAFE_componentWillMount() {
|
||||||
Setting.setLanguage();
|
|
||||||
this.updateMenuKey();
|
this.updateMenuKey();
|
||||||
this.getAccount();
|
this.getAccount();
|
||||||
}
|
}
|
||||||
@ -139,6 +138,13 @@ class App extends Component {
|
|||||||
return location.toString().replace(location.search, "");
|
return location.toString().replace(location.search, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setLanguage(account) {
|
||||||
|
let language = account?.language;
|
||||||
|
if (language !== "" && language !== i18next.language) {
|
||||||
|
Setting.setLanguage(language);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
getAccount() {
|
getAccount() {
|
||||||
let query = this.getAccessTokenParam();
|
let query = this.getAccessTokenParam();
|
||||||
if (query === "") {
|
if (query === "") {
|
||||||
@ -153,6 +159,8 @@ class App extends Component {
|
|||||||
if (res.status === "ok") {
|
if (res.status === "ok") {
|
||||||
account = res.data;
|
account = res.data;
|
||||||
account.organization = res.data2;
|
account.organization = res.data2;
|
||||||
|
|
||||||
|
this.setLanguage(account);
|
||||||
} else {
|
} else {
|
||||||
if (res.msg !== "Please sign in first") {
|
if (res.msg !== "Please sign in first") {
|
||||||
Setting.showMessage("error", `Failed to sign in: ${res.msg}`);
|
Setting.showMessage("error", `Failed to sign in: ${res.msg}`);
|
||||||
@ -196,7 +204,7 @@ class App extends Component {
|
|||||||
handleRightDropdownClick(e) {
|
handleRightDropdownClick(e) {
|
||||||
if (e.key === '/account') {
|
if (e.key === '/account') {
|
||||||
this.props.history.push(`/account`);
|
this.props.history.push(`/account`);
|
||||||
} else if (e.key === 'logout') {
|
} else if (e.key === '/logout') {
|
||||||
this.logout();
|
this.logout();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,8 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
export const ShowGithubCorner = false
|
export const ShowGithubCorner = false;
|
||||||
|
export const GithubRepo = "https://github.com/casbin/casdoor";
|
||||||
|
|
||||||
export const GithubRepo = "https://github.com/casbin/casdoor"
|
export const ForceLanguage = "en";
|
||||||
|
export const DefaultLanguage = "en";
|
@ -13,7 +13,7 @@
|
|||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import * as Conf from "./common/Conf"
|
import * as Conf from "./Conf";
|
||||||
import GithubCorner from "react-github-corner";
|
import GithubCorner from "react-github-corner";
|
||||||
|
|
||||||
class CustomGithubCorner extends React.Component {
|
class CustomGithubCorner extends React.Component {
|
||||||
|
@ -30,13 +30,11 @@ class SelectLanguageBox extends React.Component {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
onClick(e) {
|
|
||||||
Setting.changeLanguage(e.key);
|
|
||||||
};
|
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const menu = (
|
const menu = (
|
||||||
<Menu onClick={this.onClick.bind(this)}>
|
<Menu onClick={(e) => {
|
||||||
|
Setting.changeLanguage(e.key);
|
||||||
|
}}>
|
||||||
<Menu.Item key="en" icon={<IconFont type="icon-en" />}>English</Menu.Item>
|
<Menu.Item key="en" icon={<IconFont type="icon-en" />}>English</Menu.Item>
|
||||||
<Menu.Item key="zh" icon={<IconFont type="icon-zh" />}>简体中文</Menu.Item>
|
<Menu.Item key="zh" icon={<IconFont type="icon-zh" />}>简体中文</Menu.Item>
|
||||||
<Menu.Item key="fr" icon={<IconFont type="icon-fr" />}>Français</Menu.Item>
|
<Menu.Item key="fr" icon={<IconFont type="icon-fr" />}>Français</Menu.Item>
|
||||||
|
@ -21,6 +21,7 @@ import i18next from "i18next";
|
|||||||
import copy from "copy-to-clipboard";
|
import copy from "copy-to-clipboard";
|
||||||
import {authConfig} from "./auth/Auth";
|
import {authConfig} from "./auth/Auth";
|
||||||
import {Helmet} from "react-helmet";
|
import {Helmet} from "react-helmet";
|
||||||
|
import moment from "moment";
|
||||||
|
|
||||||
export let ServerUrl = "";
|
export let ServerUrl = "";
|
||||||
|
|
||||||
@ -308,20 +309,43 @@ export function getAvatarColor(s) {
|
|||||||
return colorList[random % 4];
|
return colorList[random % 4];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setLanguage() {
|
export function setLanguage(language) {
|
||||||
let language = localStorage.getItem('language');
|
localStorage.setItem("language", language);
|
||||||
if (language === undefined) {
|
changeMomentLanguage(language);
|
||||||
language = "en"
|
i18next.changeLanguage(language);
|
||||||
}
|
|
||||||
i18next.changeLanguage(language)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function changeLanguage(language) {
|
export function changeLanguage(language) {
|
||||||
localStorage.setItem("language", language)
|
localStorage.setItem("language", language);
|
||||||
i18next.changeLanguage(language)
|
changeMomentLanguage(language);
|
||||||
|
i18next.changeLanguage(language);
|
||||||
window.location.reload(true);
|
window.location.reload(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function changeMomentLanguage(language) {
|
||||||
|
return;
|
||||||
|
if (language === "zh") {
|
||||||
|
moment.locale("zh", {
|
||||||
|
relativeTime: {
|
||||||
|
future: "%s内",
|
||||||
|
past: "%s前",
|
||||||
|
s: "几秒",
|
||||||
|
ss: "%d秒",
|
||||||
|
m: "1分钟",
|
||||||
|
mm: "%d分钟",
|
||||||
|
h: "1小时",
|
||||||
|
hh: "%d小时",
|
||||||
|
d: "1天",
|
||||||
|
dd: "%d天",
|
||||||
|
M: "1个月",
|
||||||
|
MM: "%d个月",
|
||||||
|
y: "1年",
|
||||||
|
yy: "%d年",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function getClickable(text) {
|
export function getClickable(text) {
|
||||||
return (
|
return (
|
||||||
// eslint-disable-next-line jsx-a11y/anchor-is-valid
|
// eslint-disable-next-line jsx-a11y/anchor-is-valid
|
||||||
|
@ -12,38 +12,85 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
import i18n from 'i18next'
|
import i18n from "i18next";
|
||||||
import zh from './locales/zh/data.json'
|
import zh from "./locales/zh/data.json";
|
||||||
import en from './locales/en/data.json'
|
import en from "./locales/en/data.json";
|
||||||
import fr from './locales/fr/data.json'
|
import fr from "./locales/fr/data.json";
|
||||||
import de from './locales/de/data.json'
|
import de from "./locales/de/data.json";
|
||||||
import ko from './locales/ko/data.json'
|
import ko from "./locales/ko/data.json";
|
||||||
import ru from './locales/ru/data.json'
|
import ru from "./locales/ru/data.json";
|
||||||
import ja from './locales/ja/data.json'
|
import ja from "./locales/ja/data.json";
|
||||||
|
import * as Conf from "./Conf";
|
||||||
|
import * as Setting from "./Setting";
|
||||||
|
|
||||||
const resources = {
|
const resources = {
|
||||||
en: en,
|
en: en,
|
||||||
zh: zh,
|
zh: zh,
|
||||||
fr: fr,
|
fr: fr,
|
||||||
ja: ja,
|
|
||||||
de: de,
|
de: de,
|
||||||
ko: ko,
|
ko: ko,
|
||||||
ru: ru,
|
ru: ru,
|
||||||
|
ja: ja,
|
||||||
};
|
};
|
||||||
|
|
||||||
i18n
|
function initLanguage() {
|
||||||
.init({
|
let language = localStorage.getItem("language");
|
||||||
lng: "en",
|
if (language === undefined || language == null) {
|
||||||
|
if (Conf.ForceLanguage !== "") {
|
||||||
|
language = Conf.ForceLanguage;
|
||||||
|
} else {
|
||||||
|
let userLanguage;
|
||||||
|
userLanguage = navigator.language;
|
||||||
|
switch (userLanguage) {
|
||||||
|
case "zh-CN":
|
||||||
|
language = "zh";
|
||||||
|
break;
|
||||||
|
case "zh":
|
||||||
|
language = "zh";
|
||||||
|
break;
|
||||||
|
case "en":
|
||||||
|
language = "en";
|
||||||
|
break;
|
||||||
|
case "en-US":
|
||||||
|
language = "en";
|
||||||
|
break;
|
||||||
|
case "fr":
|
||||||
|
language = "fr";
|
||||||
|
break;
|
||||||
|
case "de":
|
||||||
|
language = "de";
|
||||||
|
break;
|
||||||
|
case "ko":
|
||||||
|
language = "ko";
|
||||||
|
break;
|
||||||
|
case "ru":
|
||||||
|
language = "ru";
|
||||||
|
break;
|
||||||
|
case "ja":
|
||||||
|
language = "ja";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
language = Conf.DefaultLanguage;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Setting.changeMomentLanguage(language);
|
||||||
|
|
||||||
resources: resources,
|
return language;
|
||||||
|
}
|
||||||
|
|
||||||
keySeparator: false,
|
i18n.init({
|
||||||
|
lng: initLanguage(),
|
||||||
|
|
||||||
interpolation: {
|
resources: resources,
|
||||||
escapeValue: false
|
|
||||||
},
|
|
||||||
|
|
||||||
saveMissing: true,
|
|
||||||
})
|
|
||||||
|
|
||||||
export default i18n;
|
keySeparator: false,
|
||||||
|
|
||||||
|
interpolation: {
|
||||||
|
escapeValue: false,
|
||||||
|
},
|
||||||
|
//debug: true,
|
||||||
|
saveMissing: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
export default i18n;
|
||||||
|
Reference in New Issue
Block a user