We are working to translate our site into different languages. Currently, we pull 80% of a page from a database that is already translated. We drop in a language id and output the appropriate content.
What I need to store and access are the bits and pieces of text that need to appear on every page, but won't be stored in a database. For example, a save button might say "click to save" in English, or "clique acqui" in French, or "clicken zie here" in German.
We might have 10 - 20 different pages (templates) on our site that will require this hard coded translated text. There might be 20 - 40 bits of text on a single page that need to be output into any of ten languages.
My solution is to create a global array (or structure) that contains all of the text pieces in different languages. We will populate the array with hard coded strings. When a person changes their language id, we will just access a different part of the array and output the appropriate text.
Below, I have a working page that that enables you to toggle your language and access different text pieces.
My question is, is there a major pitfall I am overlooking or is there an easier way to get this done?
<html>
<head>
<title>EJ's Test Page</title>
<!--- JQUERY --->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<body>
<cfscript>
// SET LANGUAGE
if (structKeyExists(FORM, "LanguageID")) {
LanguageID = FORM.LanguageID;
} else {
LanguageID = 1;
}
</cfscript>
<form method="post">
<select name="LanguageID" onchange="javascript:form.submit();">
<option value="1" <cfif LanguageID eq 1>selected="selected"</cfif> >American</option>
<option value="2" <cfif LanguageID eq 2>selected="selected"</cfif> >German</option>
<option value="3" <cfif LanguageID eq 3>selected="selected"</cfif> >French</option>
</select>
</form>
<cfscript>
// GET PAGE CONTENT
PageContent = getPageContent(LanguageID);
</cfscript>
<cfoutput>
<h1>#PageContent.Text1#</h1>
<p><b>#PageContent.Text3#</b> - #PageContent.Text4#</p>
<button id="MyButton">#PageContent.Text2#</button>
</cfoutput>
<!--- GET PAGE CONTENT --->
<cffunction name="getPageContent">
<cfscript>
makeLanguageArray();
PC = APPLICATION.PageContent[LanguageID];
return PC;
</cfscript>
</cffunction>
<!--- MAKE LANGUAGE ARRAY --->
<cffunction name="makeLanguageArray">
<cfscript>
if (not structKeyExists(APPLICATION, "PageContent")) {
APPLICATION.PageContent = structNew();
// ENGLISH
i = 1;
APPLICATION.PageContent[i] = structNew();
APPLICATION.PageContent[i].Text1 = "What a fine day!";
APPLICATION.PageContent[i].Text2 = "The flowers are in bloom.";
APPLICATION.PageContent[i].Text3 = "Follow the money";
APPLICATION.PageContent[i].Text4 = "Save to List";
// GERMAN
i++;
APPLICATION.PageContent[i] = structNew();
APPLICATION.PageContent[i].Text1 = "Einen Augenblick, bitte!";
APPLICATION.PageContent[i].Text2 = "Wie teuer ist das?";
APPLICATION.PageContent[i].Text3 = "Kommen Sie mit!";
APPLICATION.PageContent[i].Text4 = "Gute Nacht!";
// FRENCH
i++;
APPLICATION.PageContent[i] = structNew();
APPLICATION.PageContent[i].Text1 = "Je ne l'ai pas fait intentionnellement.";
APPLICATION.PageContent[i].Text2 = "C’est lui l’agresseur.";
APPLICATION.PageContent[i].Text3 = "Je viens d’être victime de vol.";
APPLICATION.PageContent[i].Text4 = "Ce n’est pas ma faute.";
}
</cfscript>
</cffunction>
<script>
<cfoutput>
var ButtonText = '#PageContent.Text4#';
</cfoutput>
$MyButton = $("#MyButton");
throwAlert = function() {
alert(ButtonText);
}
$MyButton.click(throwAlert);
</script>
</body>
</html>
-
\$\begingroup\$ There are several i18n packages out there. github.com/jmohler1970/PHP-Language-File-in-ColdFusion is one of them. Disclaimer: I wrote it \$\endgroup\$James A Mohler– James A Mohler2013年12月18日 05:24:18 +00:00Commented Dec 18, 2013 at 5:24
1 Answer 1
You could create a language layer and the code only knows the reference. PHPBB has a good example of this in their templating system.
That way the button on a form, for example, is always something like template_var['login']. There is a replace going on based on a language pack file. The user selected preferences define which language pack to use. In the end you're just dumping the language file definition into a template variable that never changes.
This route might be easier to manage than an huge structure that keeps growing as you expand?