O4DOIXmlFilter::getPrimaryTranslation PHP Method

getPrimaryTranslation() public method

Identify the primary translation from an array of localized data.
public getPrimaryTranslation ( $localizedData, $localePrecedence ) : mixed | null
$localizedData array An array of localized data (key: locale, value: localized data).
$localePrecedence array An array of locales by descending priority.
return mixed | null The value of the primary locale or null if no primary translation could be found.
    function getPrimaryTranslation($localizedData, $localePrecedence)
    {
        // Check whether we have localized data at all.
        if (!is_array($localizedData) || empty($localizedData)) {
            return null;
        }
        // Try all locales from the precedence list first.
        foreach ($localePrecedence as $locale) {
            if (isset($localizedData[$locale]) && !empty($localizedData[$locale])) {
                return $localizedData[$locale];
            }
        }
        // As a fallback: use any translation by alphabetical
        // order of locales.
        ksort($localizedData);
        foreach ($localizedData as $locale => $value) {
            if (!empty($value)) {
                return $value;
            }
        }
        // If we found nothing (how that?) return null.
        return null;
    }