Lucius
Posts: 220
|
Posted: 02/22/2013, 2:07 PM |
|
Hi,
Recently during development I started do delve more and more into JavaScript jQuery libraries and doing more and more interactive client-side coding.
Today I started to create my own jQuery library for JavaScript that simplifies adding jQuery client-side objects to my project.
Anyway one of the things I encountered is how to best use the localization resource files from CCS to localize my jQuery functions. I have found out an easy solution that uses CCS mechanisms that is already used to translate CCS generated js files. And it all took 5 minutes to set up!
1. CCS uses ClientI18N.php file to parse JS files to enable localisation. Create a copy of this file, and name it like "ClientI18N_Custom.php".
2. Open the new file in CCS or external editor and edit the following REGEX lines, so your custom JS file is allowed to be parsed:
$AllowedFiles = array(
"/^DatePicker\\.js$/" => "content-type: text/javascript; charset=$ClientFileEncoding",
"/^Functions\\.js$/" => "content-type: text/javascript; charset=$ClientFileEncoding",
"/^[\\w\\/]+_events\\.js$/" => "content-type: text/javascript; charset=$ClientFileEncoding"
);
Remember to publish the new php file to the server !
3. In your HTML file link your custom JS file, so it's loaded through ClientI18N_Custom.php:
<script language="JavaScript"
src="/ClientI18N_Custom.php?file=/js/CustomFunctions.js&localeTWS={res:CCS_LocaleID}"
type="text/javascript" charset="utf-8"></script>
4. In your custom functions you can now use the same resource localisations, like you do in HTML:
function foo() {
var translatedString = "{res:my_localized_string}";
alert(translatedString);
}
5. Profit
|