The following warnings occurred:
Warning [2] Undefined variable $unreadreports - Line: 26 - File: global.php(961) : eval()'d code PHP 8.1.2-1ubuntu2.14 (Linux)
File Line Function
/global.php(961) : eval()'d code 26 errorHandler->error
/global.php 961 eval
/showthread.php 28 require_once





× This forum is read only. As of July 23, 2019, the UserSpice forums have been closed. To receive support, please join our Discord by clicking here. Thank you!

  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Make additional $user variables?
#1
Hi all,

The script I'm working on, uses an Array of strings to be both in Spanish and English. (or at least the original script I'm converting to US does).

A couple of questions, I saw that US has its own multi-language section, can this be added on to?
If not, is it possible for me to turn an array such as $lang so that I could refer to element 15 and echo just $lang->15 without having to load the array in every file?

Hope these questions make sense,
Chris
  Reply
#2
Can someone explain how this code works?

<?=lang("SIGNUP_TEXT","");?> not sure who this echos the value of SIGNUP_TEXT, and for the "" what would go there if you supported multiple languages?

And how would your array need to be built to handle this?

Thanks,

Chris
  Reply
#3
You can certainly do this, and fairly easily. Once it's set up, all you need to do is add your per-page translations (see below) for any pages you want both languages on.

The language file is located at
Code:
/users/helpers/language.php
. This file loads all the English translations into the array $lang. If you make a copy of this
Code:
file and name it language_es.php
(for example), you can change all the translations into Spanish. Also, change all instances of $lang to $lang_es, and you can use the Spanish language array the same way as the English:
Code:
<?= lang_es("SIGNUP_TEXT", ""); ?>

Be sure to copy and modify the lang() function in
Code:
us_helpers.php
to create your new lang_es() function:
Code:
function lang_es($key,$markers = NULL){
Code:
global $lang_es;
Code:
if($markers == NULL){
Code:
$str = $lang_es[$key];
Code:
}else{
Code:
//Replace any dyamic markers
Code:
$str = $lang_es[$key];
Code:
$iteration = 1;
Code:
foreach($markers as $marker){
Code:
$str = str_replace("{3bc1fe685386cc4c3ab89a3f76566d8931e181ad17f08aed9ad73b30bf28114d}m".$iteration."{3bc1fe685386cc4c3ab89a3f76566d8931e181ad17f08aed9ad73b30bf28114d}",$marker,$str);
Code:
$iteration++;
Code:
}
Code:
}
Code:
//Ensure we have something to return
Code:
if($str == ""){
Code:
return ("No se encontró ninguna clave de idioma");
Code:
}else{
Code:
return $str;
Code:
}
Code:
}

To add on to the array, you can include it in
Code:
language.php
or simply on the page itself where needed:
Code:
$lang = array_merge($lang, array(
Code:
"TRACKING_ERROR" => "There was an error retrieving the tracking number.",
Code:
"TRACKING_NUMBER" => "Tracking Number",
Code:
));
Code:
echo $lang('TRACKING_NUMBER', '') . ': ' . $trackingNumber;

This isn't the most elegant solution since the language feature was intended for one language at a time instead of multiple, but I'll look at updating this so you can specify your language in your lang() function call. Let me know if you are able to get this going!
  Reply
#4
The way I formerly did it was this:

switch ($lang) {
case 0:
include_once 'lang/spanish.php';]

break;
case 1:
include_once 'lang/english.php';

break;


This way it didn't require any difference in coding as $lang[23] was the same string, in both files. Any way I can use my scripts to do this? The site I'm designing won't use much of the US interface.
  Reply
#5
Oh I'm sorry, I thought you wanted to use both languages side by side. In that case, you can do as you suggested and load either file. You can go through the language arrays to remove the string keys associated with each text line, but if you add a new line earlier in your array all your array keys will be off. I'd suggest keeping the keys as a string, since the end user will never see the anyway.

Code:
lang_en.php:
Code:
$lang = array_merge($lang, array(
Code:
"HELLO_WORLD" => "Hello world!"
Code:
));

Code:
echo lang('HELLO_WORLD);
outputs:
Code:
Hello world!


Code:
lang_es.php:
Code:
$lang = array_merge($lang, array(
Code:
"HELLO_WORLD" => "¡Hola Mundo!"
Code:
));

Code:
echo lang('HELLO_WORLD);
outputs:
Code:
¡Hola Mundo!
  Reply
#6
Okay so I'd have to add my existing arrays with the US ones, for <?= to work... okay.

Still wish I knew how <?= worked though lol
  Reply
#7
<?= is a shorthanded <?php echo

E.g. I have defined $test = "Blah blah blah"
I can then to <?=$test;?> (you can drop the ; actually) and it will echo this value.

You can read more here:
https://stackoverflow.com/questions/2020...ean-in-php
  Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)