I regularly develop import/export commands that deal with multiple languages. I always use Extbase repositories and models, so I don't have to connect records manually or do other sketchy things.
I have my workarounds with setRespectSysLanguage to false and add the sysLanguageUid manually to every query, but I must have my customized methods that return the record in the language I need.
Isn't there a way in TYPO3 to set a specific language in a Symfony command, maybe via the language aspect, globally so that I don't have to have specialized methods for everything and I can switch to a different language, other than the default language, and fetch records from the repository in this language?
You could implement the following Method in your repository class:
public function setLanguage($langId)
{
$querySettings = $this->createQuery()->getQuerySettings();
$querySettings->setLanguageUid($langId);
$this->setDefaultQuerySettings($querySettings);
}
And now you can set the language Id to your repository before fetching data. I.e. like this:
// LangId 2 i.e.
$langIdYouWant = 2;
$this->yourRepository->setLanguage($langIdYouWant);
$model = $this->yourRepository->findByAnythingYouWant('WhatYouWant');
Be aware, that the findByUid method will ignore the set languageId.
You can now pass the language Id to your cli command and change the langauage in the repo before doing things.
EDIT after first comment
If you need it a bit more automated you could do the following. Implement an base class for your repositories that will be implemented by all your repositories like that:
class YourRepository extends YourBaseRepository
{
// Do fancy repo stuff here....
}
In the base repository you can implement the initializeObject Method like this:
class YourBaseRepository extends Repository
{
public function initializeObject() {
parent::initializeObject();
if(defined('CLI_IMPORT_LANGUAGE_UID')){
$langId = constant('CLI_IMPORT_LANGUAGE_UID');
$querySettings = $this->createQuery()->getQuerySettings();
$querySettings->setLanguageUid($langId);
$this->setDefaultQuerySettings($querySettings);
}
}
}
So now you can pass a language id to your CommandController when executing it. And set the language id as runtime constant.
define('CLI_IMPORT_LANGUAGE_UID', $langUid);
In that way the constant is only available for the import-routine of your command. That should do the job.