Glossary.Lexeme (Glossary v0.2.0)

View Source

Internal functions for working with glossary lexemes.

This module is not part of the public API. It provides utilities to identify, qualify, and resolve localized expressions based on Ecto-style validation metadata.

Summary

Functions

Builds a lexeme identifier from Ecto validation options.

Looks up a localized expression in the glossary by its fully qualified key.

Prepends a locale prefix to a lexeme to produce a fully qualified glossary key.

Functions

identify(opts)

@spec identify(map()) :: String.t()

Builds a lexeme identifier from Ecto validation options.

The result is used to construct the glossary key. It attempts to match the most specific form: validation.key.kind.type, then validation.key.kind, then validation.key.

Logs a warning and returns "validation.unknown" if :validation is missing.

Examples

identify(%{validation: :length, kind: :min, type: :string})
#=> "validation.length.min.string"

identify(%{validation: :required})
#=> "validation.required"

lookup(key, glossary, bindings, fallback \\ nil)

@spec lookup(String.t(), map(), keyword(), String.t() | nil) :: String.t()

Looks up a localized expression in the glossary by its fully qualified key.

Performs interpolation of placeholders in the form {{key}} using values from bindings. If the key is not found, returns fallback or the key itself, and logs a warning.

Examples

lookup("en.validation.required", glossary, [], nil)
#=> "can't be blank"

lookup("en.validation.foo", glossary, [name: "Alice"], nil)
#=> "Hello, Alice!"

qualify(lexeme, locale)

@spec qualify(String.t(), String.t()) :: String.t()

Prepends a locale prefix to a lexeme to produce a fully qualified glossary key.

Examples

qualify("validation.required", "en")
#=> "en.validation.required"