Discussion: Skins and Localisation from Scratch

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
davex
Forum Contributor
Posts: 101
Joined: Sat Feb 27, 2010 4:10 pm
Location: Namibia

Discussion: Skins and Localisation from Scratch

Post by davex »

Hi,

Firstly; I think this is a discussion - apologies if anyone thinks it's in the wrong place and feel free to move it.

Secondly; I know there are about four and a half million ready built template engines etc which can do this but I enjoy the poking around with stuff (what I regard the fun bit of development).

The question: What does everyone think the best way to do a (from scratch - see above) system for allowing skins and localisation within an application.

This is what I have been thinking about while staring blankly at a wall at work...

(Oh a third thing - obviously in a real life application it would all be wrapped in classes etc and optimised but for the purposes of clarity it's not in the example).

1. Have an array or whatever containing "language items" - these are strings, words, whatever that is used in the interface. Load a default language that you know will have everything and then if specified load the users language.

lang/en.inc.php

Code: Select all

<?php
$lang['hello']="Hello";
$lang['goodbye']="Goodbye";
?>
lang/fr.inc.php

Code: Select all

<?php
$lang['hello']="Bonjour";
// goodbye missing to show the idea of the master language
?>
2. Have files within a specific skin directory containing the actual display elements. Again there would be a "master" which you know to contain all elements. If a user has set a specific skin try and load the element from that skin, if not then use the default as a catch-all.

skin/default/greeting.inc.php

Code: Select all

<?php
echo $lang['hello']."<br />";
?>
skin/default/goodbye.inc.php

Code: Select all

<?php
echo $lang['goodbye']."<br />";
?>
skin/red/greeting.inc.php

Code: Select all

<?php
echo "<span class=\"color: red;\">".$lang['hello']."</span><br />";
?>
Again intentionally no goodbye.inc.php for the red skin

3. Pull it all together...

Code: Select all

<?php
$default_language = "en";
$user_language = "fr";
$default_skin = "default";
$user_skin = "red";
 
function Display_Element($element)
{
 global $default_skin,$user_skin;
 $user_skin_element = "skin/".$user_skin."/".$element.".inc.php";
 $default_skin_element = "skin/".$default_skin."/".$element.".inc.php";
 if (file_exists($user_skin_element)) include($user_skin_element); // try for user skin element
 else if (file_exists($default_skin_element)) include($default_skin_element); // or the default
 else echo "Skin Element Error";
}
 
include("lang/".$default_language.".inc.php"); // include the "base"/default language
include("lang/".$user_language.".inc.php"); // include user language
 
// now some actual display code...
Display_Element("greeting");
Display_Element("goodbye");
?>
 
 
The above should (obviously there may be parse errors as just typed into the forum) display:

Bonjour (in red - user skin of red and then user language of French)
Goodbye (in black - no user skin element for this and no French version)

So... I hope I've made this clear. What do you think? As I said this is just a discussion really on what/how the best from-scratch way to do this would be.

Cheers,

Dave.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Discussion: Skins and Localisation from Scratch

Post by Benjamin »

:arrow: Moved to PHP - Theory and Design
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Discussion: Skins and Localisation from Scratch

Post by josh »

Define skin.
Define what is being localized ('happy' text in templates? Form labels? Measurements? Dates? Times? Currencies? )

For "skins" I generally thing CSS (the markup should not have to change to change the look).

Although in practice I do set up multiple "layouts". Each layout is a folder that holds it's design files (2 column layout, 1 column layout). etc.. which calls for different markup. I think group resource files. If there is CSS specific to only the 1 column layout, I put it in that layout folder. If it is common to 2 more or more, it goes into a default/base layout.

For translation I think you are on the right track. Normally there is something like one csv file per language, per module.

Instead of doing echo _"hello", I would do echo _"homepage_greeting". Avoids issues of words being used in different contexts (ex. maybe in English its the same word for two meanings, but in French they have distinct words for that concept?). It's also a "cleaner" way for the client to edit their text in English. Would be kind of silly to have a translation file that says "hello" = "hi" (confusing). Better to have "homepage_greeting" = "hi"
Post Reply