Multiple languages on website from mysql - php

Is it bad idea to hold translation in database and display words translation based on user settings. I imagined something like this:
mysql
lang | greeting
spanish | ola
english | hi
for example
$sql=mysql_query("SELECT * FROM lang where lang='spanish'");
and then just echo that value in proper div

You can have 1 table for your languages
id_lang | label
1 | English
2 | Spanish
3 | French
And then each your other tables can have a second key id like this for exemple :
traduction_table
id | id_lang | value
1 | 1 | Hi
2 | 2 | Ola
3 | 3 | Salut
You need tu have primary keys on id and id_lang.
Then you just have to make your SQL requests correctly, like :
select * from traduction_table where ID = 1 and id_lang = ??
Where ?? is replaced by the user language ofc.

From my experience, best is maintaining what I call "interface" texts (English "cancel",Spanish "cancelar",...) with .po files (gettext standard)
On the other hand, all texts likely to be editable by the user, they should be stored in database. Per example, if it is a site which needs to publish a description in different languages, and this description needs to be edited/maintained by the user, and not by the programmer, then you should store the translations in database. And yes,somehing like that table you said is a good idea.

This way you don't need to add columns for extra languages.
id | text | lang
------------------------
1 | ola |spanish
2 | hi |english

Related

How to store mostly-empty data?

I want to set up a system that allows for, say, 200 different translations per post. However most translations wouldn't exist, so there'd be a lot of empty datasets. How much of a performance and storage hit is it if I save every language (including empty ones) as a specific column? I.E.
English | Arabic | Mandarin | Russian | French | German
Potato | | | | Pomme de Terre |
Orange | | | | Orange |
Peach | | | | |
I wouldn't cycle through the whole list very often, I'd use a session variable or usersetting and then load directly from that column if it exists, with a fallback to a default language, and perhaps after that a full search.
if (exists(french))
{echo french}
else {if(exists(english))
{echo english}}
else {echo links to non-null language}
}
I'd assume that, if I tell the server which column to go to, the overhead in terms of processing would be negligible? I also assume that an empty cell would be negligible in terms of storage? However I don't know for sure, and it could potentially be a huge mistake.
The reason I'd want to work like this is so I could assign language codes, instead of every installed instance having a different order (e.g. english|french|german|mandarin versus english|mandarin|german|french).
To prevent XY-problems, here's a more global formulation:
I want to set up a system that allows for many languages, but I expect that in most cases only 1 or two are used. What would be an efficient way to store?
Keyword: Relational database.
You will want to use multiple tables.
Let's say that the default langauge is english, then your "words" table will implicitly contain the english words.
Words:
Id | Word
1 | Potato
2 | Orange
Languages:
Id | Name
1 | Norwegian
2 | Danish
Translations:
Word | Language | Translated
1 | 1 | Potet
2 | 1 | Oransje
1 | 2 | Kartoffel
2 | 2 | Appelsin
Then you can do (pseudo sql, you can look up the language and word ids first, or use a more advanced query):
SELECT Translated FROM Translations WHERE Word = (the word id) and Language = (the language id)
This comes with the benefit that it's very simple to list all the languages you support, all the Words you support, and also all translated words for a specific language (or, find all NON translated words for a language).
A specific query for translating "Potato" into "Danish" would look like:
SELECT Translated FROM Translations
JOIN Words ON Words.Id = Translations.Word
JOIN Languages ON Languages.Id = Translations.Language
WHERE
Languages.Name = "Danish" and Words.Word = "Potato"

PHP MySQL Multi-Language website

I am trying to build an English and French website by pulling the information from a MySQL table but I am stuck. I think that kind of query is too advance for me. My idea is having a table with both language translated and assign them with an ID.
Here's the table lang
+--------+-------------------+----------------------+
| id | English | French |
+--------+-------------------+----------------------+
| 1 | Verbal Warning | Avis verbal |
| 2 | Written Warning | Avis écrit |
| 3 | Evaluation | Évaluation |
| 4 | Other (specify) | Autres (spécifiez) |
+--------+-------------------+----------------------+
Then I have another table that people inputs 'Topic' into the database. So when I switch the page to French the content of the table will display in French.
+-----------+---------+
| EMP_ID | Topic |
+-----------+---------+
| 845689 | 4 |
| 185648 | 3 |
| 485497 | 1 |
| 416798 | 2 |
+-----------+---------+
I want the ouput to be this in a table when we're on the English page
+-----------+------------------+
| EMP_ID | Topic |
+-----------+------------------+
| 845689 | Other (specify) |
| 185648 | Evaluation |
| 485497 | Verbal Warning |
| 416798 | Written Warning |
+-----------+------------------+
then this when it's the French page is selected.
+-----------+---------------------+
| EMP_ID | Topic |
+-----------+---------------------+
| 845689 | Autres (spécifiez) |
| 185648 | Évaluation |
| 485497 | Avis verbal |
| 416798 | Avis écrit |
+-----------+---------------------+
Is there a way to make it work or there's easier ways to display
As suggested in another answer, it would be better to store your language strings in files, and use Javascript to load them. I would suggest to use i18next, which is a very useful and easy-to-use JS library. It would really be simple :
/* en.json */
{
"home":{
"title":"Hello, world !",
"welcomeMessage":"Welcome to my great website !"
}
}
/* fr.json */
{
"home":{
"title":"Bonjour !",
"welcomeMessage":"Bienvenue sur mon superbe site web"
}
}
In your html code :
<!-- head and other stuffs... -->
<script type="text/javascript" src="i18next.js"></script>
<body onload="translate()">
<h1 i18n-data="home.title"></h1>
<p i18n-data="home.welcomeMessage"></p>
</body>
In your functions JS file :
function translate() {
i18n.init({
resGetPath:'en.json',
getAsync:false
}).done(function(){
$('[data-i18n]').i18n();
});
}
This way, your website will run faster (less database calls), and it will be easier to add/update some strings. Hope it will help :)
There is no reason to keep lang schema in DB, try to include files with your lang instead.
Well you shouldn't change the table content each time someone loads the page that would be a performance catastrophe ;-) But you can make a SQL Join Query.
If you are on the french page you do
SELECT l.French FROM topic AS t
JOIN lang AS l ON t.Topic = l.id
WHERE t.EMP_ID = $emp_id;
and on the english you do
SELECT l.English FROM topic AS t
JOIN lang AS l ON t.Topic = l.id
WHERE t.EMP_ID = $emp_id;
I think the easiest way will be to create a view per language:
For French:
CREATE VIEW empl_fr
AS
SELECT
emp.EMP_ID as EMP_ID
, lang.French as Topic
FROM employee emp, language lang
WHERE emp.Topic = lang.id
For English:
CREATE VIEW empl_en
AS
SELECT
emp.EMP_ID as EMP_ID
, lang.English as Topic
FROM employee emp, language lang
WHERE emp.Topic = lang.id
and then you can query your topics like:
SELECT * FROM empl_fr
or
SELECT * FROM empl_en
The more suffisticated way will be to create a view with both languages and query your view with a language parameter
To make your site extensible and easy to support future, additional languages; I’d suggest using MySQL in its intended fashion and create a relational schema.
If you have topics, and topic titles are to be translatable, then you’ll need three tables: a topics table, a languages table, and a table that joins the two.
Your topics table is easy: you just need a primary key, and any other language-independent columns (such as created, modified etc). For the languages table, store again a primary key, the language name, and maybe an ISO short code for consistent naming/identification.
Finally, your join table (could be called languages_topics) is where the associations happen. You would have two foreign keys (topic_id and language_id) and another column that actually holds the localised value.
For example, if English is language ID 1 and French is language ID 2, then your table could look as follows:
+----------+-------------+----------------+
| topic_id | language_id | value |
+----------+-------------+----------------+
| 1 | 1 | Verbal Warning |
| 1 | 2 | Avis verbal |
+----------+-------------+----------------+
As you can see, a topic can have multiple rows in this table, with a record per language to offer up the translations.
I know this maybe doesn’t exactly answer your question, but should set you on the right path on how best to store your data. Languages can easily be added in the future without needing to modify your database schema.

Multiple columns for languages vs. serialized array in a one column

I am trying to create a table in a more accurate and professional manner.
I need two fields TITLE and DESCRIPTION for two languages, but I'm not quite sure if I won't add a third language after a while.
Which one would you prefer?
-----------------------------------------------------------
id | title_en | title_ge | description_en | description_ge
-----------------------------------------------------------
...|..........|..........|................|................
-----------------------------------------------------------
or
---------------------------
id | titles | descriptions
---------------------------
...|serialized| serialized
---------------------------
where serialized is like this
array(
'en' => 'title for english',
'ge' => 'title for georgian'
)
Or any other suggestions? Thanks for future recomendations.
I would do it in a combination of two tables.
Your first table should include titles and description in just english. Similar to what you have right now.
Your secondary table will include translations. With a structure something like:
-----------------------------------
doc_id | lang | title | description
-----------------------------------
You would put a primary key that would include both doc_id and lang and fulltext indexes on title and description.
This way you could construct a query that would fallback to the original english text if a translation for the language you are looking for does not exist, as well as limit your searching to one specific language or another.
I would create a table with four fields
(id,title,description,language)
--------------------------------------
id | titles | descriptions | language
---------------------------------------
1 |title in en| desc in en | en
----------------------------------------
2 |title in ge| desc in ge | ge

MYSQL Comma Delimited list, possible to add and remove values?

I have a comma delimited list that im storing in a varchar field in a mysql table.
Is it possible to add and remove values from the list directly using sql queries? Or do I have to take the data out of the table, manipulate in PHP and replace it back into mysql?
There is no way to do it in InnoDB and MyIsam engines in mysql. Might be in other engines (check CSV engine).
You can do it in a stored procedure, but, not recommended.
What you should do to solve such an issue is to refactor your code and normalize your DB =>
original table
T1: id | data | some_other_data
1 | gg,jj,ss,ee,tt,hh | abanibi
To become:
T1: id | some_other_data
1 | abanibi
T2: id | t1_id | data_piece
1 | 1 | gg
2 | 1 | jj
3 | 1 | ss
4 | 1 | ee
5 | 1 | tt
6 | 1 | hh
and if data_piece is a constant value in the system which is reused a lot, you need to add there a lookup table too.
I know it looks more work, but then it will save you issues like you have now, which take much more time to solve.

How can I make multi-language website? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Create a webpage with Multilanguage in PHP
PHP - how to translate a website into multiple languages?
I want to make a site which will have 3 languages - e.g. English, Arabic and Italian;
the content sure will be different from one language to another.
Should I make different table for each language, e.g.:
en_articles
ar_articles
it_articles
each with the same article in different language,
or make one table articles like this:
article_id
article_en_title
article_ar_title
article_it_title
Please advise me.
Create a table with a list of languages, and an articles table with a language column. That way, if you add a new language, you only need to add it to the languages table.
Example:
table `languages`:
| id | name |
================
| 1 | English |
| 2 | Arabic |
| 3 | Italian |
table `articles` (only relevant columns):
| language_id | title | content |
=======================================================================
| 1 | Some title | Some content in English |
| 3 | Ascia | Dio mio! C'e' un' ascia nella mia testa! |
| 1 | Axe | Oh my god! There's an axe in my head! |
This way, you won't need to change the database schema when adding languages. As you can see, there is one articles table with one content column - significantly simpler to use than multiple article tables or multiple content columns.
If you are very sure that you are going to work only with 3 languages, the best option is to use one table, with three columns, one for language:
article_id
article_en_title
article_ar_title
article_it_title
If eventually you need to add other language, only add other column.
If you think that you are going to add other languages, o you want to use the code for others web with differents languages, I think that the best solution is to use 3 tables, one for the languages, one for the articles and other table for relation them
table "languages"
language_iso
language_name
table "articles"
article_id
article_name (Internal name for the article)
table "articles_x_languages"
article_id
language_iso
article_title
article_text
I'm assuming that you are going to have each article in the three languages. Example:
Languages
language_iso | language_name
en | English
ar | Arabic
it | Italian
Articles
article_id | article_name
1 | Sample 1
2 | Sample 2
Articles_x_languages
article_id | language_iso | article_title | article_text
1 | en | english title | Lorem ipsum ..
1 | ar | arabic title | Lorem ipsum ..
1 | it | italian title | Lorem ipsum ..
2 | en | english title | Lorem ipsum ..
2 | ar | arabic title | Lorem ipsum ..
2 | it | italian title | Lorem ipsum ..
I would suggest that you create only one table for the articles and put a column for the language. So, if you need to add a new language you don't need to change anything in your db
When you use PHP, you can see here: http://www.bitrepository.com/php-how-to-add-multi-language-support-to-a-website.html
If you are writing your website using java you might want to search about JAVA ResourceBundle, here is an example : http://java.sun.com/docs/books/tutorial/i18n/resbundle/propfile.html
If you are working with asp.NET you might want to check this: http://msdn.microsoft.com/en-us/library/ms228208.
If you work with ASP.Net , look at here
http://msdn.microsoft.com/en-us/library/c6zyy3s9.aspx

Categories