Giter Club home page Giter Club logo

turnwing's Introduction

turnwing

Hackable localization library for Haxe

What?

Type safety

Translations are done with interfaces. You will never mis-spell the translation key anymore.

In many existing localization libraries, the translation function looks like this:

loc.translate('hello', {name: 'World'}); 
loc.translate('orange', {number: 1});

There is one and only one translation function and its type is String->Dynamic->String. That means it takes a String key, a Dynamic parameter object and returns a substituted string. Serveral things can go wrong here: wrong translation key, wrong param name or wrong param data type.

With turnwing, we have typed translators. Each of them is a user-defined function and typed specifically.

loc.hello('World'); // String->String
loc.orange(1); // Int->String

Peace of mind

There is only one place where errors could happen, that is when the localization data is loaded.

This is because data are validated when they are loaded. The data provider does all the heavy lifting to make sure the loaded data includes all the needed translation keys and values. As a result, there is no chance for actual translation calls to fail.

Hackable

Users can plug in different implementations at various part of the library. May it be a XmlProvider that parses XML into localization data, or a ErazorTemplate that uses the erazor templating engine to render the result.

Usage

import turnwing.*;

interface MyLocale {
	function hello(name:String):String;
	function orange(number:Int):String;
	var sub(get, never):SubLocale;
}

interface SubLocale {
	function yo():String;
}

class Main {
	static function main() {
		var provider = /* choose one from turnwing.provider package or implements your own Provider */;
		var template = /* choose one from turnwing.template package or implements your own Template */;
		var loc = new Manager<MyLocale>(provider, template);
		loc.prepare(['en']).handle(function(o) switch o {
			case Success(_):
				// data prepared, we can now translate something
				var localizer = loc.language('en'); 
				$type(localizer); // MyLocale
				trace(localizer.hello('World')); // "Hello, World!"
				trace(localizer.orange(4)); // "There are 4 orange(s)!"
			case Failure(e):
				// something went wrong when fetching the localization data
				trace(e);
		});
	}
}

// and your json data looks like this:
{
	"hello": "Hello, ::name::",
	"orange": "There are ::number:: orange(s)!",
	"sub": {
		"yo": "Yo!"
	}
}

Providers

JsonProvider is a data provider for JSON sources. Its data validation is powered by tink_json, which generates the validation code with macro at compile time according to the type information of the user-defined locale interface.

var reader = new FileReader(function(lang) return './data/$lang.json');
var provider = new JsonProvider<Data<MyLocale>>(reader);

To use it, install tink_json and include it as dependency in your project

Templates

HaxeTemplate is based on the one provided by Haxe's standard library (haxe.Template)

turnwing's People

Contributors

kevinresol avatar

Watchers

James Cloos avatar David Klein avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.