cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[BUG]: Phalcon\Translate\Adapter\Gettext::setLocale()

Open Alistar84 opened this issue 4 years ago • 2 comments

Describe the bug I have a problem with Gettext adapter in Phalcon v5.

Fatal error: Uncaught TypeError: Phalcon\Translate\Adapter\Gettext::setLocale(): Argument #2 ($localeArray) must be of type array, string given

I noticed that the type of the second parameter in the "setLocale" method was changed, but the example remained the same.

https://docs.phalcon.io/5.0/it-it/api/phalcon_translate#translate-adapter-gettext public function setLocale( int $category, array $localeArray = [] ): string | bool;

https://docs.phalcon.io/4.0/it-it/api/phalcon_translate#translate-adapter-gettext public function setLocale( int $category, string $locale ): string | bool;

How can I solve?

This is my TranslationProvider class:

<?php

namespace App\providers;

use Phalcon\Di\DiInterface;
use Phalcon\Di\ServiceProviderInterface;
use Phalcon\Translate\Adapter\Gettext;
use Phalcon\Translate\InterpolatorFactory;

class TranslationProvider implements ServiceProviderInterface
{
    /**
     * @var string
     */
    protected $providerName = 'translation';

    /**
     * @param DiInterface $di
     *
     * @return void
     */
    public function register(DiInterface $di): void
    {
        /** @var \Phalcon\Config $config */
        $config = $di->getShared('config');

        /** @var Router $router */
        $router = $di->getShared('router');

        /** @var Session $session */
        $session = $di->getShared('session');
        
        $di->setShared($this->providerName, function () use ($config, $router, $session) {

                $url_language = ($router->getMatchedRoute() != null && $router->getMatchedRoute()->getName() != null) ? substr($router->getMatchedRoute()->getName(),0,2) : null;
    		$browser_language = $this->getRequest()->getBestLanguage();
    		$session_language = ($session->has('language') && $session->get('language') !== null) ? $session->get('language') : '';
    		
    		if(in_array($url_language, $config->availableLanguage->toArray())){
    		    $language = $url_language;
    		}else if(in_array($browser_language, $config->availableLanguage->toArray())){
    	            $language = $browser_language;
    		}else if(in_array($session_language, $config->availableLanguage->toArray())){
    		    $language = $session_language;
    		}else{
    		    $language = $config->defaultLanguage;
    		}
    		
    		$session->set('language',$language);
    		
    		if(!defined('LC_MESSAGES')) define('LC_MESSAGES', 5);
    		$locale = is_dir(APP_PATH.'/modules/frontend/languages/'.$language.'_'.($language == 'en' ? 'US' : strtoupper($language)).'.utf8') ? $language.'_'.($language == 'en' ? 'US' : strtoupper($language)).'.utf8' : 'it_IT.utf8';
    		
    		$interpolator = new InterpolatorFactory();
    	
    		 $translate = new Gettext($interpolator, array(
    		        'locale' => $locale,
    		        'file' => 'messages',
    		        'defaultDomain' => 'messages',
    		        'directory' =>  APP_PATH.'/modules/frontend/languages/',
    		        'category' => LC_MESSAGES
    		    ));
    		}
    		
    		return $translate;
        });
    }
}

Details

  • Phalcon version: 5.0.3
  • PHP Version: 8.1.2
  • Operating System: Ubuntu 22.04
  • Server: Apache

Alistar84 avatar Oct 17 '22 10:10 Alistar84

You will need to change your locale element (from the options array) to an array if it is not already an array.

$locale = is_dir(APP_PATH.'/modules/frontend/languages/'.$language.'_'.($language == 'en' ? 'US' : strtoupper($language)).'.utf8') ? $language.'_'.($language == 'en' ? 'US' : strtoupper($language)).'.utf8' : 'it_IT.utf8';

$locale = is_array($locale) ? $locale : [$locale];

That should solve the issue you are looking at.

niden avatar Oct 17 '22 13:10 niden

Thanks @niden !

shovv avatar Sep 09 '23 14:09 shovv