Welcome Guest, Not a member yet? Register   Sign In
Best approach to handle languages in an app
#1

[eluser]Bramme[/eluser]
Hi everyone

I'm building an app and want to be able to provide decent language support. To understand what it does: the app will contain 'forms', for every form, there's a database table with the same name and an entry in the forms table. E.g.: I want a news form, so I'll make a 'news' table in my database and add 'news' to the forms table. Now, the news display name will have to be translated correctly offcourse, depending on what language you select, I want it to say "News", "Nieuws", "Nouveaux"... I'll also have a fields table that stores all the fields there are in the news form. This way, I'll also be able to translate the field labels ("News title", "Nieuws titel"...) and store other information. This is the basic gist.

I've worked on an application before and it would just have 5 tablefields: name1, name2, name3, name4, name5... Depending on what language the user prefers, name1, 2... was displayed. This is offcourse very limiting, as it's virtually impossible to add a new language.

So I'm thinking I should put the translations in a separate table, but I'm wondering what the best approach is: one extra table per table (so I'd have forms_translations, field_translations...) or one giant table (translations). These are the two things I can think of, but there's problably other ways out.

Have you ever encountered this problem, how did you solve it? How would you solve it? Which of my two approaches would be best?

Thanks in advance for any advice!
#2

[eluser]Kamarg[/eluser]
Personally, I'd use CodeIgniter's built in language capabilities. If I had to store my strings in the database, I'd go for one large table with a column that tells you what language the string belongs to instead of multiple tables for the same strings.
#3

[eluser]CroNiX[/eluser]
http://codeigniter.com/wiki/CodeIgniter_...ation_i18n

Haven't tried it.
#4

[eluser]Bramme[/eluser]
i18n still looks a little limitting/difficult to manage once you want to add a lot of languages. I'm gonna think about how to handle it, but will probably go for the table setup. Using language files are only part of the solution.
#5

[eluser]PhilTem[/eluser]
I thought of this problem once, too. I ended up with implementing it in the database and extending CI's Lang-class to also support a fallback-language in case the slug for e.g. french isn't defined, it'll simply display the english string.
Also thought of storing in a database vs. storing in a file. Database stored values take advantage of easier editing imho but file stored values are faster to read. Therefore I will make my MY_Lang to grab the rows from the DB like every 2h and cache them and from then on read the cached file (which gets deleted automatically when you update a row inside the database).

The concept is fully worked out, but I haven't implemented a single line of code of this. Just wanted to let you know about database vs. file storage in terms of speed Wink
#6

[eluser]Bramme[/eluser]
Hmm, I like that caching idea a lot.

Anybody knows if it's a huge speed decrease if I would cache the translations as a json file and use json_decode to read them in php? This way I can also use the translations in javascript files.
#7

[eluser]PhilTem[/eluser]
Maybe this one is a little useful http://stackoverflow.com/questions/80404...-serialize

I'm not sure, how much speed decrease you get with json_decode(). If you call the function once on every page-load it sure won't decrease speed that much. But if you do it multiple times, it might make some milliseconds but not that much I guess.

Just give it a try Wink
#8

[eluser]aquary[/eluser]
My own version for multi languages was like..
- In the database, there are tables, store all the text for each language, let say there are text_en, text_jp, text_cn. Each table has 2 fields (3 on the latest version), which are id, and the text.
- All these table are created/removed by adding/removing languages in the backend. Data would be copied from the "Primary language"
- One language is a primary one, cannot be removed, but the primary could be changed.

Now for other tables require a multilanguages fields, let say a news table:
- Those fields which required multilanguages, instead of being a varchar/text, they will be INT.
- On creation of the table, I'd add the text data to each languages, with some automated function, then save the insert_id, which is returned by the function itself.
- The same also applied to editing, but need the id of the text.
Code:
// insert/update text data to each language table,
// with value posted from the form, with input name of field_name_en, field_name_jp, field_name_cn, etc.
$id=$this->save_text('field_name'); // Insert mode
$id=$this->save_text('field_name', $news->news_title_id); // Update mode, send the stored text id

Lastly is about selecting data, which also has an automated function to select a good language, or all languanges (for backend part).
Code:
// autometically get the text for the current languages, or all langs.
$this->join_lang(array('news_title_id', 'news_content_id'));

The downside is, the query would be exponentially big in the backend, since I call 1 join / language / field. So with 3 fields and 3 languages, it's 9 joins in the backend. The worst I saw was like 7 fields on 5 languages, which is 35 joins....


Another point not yet fixed is, the table must be in sync. Only a row/id missing would make that language got screwed, happened mostly during development. >:3

Atleast it's reusable, without noticably slowing the DB on small-medium websites, as long as the tables are indexed correctly. >:3
#9

[eluser]Altazar[/eluser]
Hi, I started using CodeIgniter just two days ago. I've had some experience with Symfony before.

In my simple PHP websites I'm using SiteTranslator and now trying to use existing database in CI.
Each translation is stored in its own table.

I've tried to do it with "i18n" but it didn't work.
Please, suggest what to do. Thanks in advance!
#10

[eluser]Dani[/eluser]
Im thinking about the same topic myself. Problem with your first method is that it is very cumbersome to add a new language and also lots of work to manually translate each fields, names, paragraphs, items, text etc to all languages. You could also do like Wikipedia, but problem there is that texts across languages are totally independent. Another approach I've been thinking about is to make use of Bing Translation API. If you have facebook and have seen friends from other countries writing a status message, you'll notice they've added that translate feature. The pros with this method is that all texts are synchronized and translations are done with no work. Problem with this approach is that the translations are only decent. Translations will probably get better and better, but there _will_ sometimes be a need to manually correct a translation (because a sentence in a given language may have multiple meanings), and I'm not sure if Bing Translation API can collaborate with custom mods. And even if the translations were great, you'd still have to call the translate() function quite a lot of times (for each parts of your site that you want to have translated, i.e. the header, menu, text....).




Theme © iAndrew 2016 - Forum software by © MyBB