0

I am working on a custom component. Most input are numbers, therefor not translatable. There are also some strings that can be translated.

Instead of having the user duplicate each item to a new language and make the associations, a custom translations table is used. This way users can add items in multiple languages, without needing to to duplicate and associate.

These items are structured into categories. As each category has a menu item, it's possible to set the association in the menu items.

But the items themselves do not use menu item. Their individual pages are accesable via the categories. Therefor there are no association. :\

Resulting in:... when on an item page, and switching language, the site shows the parent category in the new language. While I want to see the same item as before language switch.

So here is my question: Is it possible to associate an item with itself in multiple languages?

Any tips and insights are most welcome. Thanks! :)

1 Answer 1

2

I'm presuming it's the Joomla Language Switcher module which you're using to switch languages.

In this case, this module has to generate the links which are behind the little flag symbols.

It does this by calling the getAssociations() method of a HelperAssociation class for your component, as described at https://docs.joomla.org/J3.x:Developing_an_MVC_Component/Adding_Associations#Front_End. The Language Switcher module looks to get back an associative array, the array key being the language code, and the array value being the url for the corresponding item in that language. It then outputs a little flag symbol associated with the language code, and puts a link to the associated url.

So inside your component source code directory create a (site) helpers folder (if not already there), and inside a file called association.php put the code for your YourcomponentHelperAssociation class and the getAssociations() method.

I'm not totally sure what way your component data is set up, but I think you're saying that you use the same id for the same item in different languages. So if that's the case, you'll want something like what's below. (Replace "yourcomponent", "yourviewname", "yourviewname" with appropriate strings).

<?php
defined('_JEXEC') or die;

JLoader::register('CategoryHelperAssociation', JPATH_ADMINISTRATOR . '/components/com_categories/helpers/association.php');

abstract class YourcomponentHelperAssociation extends CategoryHelperAssociation
{

    public static function getAssociations($id = 0, $view = null)
    {
        $input = JFactory::getApplication()->input;
        $view = $view === null ? $input->get('view') : $view;
        $id = empty($id) ? $input->getInt('id') : $id;

        if ($view === 'yourviewname')
        {
            if ($id)
            {
                $languages = JLanguageHelper::getContentLanguages();
                $return = array();
                foreach ($languages as $language => $details)
                {
                    $link = 'index.php?option=com_yourcomponent&view=yourviewname&id=' . $id . '&lang=' . $language;
                    $return[$language] = $link;
                }

                return $return;
            }
        }

        if ($view === 'yourcategoryview')
        {
            return self::getCategoryAssociations($id, 'com_yourcomponent');
        }

        return array();
    }
}

Otherwise, follow the example in the Joomla MVC tutorial.

1
  • Thanks @Robbie Jackson. Will try and get back to you on this. :)
    – TVBZ
    Feb 19, 2020 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.