1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 
<?php
namespace Omeka\View\Helper;

use Zend\I18n\Translator\TranslatorInterface;
use Zend\View\Helper\AbstractHelper;

/**
 * View helper for getting a BCP 47-compliant value for the lang attribute.
 */
class Lang extends AbstractHelper
{
    /**
     * @var TranslatorInterface $translator
     */
    protected $translator;

    /**
     * Construct the helper.
     *
     * @param TranslatorInterface $translator
     */
    public function __construct(TranslatorInterface $translator)
    {
        $this->translator = $translator;
    }

    /**
     * Get a lang value.
     *
     * Accepts a string or gets the locale from the translator.
     *
     * @param null|string $locale
     * @return string
     */
    public function __invoke($lang = null)
    {
        if (null === $lang) {
            $lang = $this->translator->getLocale();
        }
        // BCP 47 specifies that subtags are separated by hyphens.
        return str_replace('_', '-', $lang);
    }
}