Jump to content

Adding another language !


Stemar
 Share

Recommended Posts

Hi guys, For some time now, I'm working on a project just for fun so I can improve my PHP skills. One thing that I want to add is a selection for languages for example English and Spanish.I've been thinking about this and I've figured out 3 possibles ways for doing so:1- create a mysql table with the menus, and all of the explanatios and then their translates. I believe this will oversaturate the database.2- define some arrays with the translations and then include them where the values are needed. 3- create copies of the scripts and then translate them entirely. Like I said this is just for fun so the last one works fine for me but I'm curious which one is the most efficient or which one is used on really big projects?

Link to comment
Share on other sites

Well this depends. What exactly are you developing? Is it a project that is going to be distributed, or a personal project?

 

Personally, I like to create language files in php. There are plenty of ways to do this. I'm not sure if it's the most efficient way, but...

<?php// ONE file with arrays, probably not as good as my next idea// English$lan['en']['intro'] = 'Hello, and welcome blah blah';$lan['en']['content'] = 'Oh and here is some content.';// Spanish$lan['sp']['intro'] = 'No hablo espanol';$lan['sp']['content'] = 'Queso es delicioso';?>
<?php$language = // pull language type, en or spif(!$language) $language = 'en'; // or sp is Spanish is default, of course// Now you can use the language variablesecho $lan[$language]['intro'];echo $lan[$language]['content'];?>

Or create two pages.

 

One for English which would contain

<?php// english.php$lan['intro'] = 'Hello, and welcome blah blah';$lan['content'] = 'Oh and here is some content.';?>

And one for Spanish

<?php// spanish.php$lan['intro'] = 'No hablo espanol';$lan['content'] = 'Queso es delicioso';?>

And of course the file which decides which one to grab

<?php$language = // pull language type, en or sp if($language = 'sp') include 'spanish.php';else include 'english.php'; // Or reverse if spanish is default, of course// Now you can use the language variablesecho $lan['intro'];echo $lan['content'];?>
Edited by Christopher.Burkhouse
  • Like 1
Link to comment
Share on other sites

Personally, I'd use my latter example. Of everything that comes to mind, it seems to be the most efficient way (one page for each language).

 

Plus, with the latter, if you wanted to add additional languages you could just add say a german.php or russian.php file and add it to the if/else structure once.

 

There's just no real reason to store all of this on a MySQL database since you have access to PHP files and could easily modify the language files yourself, you know?

 

Side note, if you have an English/Spanish button, you can save it as a cookie. If they're logged in, you can also store it with their login info.

 

Anyways, I hope this helped out. Make sure to like the response if it did (: And if any part of it wasn't clear, please ask. We're here to help.

 

-Chris

Edited by Christopher.Burkhouse
Link to comment
Share on other sites

It's a common practice to have a set of language files and include the one that's needed. A database is probably not as good an idea. The good thing with language files is that you can send it to a translator and have them change the strings without needing to create a database interface for them.

 

I suggest using printf() or sprintf() for them, so that you can have variables in the string:

 

English:

$lang['choose'] = 'Choose %d items.';

Spanish:

$lang['choose'] = 'Elige %d objetos.';

On your page:

printf($lang['choose'], 3);
  • Like 3
Link to comment
Share on other sites

For some reason, doing it like that never crossed my mind, Ingolme. I keep forgetting that printf and sprintf exist, despite being very useful functions. I'm in that phase where I'm trying to break my newbie habits still lol.

 

-Chris

Edited by Christopher.Burkhouse
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...