Source code for cadrumo.domain.user_profile.labels
"""Operator-facing labels for the user-profile schema, in the active language.The schema TOML declares a ``title`` per section and a ``description`` perfield. Neither is a label. ``description`` is long-form authority prose --often several sentences (``auth.provider`` runs to four) -- and it isdeliberately mixed-language, because the fields whose meaning is fixed bySpanish tax law are written in Spanish (``tax_residence.ccaa``,``renta_taxpayer.sex``, ``provenance.source``). Rendering it as a tablelabel gave the operator a paragraph in a column, in whichever language theschema author happened to use.So the two strings are separated by job, and each has exactly one home:- The schema TOML owns the long-form prose. It stays the authority the wizard's copy resolver reads and the masking heuristic scans.- The locale catalogues own the short operator-facing label, keyed per section and per field.The label is resolved through :func:`~cadrumo.core.i18n.render.tr` with the schemaprose as the ``default``, which makes the fallback truthful rather thandecorative: a field whose key no catalogue carries renders its declareddescription -- true, if verbose -- instead of a raw dotted path or amissing-key marker. The renderer already treats a value equal to its ownkey as a miss, so a scaffold placeholder falls back the same way an absentkey does; a key can therefore be declared before it is translated withoutever showing the operator a placeholder.Keys are derived from the schema rather than declared beside it, so theschema stays the single structural authority: adding a field to the TOMLcreates its key by construction. :func:`profile_schema_locale_keys` is whatthe locale scaffolder reads, which is what makes a newly-declared fieldwithout labels a loud parity failure rather than a silent English row.See Also: :class:`ProfileSchemaDefinition` The schema whose sections and fields these keys are derived from."""from__future__importannotationsfromtypingimportTYPE_CHECKINGfrom...core.i18n.renderimporttrifTYPE_CHECKING:from.schemaimportProfileFieldDefinition,ProfileSchemaDefinition,ProfileSectionDefinition_KEY_ROOT="profile.schema"# The two key families are separated by an explicit ``section``/``field``# infix rather than nested directly under the section key. Without it a# field named ``title`` would build ``profile.schema.<section>.title.label``# while its own section built ``profile.schema.<section>.title`` -- the same# catalogue path as both a mapping and a leaf, which YAML cannot represent.# The infix makes the collision structurally impossible instead of relying# on no one ever naming a field ``title``._SECTION_INFIX="section"_FIELD_INFIX="field"
[docs]defprofile_section_title_key(section_key:str)->str:"""Return the locale key carrying one section's operator-facing title. Args: section_key: Canonical section key, e.g. ``identity``. Returns: The dotted locale key, e.g. ``profile.schema.section.identity.title``. """returnf"{_KEY_ROOT}.{_SECTION_INFIX}.{section_key}.title"
[docs]defprofile_field_label_key(section_key:str,field_key:str)->str:"""Return the locale key carrying one field's operator-facing label. Args: section_key: Canonical section key, e.g. ``identity``. field_key: Canonical field key within that section, e.g. ``tax_id``. Returns: The dotted locale key, e.g. ``profile.schema.field.identity.tax_id.label``. """returnf"{_KEY_ROOT}.{_FIELD_INFIX}.{section_key}.{field_key}.label"
[docs]defprofile_section_title(section:ProfileSectionDefinition,*,locale:str|None=None)->str:"""Return one section's title in ``locale``, falling back to the schema. Args: section: The declared section. locale: Language code; the active output language when ``None``. Returns: The catalogue title, or the schema's declared ``title`` when the catalogue does not carry one. """returntr(profile_section_title_key(section.key),locale=locale,)
[docs]defprofile_field_label(section_key:str,field:ProfileFieldDefinition,*,locale:str|None=None,)->str:"""Return one field's label in ``locale``, falling back to the schema. Args: section_key: Canonical key of the section declaring ``field``. field: The declared field. locale: Language code; the active output language when ``None``. Returns: The catalogue label, or the field's declared ``description`` when the catalogue does not carry one. """returntr(profile_field_label_key(section_key,field.key),locale=locale,)
[docs]defprofile_schema_locale_keys(schema:ProfileSchemaDefinition)->set[str]:"""Return every locale key the schema's sections and fields declare. This is the enrolment surface the locale scaffolder and the parity gate read, so a field added to the schema TOML without a catalogue entry fails parity instead of silently rendering its description. Args: schema: The loaded schema to enumerate. Returns: Every section-title and field-label key declared by ``schema``. """keys:set[str]=set()forsectioninschema.sections:keys.add(profile_section_title_key(section.key))forfieldinsection.fields:keys.add(profile_field_label_key(section.key,field.key))returnkeys