Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP better practice to build a multi language site

Tags:

php

mysql

i have a website that i built in two languages including two different "dictionaries" based on browser language

in each langXX.php i have

define('SOME_STRING', 'Some string');

etc... but i hate now this solution because i'm never sure if i included a new definition in both files...so i'm trying to move including all the definition in mysql and build a function like

translate('SOME_STRING', 'en'); // ouput 'Some string'

i'm moving to mysql because id like to manage the translation directly via a CMS i will build, i dont want to have the webmaster going trough ftp and textmate...

so my question is, if this latter solution i sbetter or is too stressful for mysql to get a query for each text element of the page..

PS. as far as i remember even osCommerce used this practice right?

like image 380
Francesco Avatar asked Mar 09 '11 14:03

Francesco


People also ask

Which website builder is best for multiple languages?

Why WordPress is the Best Platform for Creating a Multilingual Website. WordPress isn't just the best way to make a multi language website because of its popularity – it also offers all the tools you need to quickly and easily translate your site into new languages.

How do I add PHP multilingual support to my website?

Ways to add multi-language support to a website There are various ways to enable Multi-language support for an application. By having duplicate pages with the same content in the required languages. By managing language configuration (localization) with files containing texts in different languages.

How PHP implement multiple language?

lang_pl.php Add <meta > tag to set charset to utf8 . Create a <select > element with language options and create a <form> where use constant variables. Submit the language form when the option is selected from the <select> element. Initialize $_SESSION['lang'] with submitted language value and include a file.


2 Answers

Why not use file caching of the data stored in the database? You remove a potentially costly, heavily repeated set of database calls, but can retain the advantages of CMS overview of language values.

  • Store translations in the database
  • Have a process for generating a flat file from the database values
  • Re-fire this process every time a translation update occurs
  • Use the flat file in any front-end translation retrieval operations.
like image 157
Jeff Parker Avatar answered Sep 28 '22 22:09

Jeff Parker


A simple method used to translate text elements of your website could be broken down into this simple three-table structure:

Language
--------
Id
Name    # English, French, etc
Code    # en-gb, fr, etc


# Text table stores content in the default language.

Text
----
Id
Content


# Translations relate to a language and an existing Text

Translation
-----------
FkText
FkLanguage
Content

This way, a simple query can obtain the requested content in any language, automatically falling back to default language if there is no translation.

like image 45
Greg Avatar answered Sep 29 '22 00:09

Greg