Store a variable as variable in database

Hello im building a multilenguaje app in php, this is the first time i handle multilenguaje, im trying to do it like this.

For months for example

i have a table "months" with id | month estructure

in the users table i have id | username | month_id

my guess (bad guess) its that i can save for example $january direct on the database so in the php when its called, it looked for the defined variable.

$january = "january"; and if im on spanish web then i change this to $january = "Enero";

but this is not working (obviusly because it prints as a result of a call $january, Its saved as $january in the database)

How is the best method for what im trying to do.. cause im lost in this point.. and i need this estructure in the database for a lot of reasons i wont mention, right now,

thanks!

My code

This $row2['months']

Print this $january

Even when in the php code i have set $january = "enero";

Answers


To get php to evaluate that variable, what you need to do is eval:

$january='Enero';
$row='$january';

echo $row;                     //Prints $january
echo PHP_EOL;
echo eval("return ".$row.";"); //Prints Enero

Here that is in ideOne: http://ideone.com/f0LCT0


You might rethink your strategy.

Example:

For 10 languages if you keep 10 php files defining the month names or actually all language locale words in, it will be sufficient and you can include them without disturbing the database.

<?php

 // this is words_en.php, you can make another like words_de.php etc.
 $months = array('january','february','march','april'....etc );

?>

If you structure your locale file consistently for instance:

 $msgs = array(
    'MSG1'=>'first message',...
 }

keeping only the references(like'MSG1') in your code and also in your database will be sufficient. For the months, you can keep them apart from $msgs since their use is specific and numeric indexing adds more consistency to coding for their case.

Note that this is not the only method for language locales but this is an easy to maintain version.


You should save the users locale e.g. en_US in your table and base further translations on that value with a fallback to the default language.


Need Your Help

JavaFX 2, Add items from ListView to TableView

listview javafx-2 tableview

A TableView is filled by double left click on a ListView item(s).

Import/export ios data (e.g. CoreData) from/to local files on Mac

ios import export core-data-migration

I'm developing an app with a feature that needs to import and export from local files.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.