Skip to content

i18n #

V i18n

A lightweight V port of nicksnyder's go-i18n package, providing simple message localization, bundle loading and translation helpers for V applications.

Installing via v install nepinhum.i18n

Manual Messages

import i18n

mut bundle := i18n.new_bundle('en')!
bundle.add_messages('en', [
    i18n.Message{
        id: 'Cats'
        one: 'I have {{.PluralCount}} cat'
        other: 'I have {{.PluralCount}} cats'
    },
])!

localizer := i18n.new_localizer(bundle, ['en-US,en;q=0.9'])!
text := localizer.localize(i18n.LocalizeConfig{
    message_id: 'Cats'
    plural_count: i18n.plural_count_int(2)
})!

assert text == 'I have 2 cats'

TOML Messages

hello = 'Hello'

[cart]
other = '{{.Name}} has {{.PluralCount}} items'

[Cats]
one = 'I have {{.PluralCount}} cat'
other = 'I have {{.PluralCount}} cats'
mut bundle := i18n.new_bundle('en')!
bundle.load_message_file('active.en.toml')!

localizer := i18n.new_localizer(bundle, ['en'])!
text := localizer.localize(i18n.LocalizeConfig{
    message_id: 'hello'
})!

The language tag is derived from the file name. For example, active.en.toml loads en and locales/pt-BR.toml loads pt-BR.

Supported Template Subset

Templates support only dot lookups with string data:

{{.Name}}
{{.PluralCount}}

Custom delimiters can be set per message with left_delim / right_delim or go-i18n style leftDelim / rightDelim in TOML.

fn new_bundle #

fn new_bundle(default_language string) !Bundle

fn new_localizer #

fn new_localizer(bundle Bundle, languages []string) !Localizer

fn parse_message_file_bytes #

fn parse_message_file_bytes(data []u8, path string) !MessageFile

fn plural_count_int #

fn plural_count_int(value i64) PluralCount

fn plural_count_string #

fn plural_count_string(value string) !PluralCount

fn plural_form_for_language #

fn plural_form_for_language(tag LanguageTag, count PluralCount) !PluralForm

fn PluralForm.from #

fn PluralForm.from[W](input W) !PluralForm

enum PluralForm #

enum PluralForm {
	invalid
	zero
	one
	two
	few
	many
	other
}

struct Bundle #

struct Bundle {
	default_tag LanguageTag
mut:
	tags      []LanguageTag
	templates map[string]MessageTemplate
}

fn (Bundle) default_language #

fn (bundle Bundle) default_language() LanguageTag

fn (Bundle) language_tags #

fn (bundle Bundle) language_tags() []LanguageTag

fn (Bundle) add_messages #

fn (mut bundle Bundle) add_messages(language string, messages []Message) !

fn (Bundle) load_message_file #

fn (mut bundle Bundle) load_message_file(path string) !MessageFile

fn (Bundle) parse_message_file_bytes #

fn (mut bundle Bundle) parse_message_file_bytes(data []u8, path string) !MessageFile

struct LanguageTag #

struct LanguageTag {
	parts []string
}

fn (LanguageTag) str #

fn (tag LanguageTag) str() string

struct LocalizeConfig #

struct LocalizeConfig {
pub:
	message_id      string
	default_message Message
	template_data   map[string]string
	plural_count    ?PluralCount
}

struct Localizer #

struct Localizer {
	bundle      Bundle
	preferences []LanguageTag
}

fn (Localizer) localize #

fn (localizer Localizer) localize(config LocalizeConfig) !string

struct Message #

struct Message {
pub:
	id          string
	hash        string
	description string
	left_delim  string
	right_delim string
	zero        string
	one         string
	two         string
	few         string
	many        string
	other       string
}

struct MessageFile #

struct MessageFile {
pub:
	path     string
	tag      LanguageTag
	format   string
	messages []Message
}

struct PluralCount #

struct PluralCount {
	raw          string
	i            i64
	has_fraction bool
}

fn (PluralCount) str #

fn (count PluralCount) str() string