Storyblock 定义了两种语言:俄语、英语。默认语言是俄语。我需要添加浏览器语言定义。如果浏览器语言是俄语,则站点应以俄语打开,如果浏览器语言为其他语言,则站点应以英语打开。该网站还有一个语言切换器(俄语、英语)。使用 Intl.NumberFormat().resolvedOptions().locale 确定浏览器语言并重定向到网站的英文版可以,但随后网站上的语言切换器不起作用。
这是storyblok-service.js
import { DEV_MODE } from "../constants";
const { publicRuntimeConfig } = getConfig();
const { TOKEN } = publicRuntimeConfig;
class StoryblokService {
constructor () {
this.devMode = DEV_MODE;
this.token = TOKEN;
this.client = new StoryblokClient({
accessToken: this.token
});
this.query = {};
}
getCacheVersion () {
return this.client.cacheVersion;
}
get (slug, params) {
params = params || {};
if (this.devMode) {
params.version = "draft";
} else {
params.version = "published";
}
if (typeof window !== "undefined" && typeof window.StoryblokCacheVersion !== "undefined") {
params.cv = window.StoryblokCacheVersion;
}
return this.client.get(slug, params);
}
initEditor (reactComponent) {
if (window.storyblok) {
window.storyblok.init();
window.storyblok.on(["change", "published"], () => location.reload(true));
window.storyblok.on("input", (event) => {
if (event.story.content._uid === reactComponent.state.story.content._uid) {
event.story.content = window.storyblok.addComments(event.story.content, event.story.id);
window.storyblok.resolveRelations(event.story, ["featured-articles.articles"], () => {
reactComponent.setState({
story: event.story
});
});
}
});
}
}
setQuery (query) {
this.query = query;
}
getQuery (param) {
return this.query[param];
}
bridge () {
if (!this.getQuery("_storyblok") && !this.devMode) {
return "";
}
return (<script src={`//app.storyblok.com/f/storyblok-latest.js?t=${this.token}`}></script>);
}
}
const storyblokInstance = new StoryblokService();
export default storyblokInstance;```