Welcome Guest, Not a member yet? Register   Sign In
Load language array
#1

(This post was last modified: 11-16-2020, 06:57 AM by henry_miaomiao.)

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?
Reply
#2

And what version of CodeIgniter are you running?
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

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

Codeigniter 4
Reply
#4

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.
Reply
#5

(This post was last modified: 11-16-2020, 09:13 AM by henry_miaomiao.)

(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
Reply
#6

The language files do return an array see below.

Creating Language Files
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB