Frontend\Core\Language\Language::setLocale PHP Method

setLocale() public static method

Set locale
public static setLocale ( string $language = null, boolean $force = false )
$language string The language to load, if not provided we will load the language based on the URL.
$force boolean Force the language, so don't check if the language is active.
    public static function setLocale($language = null, $force = false)
    {
        // redefine
        $language = $language !== null ? (string) $language : LANGUAGE;
        // validate language
        if (!$force && !in_array($language, self::getActiveLanguages())) {
            throw new Exception('Invalid language (' . $language . ').');
        }
        // validate file, generate it if needed
        $filesystem = new Filesystem();
        if (!$filesystem->exists(FRONTEND_CACHE_PATH . '/Locale/en.json')) {
            self::buildCache('en', 'Frontend');
        }
        if (!$filesystem->exists(FRONTEND_CACHE_PATH . '/Locale/' . $language . '.json')) {
            self::buildCache($language, 'Frontend');
        }
        // set English translations, they'll be the fallback
        $fallbackTranslations = json_decode(file_get_contents(FRONTEND_CACHE_PATH . '/Locale/en.json'), true);
        self::$fallbackAct = (array) $fallbackTranslations['act'];
        self::$fallbackErr = (array) $fallbackTranslations['err'];
        self::$fallbackLbl = (array) $fallbackTranslations['lbl'];
        self::$fallbackMsg = (array) $fallbackTranslations['msg'];
        // We will overwrite with the requested language's translations upon request
        $translations = json_decode(file_get_contents(FRONTEND_CACHE_PATH . '/Locale/' . $language . '.json'), true);
        self::$act = (array) $translations['act'];
        self::$err = (array) $translations['err'];
        self::$lbl = (array) $translations['lbl'];
        self::$msg = (array) $translations['msg'];
    }

Usage Example

Example #1
0
 /**
  * Validate the form
  */
 private function validateForm()
 {
     if ($this->frm->isSubmitted()) {
         $this->frm->cleanupFields();
         // shorten the fields
         $txtName = $this->frm->getField('name');
         $txtEmail = $this->frm->getField('email');
         $ddmMethod = $this->frm->getField('method');
         $txtSuccessMessage = $this->frm->getField('success_message');
         $txtIdentifier = $this->frm->getField('identifier');
         $emailAddresses = (array) explode(',', $txtEmail->getValue());
         // validate fields
         $txtName->isFilled(BL::getError('NameIsRequired'));
         $txtSuccessMessage->isFilled(BL::getError('SuccessMessageIsRequired'));
         if ($ddmMethod->isFilled(BL::getError('NameIsRequired')) && $ddmMethod->getValue() == 'database_email') {
             $error = false;
             // check the addresses
             foreach ($emailAddresses as $address) {
                 $address = trim($address);
                 if (!\SpoonFilter::isEmail($address)) {
                     $error = true;
                     break;
                 }
             }
             // add error
             if ($error) {
                 $txtEmail->addError(BL::getError('EmailIsInvalid'));
             }
         }
         // identifier
         if ($txtIdentifier->isFilled()) {
             // invalid characters
             if (!\SpoonFilter::isValidAgainstRegexp('/^[a-zA-Z0-9\\.\\_\\-]+$/', $txtIdentifier->getValue())) {
                 $txtIdentifier->setError(BL::getError('InvalidIdentifier'));
             } elseif (BackendFormBuilderModel::existsIdentifier($txtIdentifier->getValue())) {
                 // unique identifier
                 $txtIdentifier->setError(BL::getError('UniqueIdentifier'));
             }
         }
         if ($this->frm->isCorrect()) {
             // build array
             $values['language'] = BL::getWorkingLanguage();
             $values['user_id'] = BackendAuthentication::getUser()->getUserId();
             $values['name'] = $txtName->getValue();
             $values['method'] = $ddmMethod->getValue();
             $values['email'] = $ddmMethod->getValue() == 'database_email' ? serialize($emailAddresses) : null;
             $values['success_message'] = $txtSuccessMessage->getValue(true);
             $values['identifier'] = $txtIdentifier->isFilled() ? $txtIdentifier->getValue() : BackendFormBuilderModel::createIdentifier();
             $values['created_on'] = BackendModel::getUTCDate();
             $values['edited_on'] = BackendModel::getUTCDate();
             // insert the item
             $id = BackendFormBuilderModel::insert($values);
             // trigger event
             BackendModel::triggerEvent($this->getModule(), 'after_add', array('item' => $values));
             // set frontend locale
             FL::setLocale(BL::getWorkingLanguage(), true);
             // create submit button
             $field['form_id'] = $id;
             $field['type'] = 'submit';
             $field['settings'] = serialize(array('values' => \SpoonFilter::ucfirst(FL::getLabel('Send'))));
             BackendFormBuilderModel::insertField($field);
             // everything is saved, so redirect to the editform
             $this->redirect(BackendModel::createURLForAction('Edit') . '&id=' . $id . '&report=added&var=' . rawurlencode($values['name']) . '#tabFields');
         }
     }
 }
All Usage Examples Of Frontend\Core\Language\Language::setLocale