/***********************************************************************/
/*                                                                     */
/*      YaltSample :: Demonstrates a simple localization technique     */
/*                    (without using the ExtendScript feature)         */
/*                                                                     */
/*      [Ver: 2.0]    [Author: Marc Autret]     [Modif: 12/11/09]      */
/*      [Lang: EN|FR] [Req: InDesign CS4]       [Creat: 06/27/09]      */
/*                                                                     */
/*      Installation:                                                  */
/*                                                                     */
/*      1) Place the current file into Scripts/Scripts Panel/          */
/*                                                                     */
/*      2) Run InDesign                                                */
/*                                                                     */
/*      3) Exec script from Window > Automatisation > Scripts          */
/*         (double-click on the script name)                           */
/*                                                                     */
/*      Bugs & Feedback : marc{at}indiscripts{dot}com                  */
/*                        www.indiscripts.com                          */
/*                                                                     */
/***********************************************************************/

// 1. Create your own localized strings by setting
//    the lines below (keep the commented format):
//    You can use \uXXXX to escape non-ascii chars

//======================================
// <L10N> :: FRENCH_LOCALE :: GERMAN_LOCALE
//======================================
// Yes :: Oui :: Ja
// I love :: J'adore :: Ich liebe
// This is a translated text :: Ceci est un texte traduit :: Dies ist eine \u00FCbersetzte Text
// </L10N>

// 2. Add the code's block below in your script:

var L10N = L10N || (function()
	{
	var ln = (function()
		{ // <this> : Number
		for(var p in Locale) if(Locale[p] == this) return(p);
		}).call(app.locale);

	var parseL10N = /*str[]*/function(/*str*/locale_, /*[File]*/f)
		{
		var lines = (function()
			{ // <this> : File
			var	__com = '// ', __sep = ' :: ', __beg = '<L10N>', __end = '</L10N>';
			var l,r = [];
			var uEsc = function(){return String.fromCharCode(Number('0x'+arguments[1]));}
			if( this.open('r') )
				{
				var comSize=__com.length;
				while( !this.eof )
					{
					l = this.readln().replace(/\\u([0-9a-f]{4})/gi, uEsc);
					if( l.indexOf(__com) != 0 ) continue;
					if( l.indexOf(__end) >= 0 ) break;
					if( l.indexOf(__sep) < 0  ) continue;
					r.push(l.substr(comSize).split(__sep));
					}
				this.close();
				}
			while( (l=r.shift()) && l[0] != __beg ) {};
			return (l)?[l].concat(r):false;
			}).call(f||File(app.activeScript));

		var r=[];
		if (!lines) return r;
		var line = lines[0];
		var locIndex = (function()
			{
			for (var i=1,sz=line.length ; i<sz ; i++)
				if ( line[i] == locale_ ) return i;
			return 0;
			})();
		if (!locIndex) return r;
		
		while( line=lines.shift() )
			if ( typeof line[locIndex] != 'undefined' )
				r[line[0]] = line[locIndex];
		return r;
		}
	
	var tb = parseL10N(ln);
	__ = function(/*str*/ks){return(tb[ks]||ks);}
	return {locale: ln};
	})();



// 3. Use the syntax __(<string>) anywhere you need
//    to localize your text:

alert( __("This is a translated text") + ":\n\n"
	+ __("I love") + " Indiscripts.com!");