Welcome Guest, Not a member yet? Register   Sign In
Language problem
#1

Hi everybody;

I create a website using both english, french and spanish languages as a user may choose.
This work perfectly at the homegage since I don't use language helper (I use post and session).

My problem appear later at a form page. I followed the Language Class tutorial (https://www.codeigniter.com/user_guide/l...guage.html) and created dedicated files:
application/language/english/connect_lang.php
application/language/french/connect_lang.php
application/language/spanish/connect_lang.php

all of them filled respectively with things like:
//for french
$lang['pas_de_compte'] => "Je n'ai pas encore de compte";
$lang['a_compte']  => "J'ai déjà un compte";

//for english
$lang['pas_de_compte'] => "I have no account yet";
$lang['a_compte']  => "I already have an account";

// for spanish
$lang['pas_de_compte'] = "Aun no tengo cuenta";
$lang['a_compte'] = "Ya tengo una cuenta";

I catch the language used by the Connect.php controller with:
$langue = $this->session->userdata('langue');
$idiom=['fr'=>'french', 'en'=>'english', 'es' => 'spanish'];

My problem is that when I use :
        $this->lang->load('connect', $idiom[$langue]);
the language file is correctly found but the array of keys/values is printed directly in the HTML as a whole file instead of  filling each label or text field. Things happen the same when I use that in the view instead of the controller.

I don't understand why nor how to fix it.
code result:
Code:
<!DOCTYPE html>
<html>
 <head>
 <link rel="shortcut icon" type="image/x-icon" href="../fond.png" />
   <meta http-equiv="content-type" content="text/html; charset=UTF-8">
   <title>title/title>
   <link rel="stylesheet" href="../style.css">
       </head>
       <body>

$lang['a_compte']  => "J'ai déjà un compte";
$lang['nom']  => "Nom";

// ... all the pairs key/value from accept_lang.php


<table>
<caption>se connecter</caption>
<tr><th colspan="2">
<label for="deja[0]"></label><input type="radio" name="deja" id="deja[0]" value=0 checked onclick="toggle()">
&nbsp;
<input type="radio" name="deja"  id="deja[1]" value=1 onclick="toggle()"><label for="deja[1]"></label></td></tr>

<tr><td><label for="nom" class="obligatoire"></label></td><td>
<input type="input" name="nom" value="" size="50" /></td></tr>
// -- all the fields from the form

</table>
</form>


As you cas see, the lang file is found, its key/value array is printed without being requested, and the labels are printed but empty.
Reply
#2

You are loading the languages wrong.

PHP Code:
$this->lang->load('connect'$idiom[$langue]);

// should be
// you only load the language you need not an array.
$idiom 'english';
$this->lang->load('connect'$idiom); 
What did you Try? What did you Get? What did you Expect?

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

How do you fetch the translations in your view? I suspect you have an error that outputs the $lang array. See this page for the correct syntax for the lang() helper: https://www.codeigniter.com/user_guide/h....html#lang
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#4

(06-27-2019, 03:04 AM)InsiteFX Wrote: You are loading the languages wrong.

PHP Code:
$this->lang->load('connect'$idiom[$langue]);

// should be
// you only load the language you need not an array.
$idiom 'english';
$this->lang->load('connect'$idiom); 
Thank you for your answer but the array is not the problem.
$langue values 'fr' or 'en' or 'es', so $idiom[$langue] values 'french' or 'english' or 'spanish', witch is correct since the language file is correctly found.
Reply
#5

(06-28-2019, 05:37 AM)includebeer Wrote: How do you fetch the translations in your view? I suspect you have an error that outputs the $lang array. See this page for the correct syntax for the lang() helper: https://www.codeigniter.com/user_guide/h....html#lang

Thank you for your answer. I can not verify immediately since I changed my code including the complete array of translation in my view (old fashion way,boring but efficient) to not keep being blocked with that problem. I know this solution is not the elegant one so I will try to go back to the 'state of the art' solution a little bit later, including what you mention.
Reply
#6

What's the purpose of including all key/value pairs in the view?
You didn't enclose it in php tags. Anything in a view without php, html or javascript tags, will be literally visible on the page (as plain text).
Reply




Theme © iAndrew 2016 - Forum software by © MyBB