Application-focused internationalization and localization toolkit for MoonBit
moon add zlhahaha/moonlocaleimport {
"zlhahaha/moonlocale",
}fn main {
let en = @moonlocale.Locale::parse("en").unwrap()
let catalog = @moonlocale.MessageCatalog::compile(en, [
@moonlocale.message_resource(
"inbox",
"{count, plural, one {Hello {name}, one message} other {Hello {name}, # messages}}",
),
]).unwrap()
let bundle = @moonlocale.MessageBundle::new(en, [catalog]).unwrap()
let text = bundle.format(
@moonlocale.Locale::parse("en-GB").unwrap(),
"inbox",
[
@moonlocale.string_argument("name", "Mina"),
@moonlocale.int_argument("count", 2500),
],
).unwrap()
println(text) // Hello Mina, 2,500 messages
}moon run examples/basicWelcome, {name}
{role, select, admin {Administrator} other {Member}}
{count, plural, =0 {Empty} one {# item} other {# items}}
{rank, selectordinal, one {#st} two {#nd} few {#rd} other {#th}}
{count, plural, offset:1 =0 {Nobody came} one {{name} came} other {{name} and # others came}}let audit = @moonlocale.audit_catalogs(reference, translations)
if !audit.is_complete() {
for issue in audit.issues() {
println(issue.message())
}
}
let preview = reference.pseudo_localized(
@moonlocale.Locale::parse("en-XA").unwrap(),
)moon fmt
moon check --deny-warn
moon build
moon test --deny-warn
moon run examples/basicpub(all) enum BundleFormatError {
MissingMessage(String, Array[String])
MessageFormattingFailed(String, String, MessageFormatError)
} derive(Eq, Debug)pub struct CatalogAudit {
reference_locale : Locale
checked_catalogs : Int
issues : Array[CatalogAuditIssue]
} derive(Eq, Debug)pub(all) enum CatalogCompileError {
EmptyMessageKey(Int)
DuplicateMessageKey(String)
InvalidMessage(String, MessageParseError)
} derive(Eq, Debug)fn DateTimeFormatter::format_datetime(self : DateTimeFormatter, date : CivilDate, time : CivilTime) -> Stringfn DateTimeFormatter::with_date_style(self : DateTimeFormatter, style : DateStyle) -> DateTimeFormatterfn DateTimeFormatter::with_hour_cycle(self : DateTimeFormatter, cycle : HourCycle) -> DateTimeFormatterpub struct DecimalFormatter {
locale : Locale
grouping : Bool
minimum_fraction_digits : Int
maximum_fraction_digits : Int
sign_display : SignDisplay
} derive(Eq, Debug)fn DecimalFormatter::format_percent(self : DecimalFormatter, value : Decimal, spacing : PercentSpacing) -> Stringfn DecimalFormatter::with_maximum_fraction_digits(self : DecimalFormatter, digits : Int) -> DecimalFormatterfn DecimalFormatter::with_minimum_fraction_digits(self : DecimalFormatter, digits : Int) -> DecimalFormatterfn DecimalFormatter::with_sign_display(self : DecimalFormatter, display : SignDisplay) -> DecimalFormatterpub(all) enum MeasurementUnit {
Meter
Kilometer
Centimeter
Millimeter
SquareMeter
Hectare
Liter
Milliliter
Gram
Kilogram
MeterPerSecond
KilometerPerHour
BitPerSecond
KilobitPerSecond
MegabitPerSecond
Celsius
Fahrenheit
Byte
Kilobyte
Megabyte
Gigabyte
Terabyte
Kibibyte
Mebibyte
Gibibyte
Tebibyte
Millisecond
Second
Minute
Hour
Day
} derive(Eq, Debug)pub struct MessageBundle {
default_locale : Locale
catalogs : Array[MessageCatalog]
} derive(Eq, Debug)fn MessageBundle::format(self : MessageBundle, requested : Locale, key : String, arguments : Array[MessageArgument]) -> Result[String, BundleFormatError]fn MessageBundle::new(default_locale : Locale, catalogs : Array[MessageCatalog]) -> Result[MessageBundle, BundleBuildError]fn MessageBundle::resolve(self : MessageBundle, requested : Locale, key : String) -> Result[ResolvedMessage, BundleFormatError]fn MessageCatalog::compile(locale : Locale, resources : Array[MessageResource]) -> Result[MessageCatalog, CatalogCompileError]fn MessageCatalog::pseudo_localized(self : MessageCatalog, locale : Locale, options? : PseudoOptions) -> MessageCatalogpub(all) enum MessageNode {
Text(String)
Argument(String)
Select(String, Array[MessageCase])
Plural(String, Int, PluralKind, Array[MessageCase])
} derive(Eq, Debug)fn MessageTemplate::format(self : MessageTemplate, locale : Locale, arguments : Array[MessageArgument]) -> Result[String, MessageFormatError]fn MessageTemplate::pseudo_localized(self : MessageTemplate, options? : PseudoOptions) -> MessageTemplatepub struct NumberFormatter {
locale : Locale
grouping : Bool
minimum_integer_digits : Int
sign_display : SignDisplay
} derive(Eq, Debug)fn NumberFormatter::with_minimum_integer_digits(self : NumberFormatter, digits : Int) -> NumberFormatterfn NumberFormatter::with_sign_display(self : NumberFormatter, display : SignDisplay) -> NumberFormatterpub struct RelativeTimeFormatter {
locale : Locale
numeric : RelativeNumeric
width : ListWidth
} derive(Eq, Debug)fn RelativeTimeFormatter::format(self : RelativeTimeFormatter, value : Int, unit : RelativeUnit) -> Stringfn RelativeTimeFormatter::with_numeric(self : RelativeTimeFormatter, numeric : RelativeNumeric) -> RelativeTimeFormatterfn RelativeTimeFormatter::with_width(self : RelativeTimeFormatter, width : ListWidth) -> RelativeTimeFormatterpub struct ResolvedMessage {
requested : Locale
resolved : Locale
key : String
template : MessageTemplate
} derive(Eq, Debug)pub struct UnitFormatter {
locale : Locale
width : UnitWidth
number : DecimalFormatter
} derive(Eq, Debug)fn UnitFormatter::with_fraction_digits(self : UnitFormatter, minimum : Int, maximum : Int) -> UnitFormatterfn currency_fraction_digits(code : String) -> Intfn format_byte_size(bytes : Int, locale : Locale, style? : ByteSizeStyle, fraction_digits? : Int) -> Stringfn format_currency(locale : Locale, value : Decimal, code : String, display : CurrencyDisplay) -> Stringfn format_message(locale : Locale, source : String, arguments : Array[MessageArgument]) -> Result[String, String]Application-focused internationalization and localization toolkit for MoonBit