Search code examples
vue.jsshopwareshopware6

Problem with Utility Functions in Shopware 6


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


Solution

  • 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];
            },
        },
    });