Javascript 使用react-redux-i18n将API中的区域设置数据与存储同步
是否可以根据API的内容动态地将一组翻译与Javascript 使用react-redux-i18n将API中的区域设置数据与存储同步,javascript,reactjs,redux,internationalization,Javascript,Reactjs,Redux,Internationalization,是否可以根据API的内容动态地将一组翻译与react-redux-i18n同步 现在,我正在使用一组静态存储在我的应用程序中的几种语言(fr、en、es、nl)的翻译,这非常适合我的web应用程序的网站端的翻译目的 例如,我有一种语言的对象(在locale data/nl.js下为荷兰语): 通过使用嵌入式方法,我可以在初始化时定义应用程序中的默认语言: // locale-data/index.js import fr from './fr' import en from './en'
react-redux-i18n
同步
现在,我正在使用一组静态存储在我的应用程序中的几种语言(fr、en、es、nl)的翻译,这非常适合我的web应用程序的网站端的翻译目的
例如,我有一种语言的对象(在locale data/nl.js
下为荷兰语):
通过使用嵌入式方法,我可以在初始化时定义应用程序中的默认语言:
// locale-data/index.js
import fr from './fr'
import en from './en'
import nl from './nl'
import es from './es'
// in index.js
...
export default { fr, en, nl, es }
import translations_data from './locale-data'
const translationsObject = {
...translations_data
}
syncTranslationWithStore(store)
store.dispatch(loadTranslations(translationsObject))
store.dispatch(setLocale('en'))
问题是,如果我想为每种语言使用大量的字符串集合,这将大大降低前端应用程序的速度。是否可以在开始时只加载一些语言(例如:英语),并在用户触发redux操作时更新存储,以便从API重新同步所需的翻译?spoiler->but react-i18next提供->通过xhr加载翻译-只需加载所需的语言,只需加载所需的转换文件(您可以将它们拆分为多个文件)。这似乎是我问题的答案:)spoiler->但是react-i18next提供了->通过xhr加载翻译-只需加载所需的语言,只需加载所需的转换文件(您可以将它们拆分为多个文件)。这似乎是我问题的答案:)
export default {
application: {
title: 'Awesome app with i18n!',
hello: 'Hello, %{name}!'
},
date: {
long: 'MMMM Do, YYYY'
},
export: 'Export %{count} items',
export_0: 'Nothing to export',
export_1: 'Export %{count} item',
two_lines: 'Line 1<br />Line 2',
action: {
sign_up: 'Sign Up',
sign_out: 'Sign Out',
log_in: 'Log In',
dashboard: 'Dashboard'
}
}
// locale-data/index.js
import fr from './fr'
import en from './en'
import nl from './nl'
import es from './es'
// in index.js
...
export default { fr, en, nl, es }
import translations_data from './locale-data'
const translationsObject = {
...translations_data
}
syncTranslationWithStore(store)
store.dispatch(loadTranslations(translationsObject))
store.dispatch(setLocale('en'))