在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):kevinresol/turnwing开源软件地址(OpenSource Url):https://github.com/kevinresol/turnwing开源编程语言(OpenSource Language):Haxe 96.8%开源软件介绍(OpenSource Introduction):turnwingHackable localization library for Haxe What?Type safetyTranslations 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 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 mindThere 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. HackableUsers can plug in different implementations at various part of the library. For example, Also, an Usageimport turnwing.*;
import turnwing.provider.*;
import turnwing.template.*;
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 source = new ResourceStringSource(lang -> '$lang.json');
var template = new HaxeTemplate();
var loc = new Manager<MyLocale>(new JsonProvider<MyLocale>(source, template));
loc.get('en').handle(function(o) switch o {
case Success(localizer):
// data prepared, we can now translate something
$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!"
}
} ProvidersJsonProvider
Requires a templating engine to interpolate the parameters.
The interface is defined in Usage: var source = new ResourceStringSource(lang -> '$lang.json');
var template = new HaxeTemplate();
var provider = new JsonProvider<MyLocale>(source, template); To use it, install FluentProvider (JS Only)
Messages in the FTL file should be named the same as the Locale interface functions.
Nested interfaces should be delimited by a dash ( At the moment, the validation logic is incomplete and only performs a very rough check. So, runtime error may occur in a locale function call. This will be improved in the future. Usage: var source = new ResourceStringSource(lang -> '$lang.ftl');
var provider = new FluentProvider<MyLocale>(source); To use it, you have to install the npm package |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论