Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nadako/hxlocalize
Type-safe localization with Haxe (EXPERIMENT)
https://github.com/nadako/hxlocalize
haxe localization macros
Last synced: 29 days ago
JSON representation
Type-safe localization with Haxe (EXPERIMENT)
- Host: GitHub
- URL: https://github.com/nadako/hxlocalize
- Owner: nadako
- Created: 2016-03-30T00:51:09.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2019-05-04T09:56:51.000Z (over 5 years ago)
- Last Synced: 2024-10-25T09:49:58.266Z (3 months ago)
- Topics: haxe, localization, macros
- Language: Haxe
- Size: 11.7 KB
- Stars: 4
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# hxlocalize
[![Build Status](https://travis-ci.org/nadako/hxlocalize.svg?branch=master)](https://travis-ci.org/nadako/hxlocalize)
This is my small experiment with implementing type-safe localization in Haxe using macros.
> The awesome @kevinresol developed a proper library based on this idea, so if you're looking for a complete solution, check out [turnwing](https://github.com/kevinresol/turnwing)!
The idea is pretty simple: you declare all possible messages as empty methods in a child class of a magic base-class, like this:
```haxe
class MyLocale extends Locale {
function person():String;
function message(name:String, count:Int):String;
}
```Then create an instance of that class, passing translations map like this:
```haxe
var locale = new MyLocale(new Catalog([
"person" => "Dan",
"message" => "Hi, {name} ({count})!" // `name` and `count` match argument names
]));
```Then just call defined methods and get translated strings:
```haxe
trace(locale.message(locale.person(), 5)); // Hi, Dan (5)!
```I think there are several benefits with this approach, such as:
* you can't make a typo in your locale key, since it's a real method, known at compile-time
* you get autocompletion and refactoring support for your locale keys
* localized string interpolation arguments are well-defined and typed
* it's easy to validate the translation data files from a macro using method definitions