SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2002
    Location
    Denmark
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Good language file tacticts?

    I have initiated the work to get a CMS script translate into a few more languages. My plan was to replace all words and phrases in the code with variables and then include a language_french.php.ini file with variables and translated phrases. Eg.

    $_click_here = "Click here"
    etc...

    Is this a smart enough way to do it, or am I shooting myself in the foot doing something stupid here?

    Thanks..

  2. #2
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Better way is to store translated strings in the database, it's easier to manage than a file.
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2002
    Location
    Denmark
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is it? I figured that all the database requests would slow the site down, but I have no idea actually.

    One problem though with the database is that it requires our users/customers to update the database (backup data, drop old DB, upload new DB, import saved data...) whereas doing it in files just requires a quick FTP upload..

    Or maybe I'm missing something.

  4. #4
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, db requests may slow-down the site, but requesting a file read also generates a disk read request. Anyway, the way I handle the multilingual strings is via database but I get the results via the cache proxy that uses memcache. This is the best solution - if the result if found in cache, it is returned, otherwise it is from the database but the result is added to cache, so next time the same request will be from cache.

    Also, and this is important, mysql usually caches the query results, so getting result of the same exact sql query returns the result from mysql's cache. This would depend on the settings of mysql and on how you query the database.

    If you want to use files, there is an extension called gettext that is designed specifically for storing the multilingual strings. If you want to go with the file-based storage of translations, better go with the gettext because it's a standard.
    http://us2.php.net/gettext
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2002
    Location
    Denmark
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually my plan is something else ( I think).. My idea was to simply put

    include ("language_french.php.ini");

    in the header of all CMS pages, so in this case French would be loaded. language_french.php.ini would then simply contain phrase variables, e.g:

    $_yes = "Oui";
    $_no = "Non";
    ...
    ....

    And in the code I would put variables:

    <select class="cp_select" id="voting" name="vote"
    <option value="<php echo $_yes ?>"><php echo $_yes ?></option>
    <option value="<php echo $_no ?>"<php echo $_no ?></option>
    </select>

    So by replacing language_french.php.ini with language_spanish.php.ini I have the site translated.
    This also only requires one (extra) file reading in getting the language_spanish.php.ini file. Of course it then requires some php processing - four in this case.

    To me this seems quite neat, but maybe I'm missing something?

  6. #6
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds simple, and may actually work. The more sophisticated translation classes also have options to fallback to a string in default language in case a string in selected language does not exist.

    Also more sophisticated classes orginize strings by 'page', for example a set of language strings may belong to 'home_page' or to 'blog_article' page. This way you can select all strings that belong to the same page in one select.

    Your solution is very simple, if it fits your needs, then use it. If you want something more sophisticated take a look at how its done in a popular pear class:

    http://pear.php.net/package/Translation2

    it has options to store translation strings in database or in xml file or in gettext type of files, you have total control.

    I also wrote a class inspired by Translation2 but using newest php5 and spl classes. If you follow my sig, find the svn, look in classes/clsTr, clsTrPage, clsTrString
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  7. #7
    SitePoint Enthusiast
    Join Date
    Jul 2002
    Location
    Denmark
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I will try with this simple version here - I'll keep your suggestions here in mind if it doesn't work out.

    Thanks for the talk!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •