Documentation
¶
Overview ¶
Package messageformat provides the main MessageFormat 2.0 API
Package messageformat provides the main MessageFormat 2.0 API ¶
Package messageformat provides functional options for MessageFormat configuration
Index ¶
- Constants
- Variables
- type BidiIsolation
- type DateTimePart
- type Direction
- type FallbackPart
- type FormatOption
- type FormatOptions
- type LiteralPart
- type LocaleMatcher
- type MarkupPart
- type MessageFormat
- func (mf *MessageFormat) BidiIsolation() bool
- func (mf *MessageFormat) Dir() string
- func (mf *MessageFormat) Format(values map[string]any, options ...any) (string, error)
- func (mf *MessageFormat) FormatToParts(values map[string]any, options ...any) ([]messagevalue.MessagePart, error)
- func (mf *MessageFormat) ResolvedOptions() ResolvedMessageFormatOptions
- type MessageFormatOptions
- type MessageFunction
- type MessageFunctionContext
- type NumberPart
- type Option
- func WithBidiIsolation(strategy BidiIsolation) Option
- func WithBidiIsolationString(strategy string) Option
- func WithDir(direction Direction) Option
- func WithDirString(direction string) Option
- func WithFunction(name string, fn functions.MessageFunction) Option
- func WithFunctions(funcs map[string]functions.MessageFunction) Option
- func WithLocaleMatcher(matcher LocaleMatcher) Option
- func WithLocaleMatcherString(matcher string) Option
- func WithLogger(logger *slog.Logger) Option
- type Part
- type ResolvedMessageFormatOptions
- type StringPart
- type UnknownPart
Constants ¶
Re-export constants from bidi package for API compatibility
Variables ¶
var ( ParseMessage = datamodel.ParseMessage StringifyMessage = datamodel.StringifyMessage Validate = datamodel.ValidateMessage Visit = datamodel.Visit )
Data model operations - matches TypeScript exports
var ( IsExpression = datamodel.IsExpression IsFunctionRef = datamodel.IsFunctionRef IsLiteral = datamodel.IsLiteral IsMarkup = datamodel.IsMarkup IsMessage = datamodel.IsMessage IsPatternMessage = datamodel.IsPatternMessage IsSelectMessage = datamodel.IsSelectMessage IsVariableRef = datamodel.IsVariableRef IsCatchallKey = datamodel.IsCatchallKey )
Type guards - matches TypeScript exports
var DefaultFunctions = functions.DefaultFunctions
DefaultFunctions provides access to built-in functions
var DraftFunctions = functions.DraftFunctions
DraftFunctions provides access to draft functions (beta)
Functions ¶
This section is empty.
Types ¶
type BidiIsolation ¶ added in v0.2.0
type BidiIsolation string
BidiIsolation represents the bidi isolation strategy
const ( BidiDefault BidiIsolation = "default" BidiNone BidiIsolation = "none" )
type DateTimePart ¶ added in v0.3.6
type DateTimePart = messagevalue.DateTimePart
MessageValue types for parts formatting
type Direction ¶ added in v0.2.0
Direction represents text direction Use the Direction type from bidi package as the authoritative definition
type FallbackPart ¶ added in v0.3.6
type FallbackPart = messagevalue.FallbackPart
MessageValue types for parts formatting
type FormatOption ¶
type FormatOption func(*FormatOptions)
FormatOption represents a functional option for Format methods
func WithErrorHandler ¶
func WithErrorHandler(handler func(error)) FormatOption
WithErrorHandler sets an error handler for Format methods TypeScript original code: format(msgParams?: Record<string, unknown>, onError?: (error: Error) => void): string
type FormatOptions ¶
type FormatOptions struct {
OnError func(error)
}
FormatOptions represents options for Format and FormatToParts methods
type LiteralPart ¶ added in v0.3.6
type LiteralPart = messagevalue.TextPart
MessageValue types for parts formatting
type LocaleMatcher ¶ added in v0.2.0
type LocaleMatcher string
LocaleMatcher represents locale matching strategy
const ( LocaleBestFit LocaleMatcher = "best fit" LocaleLookup LocaleMatcher = "lookup" )
type MarkupPart ¶ added in v0.3.6
type MarkupPart = messagevalue.MarkupPart
MessageValue types for parts formatting
type MessageFormat ¶
type MessageFormat struct {
// contains filtered or unexported fields
}
MessageFormat represents a compiled MessageFormat 2.0 message TypeScript original code:
export class MessageFormat<T extends string = never, P extends string = T> {
readonly #bidiIsolation: boolean;
readonly #dir: 'ltr' | 'rtl' | 'auto';
readonly #localeMatcher: 'best fit' | 'lookup';
readonly #locales: Intl.Locale[];
readonly #message: Message;
readonly #functions: Record<string, MessageFunction<T | DefaultFunctionTypes, P | DefaultFunctionTypes>>;
constructor(locales, source, options) { ... }
format(msgParams, onError) { ... }
formatToParts(msgParams, onError) { ... }
}
func MustNew ¶ added in v0.3.0
func MustNew( locales any, source any, options ...any, ) *MessageFormat
MustNew creates a new MessageFormat and panics if there's an error This is a convenience function for cases where you're certain the input is valid
func New ¶
func New( locales any, source any, options ...any, ) (*MessageFormat, error)
New creates a new MessageFormat from locales, source, and options Supports both traditional options struct and functional options pattern TypeScript original code: constructor(
locales: string | string[] | undefined,
source: string | Message,
options?: MessageFormatOptions<T, P>
) {
this.#bidiIsolation = options?.bidiIsolation !== 'none';
this.#localeMatcher = options?.localeMatcher ?? 'best fit';
this.#locales = Array.isArray(locales) ? locales.map(lc => new Intl.Locale(lc)) : locales ? [new Intl.Locale(locales)] : [];
this.#dir = options?.dir ?? getLocaleDir(this.#locales[0]);
this.#message = typeof source === 'string' ? parseMessage(source) : source;
validate(this.#message);
this.#functions = options?.functions ? Object.assign(Object.create(null), DefaultFunctions, options.functions) : DefaultFunctions;
}
func (*MessageFormat) BidiIsolation ¶ added in v0.2.0
func (mf *MessageFormat) BidiIsolation() bool
BidiIsolation returns whether bidi isolation is enabled
func (*MessageFormat) Dir ¶ added in v0.2.0
func (mf *MessageFormat) Dir() string
Dir returns the message's base direction
func (*MessageFormat) Format ¶
Format formats the message with the given values and optional error handler Supports both traditional onError callback and functional options pattern
func (*MessageFormat) FormatToParts ¶
func (mf *MessageFormat) FormatToParts( values map[string]any, options ...any, ) ([]messagevalue.MessagePart, error)
FormatToParts formats the message and returns detailed parts Supports both traditional onError callback and functional options pattern
func (*MessageFormat) ResolvedOptions ¶ added in v0.3.0
func (mf *MessageFormat) ResolvedOptions() ResolvedMessageFormatOptions
ResolvedOptions returns the resolved options for this MessageFormat instance This method is required by the TC39 Intl.MessageFormat proposal https://github.com/tc39/proposal-intl-messageformat#constructor-options-and-resolvedoptions
type MessageFormatOptions ¶
type MessageFormatOptions struct {
// The bidi isolation strategy for message formatting.
// "default" isolates all expression placeholders except when both message and placeholder are LTR.
// "none" applies no isolation at all.
BidiIsolation BidiIsolation `json:"bidiIsolation,omitempty"`
// Explicitly set the message's base direction.
// If not set, the direction is detected from the primary locale.
Dir Direction `json:"dir,omitempty"`
// Locale matching algorithm for multiple locales.
LocaleMatcher LocaleMatcher `json:"localeMatcher,omitempty"`
// Custom functions to make available during message resolution.
// Extends the default functions.
Functions map[string]functions.MessageFunction `json:"functions,omitempty"`
// Logger for this MessageFormat instance. If nil, uses global logger.
Logger *slog.Logger `json:"-"`
}
MessageFormatOptions represents options for creating a MessageFormat TypeScript original code: export interface MessageFormatOptions<
T extends string = never,
P extends string = T
> {
bidiIsolation?: 'default' | 'none';
dir?: 'ltr' | 'rtl' | 'auto';
localeMatcher?: 'best fit' | 'lookup';
functions?: Record<string, MessageFunction<T, P>>;
}
func NewOptions ¶ added in v0.4.18
func NewOptions(opts *MessageFormatOptions) *MessageFormatOptions
NewOptions creates a new MessageFormatOptions with defaults
type MessageFunction ¶ added in v0.3.6
type MessageFunction = functions.MessageFunction
Function types - exported for custom function implementations
type MessageFunctionContext ¶ added in v0.3.6
type MessageFunctionContext = functions.MessageFunctionContext
Function types - exported for custom function implementations
type NumberPart ¶ added in v0.3.6
type NumberPart = messagevalue.NumberPart
MessageValue types for parts formatting
type Option ¶
type Option func(*MessageFormatOptions)
Option represents a functional option for MessageFormat constructor TypeScript original code: MessageFormatOptions interface
func WithBidiIsolation ¶
func WithBidiIsolation(strategy BidiIsolation) Option
WithBidiIsolation sets the bidi isolation strategy TypeScript original code: bidiIsolation?: 'default' | 'none';
func WithBidiIsolationString ¶ added in v0.2.0
WithBidiIsolationString sets the bidi isolation strategy from string (for backward compatibility)
func WithDir ¶
WithDir sets the message's base direction TypeScript original code: dir?: 'ltr' | 'rtl' | 'auto';
func WithDirString ¶ added in v0.2.0
WithDirString sets the message's base direction from string (for backward compatibility)
func WithFunction ¶
func WithFunction(name string, fn functions.MessageFunction) Option
WithFunction adds a single custom function TypeScript original code: functions?: Record<string, MessageFunction<T, P>>;
func WithFunctions ¶
func WithFunctions(funcs map[string]functions.MessageFunction) Option
WithFunctions adds multiple custom functions TypeScript original code: functions?: Record<string, MessageFunction<T, P>>;
func WithLocaleMatcher ¶
func WithLocaleMatcher(matcher LocaleMatcher) Option
WithLocaleMatcher sets the locale matching algorithm TypeScript original code: localeMatcher?: 'best fit' | 'lookup';
func WithLocaleMatcherString ¶ added in v0.2.0
WithLocaleMatcherString sets the locale matching algorithm from string (for backward compatibility)
func WithLogger ¶ added in v0.1.2
WithLogger sets a custom logger for this MessageFormat instance
type Part ¶ added in v0.3.6
type Part = messagevalue.MessagePart
MessageValue types for parts formatting
type ResolvedMessageFormatOptions ¶ added in v0.3.0
type ResolvedMessageFormatOptions struct {
BidiIsolation BidiIsolation `json:"bidiIsolation"`
Dir Direction `json:"dir"`
Functions map[string]functions.MessageFunction `json:"functions"`
LocaleMatcher LocaleMatcher `json:"localeMatcher"`
}
ResolvedMessageFormatOptions represents the resolved options for a MessageFormat instance Based on TC39 Intl.MessageFormat proposal https://github.com/tc39/proposal-intl-messageformat#constructor-options-and-resolvedoptions
type StringPart ¶ added in v0.3.6
type StringPart = messagevalue.StringPart
MessageValue types for parts formatting
type UnknownPart ¶ added in v0.3.6
type UnknownPart = messagevalue.UnknownPart
MessageValue types for parts formatting
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
advanced
command
|
|
|
basic
command
|
|
|
custom-functions
command
|
|
|
pluralization
command
|
|
|
internal
|
|
|
cst
Package cst provides expression parsing for CST TypeScript original code: cst/expression.ts module
|
Package cst provides expression parsing for CST TypeScript original code: cst/expression.ts module |
|
resolve
Package resolve provides expression resolution for MessageFormat 2.0 TypeScript original code: format-context.ts module
|
Package resolve provides expression resolution for MessageFormat 2.0 TypeScript original code: format-context.ts module |
|
selector
Package selector provides pattern selection for MessageFormat 2.0 TypeScript original code: select-pattern.ts module
|
Package selector provides pattern selection for MessageFormat 2.0 TypeScript original code: select-pattern.ts module |
|
pkg
|
|
|
bidi
Package bidi provides bidirectional text direction utilities for MessageFormat 2.0 TypeScript original code: dir-utils.ts module
|
Package bidi provides bidirectional text direction utilities for MessageFormat 2.0 TypeScript original code: dir-utils.ts module |
|
datamodel
Package datamodel provides CST to data model conversion TypeScript original code: data-model/from-cst.ts module
|
Package datamodel provides CST to data model conversion TypeScript original code: data-model/from-cst.ts module |
|
errors
Package errors provides error types for MessageFormat 2.0 implementation Following TypeScript errors.ts module with Go best practices
|
Package errors provides error types for MessageFormat 2.0 implementation Following TypeScript errors.ts module with Go best practices |
|
functions
Package functions provides MessageFormat 2.0 function implementations TypeScript original code: functions/ module
|
Package functions provides MessageFormat 2.0 function implementations TypeScript original code: functions/ module |
|
logger
Package logger provides simple structured logging for MessageFormat 2.0
|
Package logger provides simple structured logging for MessageFormat 2.0 |
|
messagevalue
Package messagevalue provides message value interfaces and implementations for MessageFormat 2.0 TypeScript original code: message-value.ts module
|
Package messagevalue provides message value interfaces and implementations for MessageFormat 2.0 TypeScript original code: message-value.ts module |
|
parts
Package parts provides formatted parts types for MessageFormat 2.0 TypeScript original code: formatted-parts.ts module
|
Package parts provides formatted parts types for MessageFormat 2.0 TypeScript original code: formatted-parts.ts module |
|
Package v1 provides MessageFormat v1 (ICU MessageFormat) implementation for Go
|
Package v1 provides MessageFormat v1 (ICU MessageFormat) implementation for Go |
|
examples/basic
command
Package main demonstrates basic MessageFormat usage.
|
Package main demonstrates basic MessageFormat usage. |
|
examples/ecommerce
command
Package main demonstrates real-world e-commerce MessageFormat usage.
|
Package main demonstrates real-world e-commerce MessageFormat usage. |
|
examples/multilingual
command
Package main demonstrates multilingual MessageFormat usage with different locales.
|
Package main demonstrates multilingual MessageFormat usage with different locales. |
|
examples/performance
command
Package main demonstrates MessageFormat performance optimization techniques.
|
Package main demonstrates MessageFormat performance optimization techniques. |