How to code translation inside array

Hi developers,

I am trying to get the list options in a form to translate, but so far no luck. I can’t find how to code it properly. Tried the first option of the array to translate but it throws a syntax error:

in my new_form.class.php I have:

var $consciousness
var $consciousness_array = array("1" => '<?php echo xl('bright and alert'); ?>', "2" => "uncontrolled/hyperexcitable", "3" => "sopor", "4" => "stupor", "5" => "coma");

How do I do this? The only way the list is working is when I changed some " or ’ characters but then it only shows the code in the list and not the translation.

In my general_new.html it reads, and it works…:
{html_options name="consciousness" selected=$data->get_consciousness() options=$data->consciousness_array}

I am doing something wrong with " or ', but can’t figure it out. Any help greatly appreciated.

Roland

I think you need to escape the text with xlt or xls or xla. I never get this right so give it a try.

:slightly_smiling_face: openemr/htmlspecialchars.inc.php at master · openemr/openemr · GitHub

there are some helpful comments like xlt() for html tags, xla() for html attributes https://www.w3schools.com/html/html_attributes.asp, xls() for

Translate via xl() and then escape via addslashes for use with javascript literals

@sjpadgett
thanks, me neither, me no developer just a vet, so if you don’t get it right I’ll have to do trial and lots of errors…

@stephenwaite
thanks for the links, not sure what all that means yet, but I’ll try it.
I remember seeing the translation trick inside a listbox in some of the 60+ contributed forms, but I cannot remember which it was…
Roland

Actually, i’ve found best practice is to translate to var then use var in your array/object.

I get it. I just like to rattle Brady a little…

hi @roland ,

try:
var $consciousness_array = array("1" => xlt("bright and alert") , "2" => xlt("uncontrolled/hyperexcitable"), "3" => xlt("sopor"), "4" => xlt("stupor"), "5" => xlt("coma"));

-brady

hi @brady.miller,

thanks Brady, tried it, but now it says:
PHP Fatal error: Constant expression contains invalid operations in /var/www/git-openemr/interface/forms/vet_genphys_exam/Formvet_genphys_exam.class.php on line 36, referer: ...localhost/git-openemr/interface/patient_file/encounter/forms.php

I thought that maybe I couldn’t use code words like “alert” “and” or a slash, but that doesn’t change anything.

is this suppose to be a JS array or object?

Hi @roland,

(I think this is a php class var)

Class variables generally need to be static. Would need to set this var in the class constructor. Something is not right here, though, and rec posting your form on github so we can look at the entire form.

-brady

Just trying to figure out why the echo inside the array if a class array. Plus, however it is defined, still need to json encode if heading to javascript…,

I also initially thought it was js because of the echo and var. But pretty sure it’s straight php for a class var. Will be very helpful to see the entire form to sort out this mystery :slight_smile:

@brady.miller,

yes, brady, it is php class var.
The form is a :poodle::dromedary_camel::dog2: variant of Aron’s Racho’s ros2 review of systems form. It works fine, everything also translates ok, except the list choices, wanted to do that too.
So I thought why not move all the list choices (written in html that work fine, also easy to translate) to arrays in the Form…class.php, there are so many listboxes (see screenshot)


it would be easier to have them in one place, but when I did it’s no longer working.

this is what you get when you’re a vet and no developer… :disappointed:
-Roland

how the h… do I do that?:blush:

Hi @roland ,

In the wonderful world of github where all your childhood dreams will come true (I may be exaggerating just a bit)…

First step is to set up a git/github repository via the following tutorial (note that using git/github is well worth learning and will make it much easier for you to code, track your changes, get coding help from community, and integrate your code changes into future OpenEMR versions; and it’s just plain fun (again, I may be exaggerating again)):
http://www.open-emr.org/wiki/index.php/Git_for_dummies
(ignore the video)

-brady

Hi @brady.miller,

Ok, I was just going out to find the nearest lamppost and hang myself for not getting this to work…
Maybe github is the solution, so I’ll just go out for a slice of pizza (which almost equals slower suicide) and watch the video.
A bit nervous though, I hope my “code” will not be met with sarcasm, for I am no developer and it only applies to our non-human friends, but if I can get my things done, I can handle it as a dummie vet…

1 Like

@brady.miller, @sjpadgett, @stephenwaite

Hi guys,
thanks for all your comments. Forget the arrays, it was driving me crazy; my solution was maybe a bit lame for a beginning developer, but now the form works and it translates ok, also the options.
I did :
<select name="consciousness"/> <option value="BALR">{xl t="bright/alert/responsive"}</option> <option value="UNHY">{xl t="uncontrolled/hyperexcitable"}</option>

There’s still one thing: the listboxes do not keep their selections. How to achieve this? I studied all day on the internet and found some snippets, but there is a guy in the background of OpenEMR called Smarty and he doesn’t like what I’m doing. The code is either commented out and all converted to lowercase, when I write this:
<select name="consciousness"/> <option value="BALR" <?php if($_POST['consciousness'] == 'BALR') echo "selected=selected"; ?>{xl t="bright/alert/responsive"}</option>

or he throws an unrecognized tag ‘echo’ error at me, and the form doesn’t display at all, when I do this:

		<option value="UNHY" <?php if($_POST['consciousness'] == 'UNHY') {echo "selected=selected"; } ?>{xl t="uncontrolled/hyperexcitable"}</option>

The lamppost becomes more attractive every minute…

tx
Roland

@brady.miller, @sjpadgett, @stephenwaite

Problems solved; the solution was in OpenEMR itself, the vitals form has all the answers, translation, arrays and keep selections and all… next time I’ll look into OpenEMR instead of the internet first.
My form now works fine… time to give something back to the community, anyone interested in a veterinary general phys ex form? …
If I understand how to github, I’ll post it there…
no more hanging from lampposts
R

{if $data->get_consciousness() == “value” || $data->get_consciousness() == 2 } selected{/if}>{xl t=“value”}

2 Likes

nice @roland !
Definitely interested in a vet phys ex form for the project :smile:
If git/github guides you towards another lamppost, always feel free to send me the form (and I’ll post it to github).
-brady