Javascript PascalPrecht角度平移使用URLLOADER
我对编程和角度都是新手。我需要将Angle translate与其“useUrlLoader”一起使用,因为我的翻译存储在数据库中<代码>$translateProvider.useUrlLoader('foo/bar.json'); $translateProvider.preferredLanguage('en')代码> 虽然使用StaticFileLoader对我来说似乎足够简单,因为我只需要两个单独的json文件和翻译数据,但我无法得到useUrlLoader所期望的。据我所知,它希望json包含多种语言的翻译(例如英语和德语)。Javascript PascalPrecht角度平移使用URLLOADER,javascript,angularjs,angular-translate,Javascript,Angularjs,Angular Translate,我对编程和角度都是新手。我需要将Angle translate与其“useUrlLoader”一起使用,因为我的翻译存储在数据库中$translateProvider.useUrlLoader('foo/bar.json'); $translateProvider.preferredLanguage('en') 虽然使用StaticFileLoader对我来说似乎足够简单,因为我只需要两个单独的json文件和翻译数据,但我无法得到useUrlLoader所期望的。据我所知,它希望json包含多种
在任何地方都找不到此类文件的示例 StaticFileLoader希望您将不同语言的所有翻译存储在服务器上的不同文件中。它发出如下请求:
/your/server/i18n/locale-en.json
/your/server/i18n/locale-de.json
/your/server/i18n/locale-fr.json
/your/server/i18n/locale-endpoint?lang=en
/your/server/i18n/locale-endpoint?lang=de
/your/server/i18n/locale-endpoint?lang=fr
$translateProvider.useUrlLoader('/path/to/your/endpoint', {
queryParameter : 'localeKey'
});
$translateProvider.useLoader('$translateUrlLoader', {
url : '/path/to/your/endpoint',
queryParameter : 'localeKey'
});
{
"translationId" : "Translation for this ID",
"anotherTranslationId": "Another translation for another id"
}
其中,/your/server/i18n/locale-
和.json
分别是配置过程中传递的前缀和后缀
UrlLoader希望您有一个“聪明”的端点,而不是一堆文件。它发出如下请求:
/your/server/i18n/locale-en.json
/your/server/i18n/locale-de.json
/your/server/i18n/locale-fr.json
/your/server/i18n/locale-endpoint?lang=en
/your/server/i18n/locale-endpoint?lang=de
/your/server/i18n/locale-endpoint?lang=fr
$translateProvider.useUrlLoader('/path/to/your/endpoint', {
queryParameter : 'localeKey'
});
$translateProvider.useLoader('$translateUrlLoader', {
url : '/path/to/your/endpoint',
queryParameter : 'localeKey'
});
{
"translationId" : "Translation for this ID",
"anotherTranslationId": "Another translation for another id"
}
其中,/your/server/i18n/locale endpoint
和lang
分别是您在配置过程中传递的url和queryParameter。url是必需的,但queryParameter可以省略(默认为“lang”)
您可以按如下方式设置UrlLoader:
/your/server/i18n/locale-en.json
/your/server/i18n/locale-de.json
/your/server/i18n/locale-fr.json
/your/server/i18n/locale-endpoint?lang=en
/your/server/i18n/locale-endpoint?lang=de
/your/server/i18n/locale-endpoint?lang=fr
$translateProvider.useUrlLoader('/path/to/your/endpoint', {
queryParameter : 'localeKey'
});
$translateProvider.useLoader('$translateUrlLoader', {
url : '/path/to/your/endpoint',
queryParameter : 'localeKey'
});
{
"translationId" : "Translation for this ID",
"anotherTranslationId": "Another translation for another id"
}
或者像这样:
/your/server/i18n/locale-en.json
/your/server/i18n/locale-de.json
/your/server/i18n/locale-fr.json
/your/server/i18n/locale-endpoint?lang=en
/your/server/i18n/locale-endpoint?lang=de
/your/server/i18n/locale-endpoint?lang=fr
$translateProvider.useUrlLoader('/path/to/your/endpoint', {
queryParameter : 'localeKey'
});
$translateProvider.useLoader('$translateUrlLoader', {
url : '/path/to/your/endpoint',
queryParameter : 'localeKey'
});
{
"translationId" : "Translation for this ID",
"anotherTranslationId": "Another translation for another id"
}
两个加载程序都希望将转换作为JSON对象加载。它可能是这样的:
/your/server/i18n/locale-en.json
/your/server/i18n/locale-de.json
/your/server/i18n/locale-fr.json
/your/server/i18n/locale-endpoint?lang=en
/your/server/i18n/locale-endpoint?lang=de
/your/server/i18n/locale-endpoint?lang=fr
$translateProvider.useUrlLoader('/path/to/your/endpoint', {
queryParameter : 'localeKey'
});
$translateProvider.useLoader('$translateUrlLoader', {
url : '/path/to/your/endpoint',
queryParameter : 'localeKey'
});
{
"translationId" : "Translation for this ID",
"anotherTranslationId": "Another translation for another id"
}
您可以在中找到有关不同装载机的更多信息
UrlLoader的源代码可以在中找到
希望,这会有所帮助。我转到了其他项目,那里不需要多种语言。今天在这里找到这个答案真是太棒了,当我再次需要它们的时候。谢谢!您好@DWand我需要为我的后端传递比url和queryParameter更多的变量,有什么建议吗?您好,@razorblade。在这种情况下,我建议您创建一个自定义加载程序。也许,它可以基于一些内置的加载器。在这种情况下,您将能够以任何方式操纵URL。这将导致
错误:[$injector:unpr]
。有什么建议可以解决这个问题吗?