1

enter image description here

Is there an easier way to get the CP's currently selected locale? In my plugin I am currently using this method.

public function getCurrentLocale() {
    $id = is_array($this->value) ? $this->value[0] : false;
    $segments = craft()->request->segments;
    $lastSegment = end($segments);
    $locales = craft()->elements->getEnabledLocalesForElement($id);

    if (in_array($lastSegment, $locales)) {
        return $lastSegment;
    }

    return null;
}
3
  • My current method doesn't work if my plugin's field type is being used in globals, because in globals the locale is located in a different segment position.
    – a-am
    Commented May 12, 2016 at 15:38
  • Do you mean the locale that you're currently viewing the control panel in, or if you're viewing an entry, the locale that you're viewing the entry in?
    – Brad Bell
    Commented May 12, 2016 at 16:49
  • The locale I am currently viewing the entry in.
    – a-am
    Commented May 12, 2016 at 16:51

1 Answer 1

1

I assume this is in the context of an event, in which case you've got access to the EntryModel.

If so, $entryModel->locale should be what you're looking for.

Your Answer

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

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