Search code examples
phpzend-frameworkzend-localepimcore

How to get all available languages from Localized Fields in pimcore?


I'm a little bit stuck with this: I have a controller where I'm collecting all available languages for an object in pimcore.
Right now I simply take a Localized Field from that object, run through it via foreach and fill an array with the Localized Fields's keys. So I get all languages for that object. But this is the most ugly piece I've ever coded :)

Update - here is the code:

$o = Object_Product::getById(SOME_ID);
$availableLanguages = array();
// 'category' is an attribute of my product-object that uses Localized Fields
foreach ($o->getCategory()->getLocalizedfields()->getItems() as $language => $value) {
    $availableLanguages[] = $language;
}

So I get an array that looks like:

$availableLanguages(
    0 => 'en',
    1 => 'de',
    2 => 'it'
    // etc.
);

I'm afraid I've thought too much about it and now I'm missing the forest for the trees - there must be an (more) elegant way for this. Basically Zend_Locale should have this info too, but I don't get it.

Does anyone have a clue for me? Thanks in advance!


Solution

  • Getting all available languages (enabled translations) in Pimcore: Starting from Pimcore 6.9 and in currently available Pimcore 10 we use:

    $languages = \Pimcore\Tool::getValidLanguages();
    

    The documentation is here https://pimcore.com/docs/pimcore/current/Development_Documentation/Objects/Object_Classes/Data_Types/Localized_Fields.html

    As I see in the original question, the author is creating some export for DataObject with all available translations. In such case we need to disable Fallback languages:

    \Pimcore\Model\DataObject\Localizedfield::setGetFallbackValues(false);
    

    This will allow us to get an empty value for fieds without translation instead of default language value.