localize.zig
karlseguin/localize.zig
MIT
ICU Message Parser and Renderer
5 5 0 0
4
localization, zig, zig-library, zig-package
build.zig.zon
build.zig
View on Github
karlseguin/localize.zig
ICU Message Parser and Renderer
ICU Message Format for Zig
Very basic support for parsing and rendering ICU message formats.
build.zig.zon
:zig fetch --save git+https://github.com/karlseguin/localize.zig#master
build.zig
, add the localize
module as a dependency you your program:const localize = b.dependency("localize", .{
.target = target,
.optimize = optimize,
});
// the executable from your call to b.addExecutable(...)
exe.root_module.addImport("localize", localize.module("localize"));
First, create a Resource
:
const locales: []const []const u8 = &.{"en-US", "fr-FR"}
var resource = try localize.Resource.init(allocator, locales);
defer resource.deinit();
This will likely be a long-lived object. The locales can be freed after init
returns (init
clones the values).
Next, create a parser for each locale and add messages:
var parser = try resource.parser("en-US", .{});
defer parser.deinit();
// loop over entries in a file, or something
// localize.zig currently doesn't "read" files
try parser.add("string_len_min", \\ must be at least {min}
\\ {min, plural,
\\ =1 {character}
\\ other {characters}
\\ }
\\ long
);
Once all messages have been loaded, you can use resource.write
to write a localized message:
try resource.write(writer, "en-US", "string_len_min", .{.min = 6});
The write
method is thread-safe.
In cases where you'll be generating multiple message for a single locale, you can first get a Locale
and then use its thread-safe write:</
// you very likely have logic in your code that makes it so that
// this could never return null
var locale = resource.getLocale("en-US") orelse unreachable;
locale.write("string_len_min", .{.min = 6});
Currently, this only supports: