CodeIgniter Forums
Load language array - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Load language array (/showthread.php?tid=77987)



Load language array - henry_miaomiao - 11-16-2020

I receive the "language segment" from the url (I use CI4)

ex: www.site.com/en/book

I would like to load the relative translations array in the controller for the view... so I tried:

My controller:

$data['tran'] = require_once(APPPATH."/Language/en/file_lang.php");

file_lang.php:

<?php
//english
$lang = array();

$lang['t1'] = "Description";
$lang['t2'] = "list";
$lang['t3'] = "Contact";
$lang['t4'] = "Write";
$lang['t5'] = "Views";
$lang['t6'] = "Cities";

etc.

But does not work... why?


RE: Load language array - InsiteFX - 11-16-2020

And what version of CodeIgniter are you running?


RE: Load language array - henry_miaomiao - 11-16-2020

(11-16-2020, 06:28 AM)InsiteFX Wrote: And what version of CodeIgniter are you running?

Codeigniter 4


RE: Load language array - T.O.M. - 11-16-2020

I also was interested of this question here but did not find the answer.
So I've decided do not load all translations in controller. I just output translations in view:

PHP Code:
<div><?= lang('file_lang.t1'?></div>
<div><?= lang('file_lang.t2'?></div> 

All translations are displayed in the language (locale) specified in the request. So you need to change locale in the request (if you don't do it) with $this->request->setLocale($locale) - in Controller or in Filter.


RE: Load language array - henry_miaomiao - 11-16-2020

(11-16-2020, 07:20 AM)T.O.M. Wrote: I also was interested of this question here but did not find the answer.
So I've decided do not load all translations in controller. I just output translations in view:

PHP Code:
<div><?= lang('file_lang.t1'?></div>
<div><?= lang('file_lang.t2'?></div> 

All translations are displayed in the language (locale) specified in the request. So you need to change locale in the request (if you don't do it) with $this->request->setLocale($locale) - in Controller or in Filter.

There were a very symple function in CI 2...

$data['menu'] = $this->lang->load('menu');

Why there isn't a simil function?

Here the solution:

Add: return $lang; at the end of array in the file_lang.php

Now it's working


RE: Load language array - InsiteFX - 11-16-2020

The language files do return an array see below.

Creating Language Files