🌐 lang_file#
Generates the language file of the datapack.
- beet_default(ctx: Context) None[source]#
Main entry point for the lang file plugin. This plugin handles language file generation for the datapack.
- Parameters:
ctx – The beet context.
silent#
utils#
- split_text_content(
- text: str,
- max_words: int = 5,
Split text into (prefix, core, suffix), the core spanning from the first to the last letter.
The core then expands to close the brackets and quotes opened within it. When it holds more than max_words words, the text is not split and comes back whole as the core.
>>> split_text_content(" attacks | ") (' ', 'attacks', ' | ')
- unmatched(text: str, opener: str, closer: str) int[source]#
How many openers of text are left open. A quote opens and closes with the same character, so parity decides for it.
>>> unmatched("((a)", "(", ")"), unmatched("'a'' ", "'", "'") (1, 1)
- absorb_prefix_openers(
- prefix: str,
- core: str,
- suffix: str,
Fold into the core each opener of the prefix left open, with its closer when the suffix still holds it.
- absorb_core_closers(
- core: str,
- suffix: str,
Let each opener of the core left open take its closer from the suffix.
- lang_parts(clean_text: str) tuple[str, str, str][source]#
split_text_content, with the core’s leading and trailing newlines moved out of the translation and its key.>>> lang_parts("\nHello\n\n") ('\n', 'Hello', '\n\n')
- extract_texts(
- content: str,
Every “text” value of content, as (value, start, end, value_quote, key_quote) with key_quote None for a bare key.
>>> extract_texts('{"text":"Hello World"}') [('Hello World', 1, 21, '"', '"')] >>> extract_texts("{text:'Hey'}") [('Hey', 1, 11, "'", None)]
- lang_format(
- text: str,
- ctx: Context | None = None,
Format text into a lang key, returned with its simplified form used for length and alphanumeric checks.
Path separators become underscores, other non-alphanumeric characters go, whitespace and dashes collapse, and the result is lowercased, truncated to 64 characters and prefixed with the project_id.
- Parameters:
ctx – The beet context providing project_id, None for Mem.ctx
>>> lang_format('BOMB PLANTED!', FakeContext(project_id='mgs')) ('mgs.bomb_planted', 'bombplanted')
- resolve_lang_key(base_key: str, value: str) str[source]#
Return a lang key for value, base_key itself when it is free or already holds value.
Otherwise _2, _3, … are tried until a free slot or a slot already holding value is found.
- build_replacement(
- string: str,
- text: str,
- clean_text: str,
- start: int,
- end: int,
- quote: str,
- key_quote: str | None,
- key_for_lang: str,
- prefix: str,
- suffix: str,
Build the replacement fragment, with the start and end positions in string it overwrites.
Without prefix or suffix, only the matched key:value fragment is replaced. Otherwise the enclosing JSON object is wrapped into a list, [prefix_obj?, {translate: key}, “suffix”?], or replaced in place when it cannot be located. Every translate carries the text it replaces as its fallback, shown when no resource pack defines the key.
- Parameters:
text – Raw matched value, as it appears in source with escapes.
clean_text – Decoded version of text (\n -> newline, etc.).
start – Start position of the matched key:value fragment.
quote – Quote character used around the value, single or double.
key_quote – Quote character used around the “text” key, or None.
prefix – Non-alphanumeric prefix stripped from clean_text.
- translated(
- string: str,
- text: str,
- start: int,
- end: int,
- quote: str,
- key_quote: str | None,
- ctx: Context | None,
The replacement turning one “text” value of string into a lang key, registered in
lang, or None for a value not worth one.- Parameters:
text – Raw matched value, as
extract_textsfound it at start to end.ctx – Passed to
lang_format, None for Mem.ctx.
- handle_file(
- content: TextFileBase[str] | None,
- ctx: Context | None = None,
Replace in place every useful {“text”: “…”} component of a file with a lang key.
Strings without alphanumeric characters, too short or holding macros are skipped. A non-alphanumeric prefix or suffix is stripped to derive a stable key, wrapping the enclosing JSON object into a list, so components sharing the same alphanumeric content share their key. Numeric suffixes (_2, _3, …) are the fallback when the object cannot be wrapped.
- Parameters:
ctx – The context containing project information, None for Mem.ctx