英文:
Problem with Utility Functions in Shopware 6
问题
我现在在我的项目中的一个阶段,想要使用辅助工具,并查看了以下链接:
https://developer.shopware.com/docs/guides/plugins/plugins/administration/using-utils
我想要一个辅助工具,用于翻译从数据库中读取的片段,也许还有一个视图,以使项目更加可管理(也许有更简单的方法,我需要使用不少函数来进行翻译)。
正如文章中所述,我也查看了Shopware对象,但我不知道如何使用这个对象访问函数。
谢谢帮助。
英文:
I am now at a point in my project where I would like to use helpers and have looked at this point:
https://developer.shopware.com/docs/guides/plugins/plugins/administration/using-utils
I would like to have a helper that translates my snippets read from the database, and maybe a view more to make the project more overseeable (maybe there is an easier way, I needed quite a few functions to get the translation).
As the article says, I also looked at the Shopware object, but I don't know how I could access a function using this object.
Thanks for helping
答案1
得分: 1
请查看如何在管理界面中添加代码片段的文档。您可以使用Vue I18n插件自动将代码片段翻译为当前选择的语言。
this.$tc('swag-example.general.myCustomText')
// 在模板中:{{ $tc('swag-example.general.myCustomText') }}
插件的功能在组件中是全局可用的,无需使用额外的辅助工具。
对于snippet
实体,您可以注入snippetSetService
以按其键获取翻译。
Component.register('my-component', {
template,
inject: [
'snippetSetService',
],
methods: {
async getSnippetTranslations(translationKey) {
this.isLoading = true;
const translations = await this.snippetSetService.getCustomList(1, 25, { translationKey });
if (translations.total < 1) {
return [];
}
return translations.data[translationKey];
},
},
});
英文:
Please see the documentation on how to add snippets in the administration. You can use the Vue I18n plugin to translate snippets to the currently selected language automatically.
this.$tc('swag-example.general.myCustomText')
// in the template: {{ $tc('swag-example.general.myCustomText') }}
The functions of the plugin are globally available in components. No need to use additional helpers.
For snippet
entities you could inject the snippetSetService
to fetch the translations by their key.
Component.register('my-component', {
template,
inject: [
'snippetSetService',
],
methods: {
async getSnippetTranslations(translationKey) {
this.isLoading = true;
const translations = await this.snippetSetService.getCustomList(1, 25, { translationKey });
if (translations.total < 1) {
return [];
}
return translations.data[translationKey];
},
},
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论