Jump to content

how to organize language translations


php_guest

Recommended Posts

I am developing a multilingual site. I use array to save translations in one file which I include it inside settings.php.

//Example of english 
$LANG['welcome']="Welcome";
$LANG['status']="Status";

 

I am wondering about two things:

 

1. How would be the best option to print those arrays?

a)

Echo all html and inside html I use

echo "
<body>
<div>
<p>{$LANG['welcome']}</p>
</div>
";

b)

Close php and write html and inside html I open and close php for each time

<div><p><?php echo $LANG['welcome']; ?></p></div>

 

Each one has own downsides. Option a) spends extra resources to print html. Option b) spends extra resources to close and open php each time (could means that single script needs to open and close php even 30 times per script). Is that bad or is the ammount of resources spend for this negligible?

 

2.

Also how do you translate words inside javascript? I was thinking to put all translation that are need for javascript into $LANG['js'] array. Then I would use at the head of the script foreach $LANG['js'] to print html to assign javascript language variables.

<script type="javascript">
<?php
foreach($LANG['js'] as $keyword => $translation){
  echo "var ".$keyword."= ".$translation.";\n";
}
?>
</script>

Is there any better way?

 

Thank you!

Link to comment
Share on other sites

I have mysql but the question is not how to store variables but how to print them. So to print as option a) as whole html or as b) as closing and opening for each translation php.

 

Regarding JSON, is there any other option since I am not familiar with JSON?

Link to comment
Share on other sites

my multiligual website uses 1, a) with small mod (using single quotes instead of double)

 

echo '
<body>
<div>
<p>'.$LANG['welcome'].'</p>
</div>
';

 

 

as for javascript...

I  use a php file as javascript file.. that way I can alter it using PHP variables

 

<script src="javascript.js.php" type="text/javascript" />

 

and

 

<?php

header("Content-type: text/javascript");

?>
// Javascript down here

 

but that´s just me.. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.