Home  >  Q&A  >  body text

Questions about functionality in Shopware 6

I am currently on a node in the project and wish to use helpers, and have looked at this link:

https://developer.shopware.com/docs/guides/plugins/plugins/administration/using-utils

I would like a helper for translating fragments read from the database and maybe some other functionality to make the project more manageable (maybe there is an easier way, I need to use quite a few functions to translate).

As the article said, I also looked at the Shopware object, but I don't know how to use this object to access a function.

Thanks for the help.

P粉852114752P粉852114752407 days ago538

reply all(1)I'll reply

  • P粉463824410

    P粉4638244102023-09-09 08:07:10

    See the documentation on How to add fragments in the admin interface. You can use the Vue I18n plugin to automatically translate snippets into the currently selected language.

    this.$tc('swag-example.general.myCustomText')
    // 在模板中:{{ $tc('swag-example.general.myCustomText') }}
    

    The functions of this plug-in are globally available in the component without using additional auxiliary functions.

    For snippet entities, you can inject snippetSetService to get the translation by its 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];
            },
        },
    });
    

    reply
    0
  • Cancelreply