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
https://forums.phpfreaks.com/topic/229701-how-to-organize-language-translations/
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?

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.. :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.