Babylscript
FeatureOverview  
Short overview/mini-tutorial of the main features of Babylscript.
Updated Aug 15, 2012

Overview

The Babylscript programming language is a modification of the JavaScript language. It extends JavaScript to support multiple languages.

Language Modes

In Babylscript, there are different language modes. When Babylscript is configured to be in a certain language mode, keywords and function names will be in that language. Babylscript initially defaults to the English language mode. In the English language mode, Babylscript behaves like normal JavaScript.

// Babylscript is the same as normal JavaScript 
// in the default English language mode
//
alert('hello world');

To switch language modes, you type three hyphens, then the code for the language mode that you want, and then another three minus signs. For example, to switch into the French language mode, you would use ---fr---. When in a different language mode, Babylscript's grammar remains the same as normal JavaScript, but the keywords and names will be translated to a different language.

---fr---
fonction fn()
{
   // The French language mode uses "fonction" 
   // instead of "function" and "alerter" instead
   // of "alert"
   alerter(«bonjour!»);
}

Same Object, Different APIs

When in a different language mode, objects will change their APIs to match the current language. For example, in the English language mode, an object will have English method names. But when in a French language mode, those same objects will expose French method names instead.

// In English, numbers are converted to strings using
// the toString() method, but in French, those same
// numbers use the enChaîne() method to convert themselves
// into strings

// English code
//
var num = 5;
alert(num.toString());

// French code
//
---fr---
alerter(num.enChaîne());

This allows you to easily mix code written in different languages in your programs. Objects written in one language can be used by code written in another language. Those objects work just as well in Chinese as they do in English.

Names

One of the notable features of Babylscript is that all programmers can make use of the multilingual features of the language. The multilingual features aren't just for the designers of the language. Not only are the Babylscript libraries available in multiple languages, but programmers can write their own libraries that have different APIs depending on the language mode. The underlying multilingual model of the language of Babylscript is accessible by the programmer.

In Babylscript, structures such as objects, properties, and functions can have multiple names. They all have a default name which is a universal name that can always be used to refer to something. They can also have alternate translated names, which are the translations of that name for other languages. These alternate names are described using a translation mapping: programmers specify how a name in one language should be translated to a default name.

Default Names

When you give a name to an object, that name can be used to refer to that object in all languages. The same holds for names given to properties of an object.

// This object is called obj and has a property called val
//
var obj = new Object();
obj.val = 3;

// Even in Russian, you can refer to the object with its
// original default name of "obj" and access its property
// using "val". Here in Russian mode, we use the Russian 
// alert() function предупредить() to display the object's 
// val property.
//
---ru---
предупредить(obj.val);

Translated Names

You can specify alternate names for the properties of an object. These alternate names can be used in other languages to refer to those properties. You do this by specifying a translated name. A translated name specifies that in a certain other language, a different name can be used to refer to a property. The syntax for specifying a translated name is similar to the syntax for specifying properties in JavaScript

// This object is called obj and has a property called val
//
var obj = new Object();
obj.val = 3;

// Here, we specify an alternate French name for the 
// val property
//
obj['fr':'valeur'] = 'val';

// From now on, when we're in the French language mode,
// we can use this translated name to access the property
//
---fr---
alerter(obj.valeur);

Both Default Names and Translated Names are Valid

Regardless of which language mode you are in, you can access the property of an object by using its original name or by using its translated name. Each property can thus have two names. This is useful for dealing with code evolution. Consider the situation where you need to use an object where a translation isn't available in your desired language. You can still use the object using its original default names for properties. Even if translations are later added to the objects, your code will still be valid.

// This object has a method with a default name of go()
//
var obj = new Object();
obj.go = function() { alert('go was called'); };

// In French, you can use aller() to call the go() method
//
obj['fr':'aller'] = 'go';

---fr---

// In the French language mode, aller() will be mapped to 
// the go() method
//
obj.aller();

// In the French language mode, you can also use the default 
// name go() to call the method
//
obj.go();

---ro---

// No Romanian translated have been specified for the object,
// so default names must be used instead. If translations 
// are later provided for the object, this code will 
// still be valid.
//
obj.go();

Global Names

In JavaScript, global variables can be access as properties of a "global object." When in global scope, this global object can be accessed using this or often by the alias window. This allows you to provide alternate names for global objects.

// This global variable is called length.
//
var length = 5;

// Here, we provide an alternate translated name
// for this global variable for Bengali
//
this['bn':'ব্যাপ্তি'] = 'length';

// In Bengali mode, we then access this global 
// variable using its Bengali name and display it
// on the screen
//
---bn---
সতর্ক(ব্যাপ্তি);

Local variables, function parameters, and closures cannot be translated. They are only accessible by their original names. In practice, this is not a problem because although different functions may be written in different languages, it is uncommon to use different languages within the same function.

Inheritance

Having to specify translations for every single object can be cumbersome. Fortunately, translations can be inherited. So you can specify the translations on an object's prototype and the translated names will be available for all instances of that object. Translations are even valid through long prototype chains.

// Here, we make a constructor for Website objects that hold a
// link to the website.
//
function Website(link)
{
   this.link = link;
}

// We want all website objects to be accessible in Japanese,
// so we add a Japanese translated name to the prototype
//
Website.prototype['ja':'リンク'] = 'link';

// Now, when we can create a Website object, the translations 
// for its properties are inherited through the prototype chain. 
//
var web = new Website('http://www.babylscript.com');

// In Japanese, we can use the translated name for the 
// link property
//
---ja---
アラート(web.リンク);

Iterating over Properties

The translated names are also properly handled when iterating over the properties of an object. When translations for a property exist, the translated name will be used instead of the default name. (Note: In some cases, iterating over properties might not behave as expected. For example, array indices might not be translated, using unusual types of variables to iterate over properties might not be supported, or there might be multiple translated names that map to the same default name.)

// Create an object with properties for different compass directions
// in Chinese 
//
---zh---
obj = { 北:0, 东:1, 南:2, 西:3 };

// Supply English names for these properties
//
obj['en':'north'] = '北';
obj['en':'east'] = '东';
obj['en':'south'] = '南';
obj['en':'west'] = '西';

// When iterating over the properties in English mode, the 
// English names will be provided
//
---en---
for (name in obj) 
{
   alert(name);
}

Numbers

Different cultures have different ways of expressing numbers. Babylscript supports many of these number formats in the compiler.

// In English, decimal marks are written using a period
//
a = 1.1;

// In German, a comma is used for the decimal mark
//
---de---
b = 1,2;

// In Spanish, different countries use either a period or
// a comma as the decimal mark. Babylscript supports both
// formats
//
---es---
c = 1.3 + 1,4;

// In Hindi, English numbers are supported as well as 
// Hindi digits.
//
---hi---
d = 1.5 + १.६;

You have to be careful when writing code using a language where commas are used as decimal marks. With this sort of code, it is sometimes unclear whether a comma refers to a decimal mark or as a separator between list items and function parameters. To be safe, you should put spaces around commas or use semi-colons for separators instead of commas.

---fr---

// In French, commas are used as a decimal mark. This
// can lead to confusion in some cases.
// 

// Is this a list with the value 1.2 or a list with the
// elements 1 and 2?
//
a = [1,2];

// Is this a list with three values: 1, empty, and 3?
// Or is it a list with two values: 1 and 3?
// Or is it invalid code?
//
b = [1, ,3];

// The compiler assumes that commas placed beside a number
// are decimal marks. To make things clearer for users, 
// programmers can use a semi-colon to separate numbers
// instead of a comma. 
//
a = [1;2];
b = [1; ;3];
c = nouveau Tableau(1;2;3);

// Or if they want to use commas as a list separator, they
// should leave spaces around the commas
//
a = [1 , 2];
b = [1 , , 3];
c = nouveau Tableau(1 , 2 , 3);

// Of course, if you are writing code in a language that
// doesn't use commas as decimal marks, then you don't 
// have to worry about these issues.
//
---en---
a = [1,2];
b = [1,,3];
c = new Array(1,2,3);

The current Babylscript to JavaScript translator does not extend JavaScript with internationalization support. As such, the standard libraries do not have special support for non-English numbers or dates. When parsing user input or displaying numbers on the screen, they will use an English format. When Babylscript is used for scripting Java code though, preliminary support for non-English number formats has been implemented.