Jump to content

Best way to manage language change on a static website


Ricky55

Recommended Posts

Hi

 

I have a static website that uses some basic PHP, includes, changing titles, conditionally loading CSS and JS files etc but no database.

 

What would be the best way to show this website in four different languages?

 

Just wondered if any of you guys have had to do that without using a CMS.

 

Thanks

 

Richard

Link to comment
Share on other sites

I use a template engine for this. I store all of the common (and short) text in a language file, and read it when I generate the template object. For bigger texts it supports localized versions of the template files, so that I can have one file named "info_no.php" and one that's named "info_en.php" and the script picks the best suited one.

 

So, no. You won't need a CMS or a database for this, but you will need a template engine.

Link to comment
Share on other sites

So, no. You won't need a CMS or a database for this, but you will need a template engine.

 

Why?

 

@Ricky55

 

I'm guessing given that you're not using a framework, you have some kind of 'master' include file? In there I would just construct an object with the user's language and a translation array (if not English). Then whenever you output text just pipe it through a method in the object:

 

<p><?php echo $lang->t('Some text here.') ?></p>

 

If the user's language is English, or your default language, it would do nothing but return the string. If another language it would attempt to look it up in the translation array. The array could simply be created from a JSON file:

 

translations/fr.json

{
    "Some text here.": "Certaines parties du texte ici."
}

 

(Thanks Google translate!)

Link to comment
Share on other sites

IMO I like keywords better, but I might do it something like this:

 

$lang = array(
     "KEYWORD1" => array(
          "This is English",
          "This is Spanish",
          "This is Chinese"
     ),
     "KEYWORD2" => array(
          "This is a second English keyword",
          "This is a second Spanish keyword",
          "This is a second Chinese keyword"
     )
);

Link to comment
Share on other sites

Easiest (but not the cheapest) way to do this is to hand it off to someone else.  Companies like translations.com offer services that will run a proxy in front of your website and serve a human translated version of it.  They basically use a scraper to get all of your site content, build out a human translated translation memory from that, and then dynamically do a search/replace of your content as it's served from their proxies.

 

And if you are going to build something in PHP to do this, look at the built in gettext() library.  That's based off an industry standard so it's easy to find 3rd parties and tools to manage your translation bundles.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.