나는 반응 앱을 사용 react-i18next
하고 번역을로드하고 있습니다.i18next-xhr-backend
i18n
.use(Backend)
.use(initReactI18next) // passes i18n down to react-i18next
.init({
lng: "de",
backend: {
loadPath: '/static/locales/{{lng}}/{{ns}}.json'
}
});
로컬에서 실행하면 내 앱이 제공됩니다. http://localhost:3000/
번역 파일도 잘로드됩니다 (src는에 있습니다 public/statuc/locales/
).http://localhost:3000/static/locales/de/translation.json
이제 프로덕션에서 앱이 루트에서 제공되지 않고 빌드 된 파일이 하위 폴더를 통해 제공된다는 문제에 직면 해 있습니다. 그 때문에 나는 나의 것을 바꾸고 packages.json
추가했다homepage
{
"name": "myapp",
"version": "0.1.0",
"homepage": "/static/app/",
...
}
응용 프로그램을 빌드하고 prod에 배포 한 후에도 여전히 올바르게로드되지만 번역 파일을 찾을 수 없습니다.
http : //production.tld/static/app/index.html
반응 앱 파일이 올바르게로드 됨 http : //production.tld/static/app/static/js/main * .js
하지만 번역 파일로 여전히 페치 HTTP : //production.tld/static/locales/de/translation.json 사용할 수 없습니다 더 이상 (대신 HTTP : //production.tld/static/app/static/locales/de/ translation.json 이 정확합니다)
i18n 구성을 변경하여 수정할 수 있습니다.
backend: {
loadPath: '/static/app/static(locales/{{lng}}/{{ns}}.json'
}
그런 다음 프로덕션에서는 작동하지만 더 이상 로컬에서는 작동하지 않습니다.
이 상황을 피하는 방법을 잘 모르겠습니까?
당신은 전달할 수 loadPath
로 기능 .
backend: {
loadPath: () => {
// check the domain
const host = window.location.host;
return (host === 'production.ltd' ? '/static/app':'') + '/static/app/static(locales/{{lng}}/{{ns}}.json';
},
},
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다