Discussion: Skins and Localisation from Scratch
Posted: Tue Mar 02, 2010 12:21 pm
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
lang/fr.inc.php
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
skin/default/goodbye.inc.php
skin/red/greeting.inc.php
Again intentionally no goodbye.inc.php for the red skin
3. Pull it all together...
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.
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";
?>Code: Select all
<?php
$lang['hello']="Bonjour";
// goodbye missing to show the idea of the master language
?>skin/default/greeting.inc.php
Code: Select all
<?php
echo $lang['hello']."<br />";
?>Code: Select all
<?php
echo $lang['goodbye']."<br />";
?>Code: Select all
<?php
echo "<span class=\"color: red;\">".$lang['hello']."</span><br />";
?>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");
?>
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.