-
I am working on language localization. I am using "Shield" as "Third Party". I prepare the global variables in: public function initController(RequestInterface $request, ...)
{
// Do Not Edit This Line
parent::initController($request, $response, $logger);
// Preload any models, libraries, etc, here.
// E.g.: $this->session = \Config\Services::session();
$this->helpers = array_merge($this->helpers, ['setting']);
// localization >>>
$this->viewData['locale'] = [];
$this->viewData['locale']['current_code'] = $request->getLocale();
$this->viewData['locale']['codes'] = $request->config->supportedLocales;
$this->viewData['locale']['lang_switcher'] = array(
'en'=>[ 'html_lang'=>'en-US'
,'html_dir'=>'ltr'
,'codename'=>'English'
]
,'es'=>[ 'html_lang'=>'es-ES'
,'html_dir'=>'ltr'
,'codename'=>'Español'
]
);
foreach($request->config->supportedLocales as $code){
// Other language variable values etc. I discovered following setting the for URL routing: public array $routes = [
'register' => [
[
'get',
'{locale}/register', // <- old value: 'register' In this situation; I have activated the E-Mail Activation. I had to make the following new setting for redirect by page (url) language: // If an action has been defined for register, start it up.
$hasAction = $authenticator->startUpAction('register', $user);
if ($hasAction) {
return redirect()->to($this->request->getLocale().'/auth/a/show');
// return redirect()->to('auth/a/show'); // <- old
} However, I can't get to my protected function view(string $view, array $data = [], array $options = []): string
{
if( isset($this->viewData) ) // [locale]/register ok ; fails on [locale]/auth/a/show
$data = array_merge($this->viewData, $data);
return view($view, $data, $options);
} I need to edit the action address of the form. I need access to current language code (and other variables). <form action="<?= site_url('auth/a/verify') ?>" method="post"> <form action="<?= site_url($locale['current_code'].'/auth/a/verify') ?>" method="post"> How can I access the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
Pass the variable to views. view($view, ['viewData' => $this->viewData], $options); |
Beta Was this translation helpful? Give feedback.
-
I recommend you set the viewData to the View instance with |
Beta Was this translation helpful? Give feedback.
I recommend you set the viewData to the View instance with
setVar()
in your controller.See https://codeigniter4.github.io/CodeIgniter4/outgoing/view_renderer.html