core/text/i18n
i18n
Types
4Error
Error :: enum int {
// General return values.
None = 0,
Empty_Translation_Catalog = 1,
Duplicate_Key = 2,
// Couldn't find, open or read file.
File_Error = 3,
// File too short.
Premature_EOF = 4,
// GNU Gettext *.MO file errors.
MO_File_Invalid_Signature = 5,
MO_File_Unsupported_Version = 6,
MO_File_Invalid = 7,
MO_File_Incorrect_Plural_Count = 8,
// Qt Linguist *.TS file errors.
TS_File_Parse_Error = 9,
TS_File_Expected_Context = 10,
TS_File_Expected_Context_Name = 11,
TS_File_Expected_Source = 12,
TS_File_Expected_Translation = 13,
TS_File_Expected_NumerusForm = 14,
Bad_Str = 15,
Bad_Id = 16,
}SourceParse_Options
Parse_Options :: struct {
merge_sections: bool,
}SourceSection
Section :: map[string][]stringSourceThe main data structure. This can be generated from various different file formats, as long as we have a parser for them.
Translation
Translation :: struct {
k_v: map[string]Section,
intern: strings.Intern,
pluralize: proc(number: int) -> (int),
number: int,
}SourceConstants
2DEFAULT_PARSE_OPTIONS
DEFAULT_PARSE_OPTIONS :: Parse_Options = Parse_Options{
merge_sections = false,
}SourceMAX_PLURALS
MAX_PLURALS :: min(max(#config(ODIN_i18N_MAX_PLURAL_FORMS, 10), 1), 255)SourceAllow between 1 and 255 plural forms. Default: 10.
Variables
2ACTIVE
ACTIVE :: ^TranslationSourceCurrently active catalog.
TS_XML_Options
TS_XML_Options :: xml.Options = xml.Options{
flags = {
.Input_May_Be_Modified,
.Must_Have_Prolog,
.Must_Have_DocType,
.Ignore_Unsupported,
.Unbox_CDATA,
.Decode_SGML_Entities,
},
expected_doctype = "TS",
}SourceProcedures
11destroy
destroy :: proc(catalog: ^Translation, allocator: mem.Allocator = context.allocator)Source- destroy(), to clean up the currently active catalog catalog i18n.ACTIVE
- destroy(catalog), to clean up a specific catalog.
get_by_section
get_by_section :: proc(section: string, key: string, catalog: ^Translation) -> (value: string)SourceReturns the first translation string for the passed key in a specific section or context.
It is also aliases with `get()`.
Two ways to use it:
- get(section, key), which defaults to the `i18n.ACTIVE` catalogue, or
- get(section, key, catalog) to grab text from a specific loaded catalogue
Inputs:
- section: the catalogue section (sometimes also called 'context') in which to look up the translation
- key: the string to translate
- catalog: the catalogue to use for the translation (defaults to i18n.ACTIVE)
Returns: the translated string, or the original `key` if no translation was found.get_by_section_with_quantity
get_by_section_with_quantity :: proc(section: string, key: string, quantity: int, catalog: ^Translation) -> (value: string)SourceReturns the translation string for the passed key in a specific plural form (if present in the catalogue)
in a specific section or context.
It is also aliases with `get_n()`.
Two ways to use it:
- get(section, key, quantity), which returns the appropriate plural from the active catalogue, or
- get(section, key, quantity, catalog) to grab text from a specific loaded catalogue
Inputs:
- section: the catalogue section (sometime also called 'context') from which to lookup the translation
- key: the string to translate
- qantity: the quantity of item to be used to select the correct plural form
- catalog: the catalogue to use for the translation (defaults to i18n.ACTIVE)
Returns: the translated string, or the original `key` if no translation was foundget_by_slot_by_section
get_by_slot_by_section :: proc(section: string, key: string, slot: int, catalog: ^Translation) -> (value: string)SourceTwo ways to use:
- get_by_slot(key, slot), which returns the requested plural from the active catalog, or
- get_by_slot(key, slot, catalog) to grab text from a specific one.
If a file format parser doesn't (yet) support plural slots, each of the slots will point at the same string.
Inputs:
- section: the catalogue section (sometime also called 'context') from which to lookup the translation
- key: the string to translate.
- slot: the translation slot to choose (slots refer to plural forms specific for each language and their meaning changes from catalogue to catalogue).
- catalog: the catalogue to use for the translation (defaults to i18n.ACTIVE)
Returns: the translated string or the original `key` if no translation was found.get_by_slot_single_section
get_by_slot_single_section :: proc(key: string, slot: int, catalog: ^Translation) -> (value: string)SourceTwo ways to use:
- get_by_slot(key, slot), which returns the requested plural from the active catalogue, or
- get_by_slot(key, slot, catalog) to grab text from a specific loaded catalogue.
If a file format parser doesn't (yet) support plural slots, each of the slots will point at the same string.
- section: the catalogue section (sometime also called 'context') from which to lookup the translation
Inputs:
- key: the string to translate.
- slot: the translation slot to choose (slots refer to plural forms specific for each language and their meaning changes from catalogue to catalogue).
- catalog: the catalogue to use for the translation (defaults to i18n.ACTIVE)
Returns: the translated string, or the original `key` if no translation was found.get_single_section
get_single_section :: proc(key: string, catalog: ^Translation) -> (value: string)SourceReturns the first translation string for the passed key.
It is also aliased with `get()`.
Two ways to use it:
- get(key), which defaults to the `i18n.ACTIVE` catalogue, or
- get(key, catalog) to grab text from a specific loaded catalogue
Inputs:
- key: the string to translate
- catalog: the catalogue to use for the translation (defaults to i18n.ACTIVE)
Returns: the translated string, or the original `key` if no translation was found.get_single_section_with_quantity
get_single_section_with_quantity :: proc(key: string, quantity: int, catalog: ^Translation) -> (value: string)SourceReturns the translation string for the passed key in a specific plural form (if present in the catalogue).
It is also aliased with `get_n()`.
Two ways to use it:
- get_n(key, quantity), which returns the appropriate plural from the active catalogue, or
- get_n(key, quantity, catalog) to grab text from a specific loaded catalogue
Inputs:
- key: the string to translate
- quantity: the quantity of item to be used to select the correct plural form
- catalog: the catalogue to use for the translation (defaults to i18n.ACTIVE)
Returns: the translated string, or the original `key` if no translation was found.parse_mo_file
parse_mo_file :: proc(filename: string, options = DEFAULT_PARSE_OPTIONS, pluralizer: proc() -> (int), allocator: mem.Allocator = context.allocator) -> (translation: ^Translation, err: Error)Sourceparse_mo_from_bytes
parse_mo_from_bytes :: proc(data: []u8, options = DEFAULT_PARSE_OPTIONS, pluralizer: proc() -> (int), allocator: mem.Allocator = context.allocator) -> (translation: ^Translation, err: Error)Sourceparse_qt_linguist_file
parse_qt_linguist_file :: proc(filename: string, options = DEFAULT_PARSE_OPTIONS, pluralizer: proc() -> (int), allocator: mem.Allocator = context.allocator) -> (translation: ^Translation, err: Error)Sourceparse_qt_linguist_from_bytes
parse_qt_linguist_from_bytes :: proc(data: []u8, options = DEFAULT_PARSE_OPTIONS, pluralizer: proc() -> (int), allocator: mem.Allocator = context.allocator) -> (translation: ^Translation, err: Error)SourceProcedure Groups
5get
get :: proc{get_single_section, get_by_section}Sourceget_by_slot
get_by_slot :: proc{get_by_slot_single_section, get_by_slot_by_section}Sourceget_n
get_n :: proc{get_single_section_with_quantity, get_by_section_with_quantity}Sourceparse_mo
parse_mo :: proc{parse_mo_file, parse_mo_from_bytes}Sourceparse_qt
parse_qt :: proc{parse_qt_linguist_file, parse_qt_linguist_from_bytes}Source