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
Related
I have a problem, namely I want to display hashtags from such table "posts":
| id | author | hashtags |
|:------------|-------------:|
| 1 | aaaa | #aaa, #bbbb | etc...
| 2 | bbbb | #ccc, #addd |
| 3 | cccc | #aee, #ffff |
The problem is that I would like to get something like this (only those records):
When I write a: #aaa, #addd, #aee
When I write b: #bbbb
etc...
How do I get the effect that only those hashtags that start with a letter (even in the same field) appear to me? I was thinking about regular expressions. Thanks in advance for any help.
You need to make a couple of changes:
Move hashtags columns to a new table and store different hashtags in different rows (with post id as foreign key) i.e. normalize the table
use LIKE operator to query on the new table, e.g.
SELECT hashtag
FROM post_hashtag
WHERE post_id = <id>
AND hashtag LIKE '%#a%';`
I'm trying to come up with some sort solution to a problem where I have to provide a user with dynamic dropdowns depending on the options they choose.
Currently I have 3 tables that are normalized as such.
Currently this works well with my HTML select elements, where if I select John Doe I would get Paul, Kevin and Dick as my second options and if I were to choose Kevin I would get Drake and Kanye as a third option.
My issue is that I do not want to keep creating tables since I would like to add more layers of staff_level in my application.
How would I approach this and have a fully dynamic table structure using PHP and MySQL?
Thank you for taking your time to read this.
You want an association table between the people. Put all of them in one table with unique IDs like so:
Table Staff
id | Name | <Other fields>
----+-------------+----------
1 | John Doe |
2 | Sam Smith |
3 | John Johns |
4 | Paul Pete |
5 | Kevin Mayor |
6 | Dick Ross |
...
Then the association table named whatever you like - maybe StaffHeirarchy:
Table StaffRelationships
id | ManagerId | SubordinateId
---+-----------+--------------
* | Null | 1 # Has no manager
* | 2 | 6 # Dick Ross is subordinate to Sam Smith
This table should have an id field for unique keys, but you don't have to care about what it is (it's not used as a Foreign Key as the Staff.id field is), which is why I put * there - in reality it would be some integer id.
I haven't seen your PHP for pulling values out of the database, but it is basically the same - query the association table filtering for the id of the manager you are looking for and you will get the ids of the subordinates (which you can JOIN on the staff table to get the names).
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
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.
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