An open source design system that's fully customizable and agent ready
TypeScript
fix(field): localize the required/optional indicator (#4559)
Cindy Zhangfa80d0398e08
diff --git a/.changeset/field-label-required-i18n.md b/.changeset/field-label-required-i18n.mdnew file mode 100644index 0000000000..0dd9229b33--- /dev/null+++ b/.changeset/field-label-required-i18n.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] FieldLabel: localize the "Required"/"Optional" indicator through the i18n runtime instead of hardcoding English, so consumers can translate it via `InternationalizationProvider` (#4508).++@cixzhangdiff --git a/packages/core/locales/en.json b/packages/core/locales/en.jsonindex ac7f9c2610..0a7ed670e2 100644--- a/packages/core/locales/en.json+++ b/packages/core/locales/en.json@@ -759,6 +759,14 @@"defaultMessage": "Code","description": "Screen-reader-only fallback name for a code-snippet scroll container when the code's language is unknown. \"Code\" = source code, not secret code or code of conduct."},+ "@astryx.field.optional": {+ "defaultMessage": "Optional",+ "description": "Visible indicator shown next to a form field's label when the field is optional (isOptional). Mutually exclusive with the required indicator. Very short — appears inline after the label text."+ },+ "@astryx.field.required": {+ "defaultMessage": "Required",+ "description": "Visible indicator shown next to a form field's label when the field must be filled in (isRequired). Mutually exclusive with the optional indicator. Very short — appears inline after the label text."+ },"@astryx.fileInput.clearLabel": {"defaultMessage": "Clear {label}","description": "Screen-reader-only label on the X that removes the selected file from a FileInput. Example: `Clear Attachment`, `Clear Résumé`."diff --git a/packages/core/src/Field/FieldLabel.test.tsx b/packages/core/src/Field/FieldLabel.test.tsxindex e7db702172..5d475a8631 100644--- a/packages/core/src/Field/FieldLabel.test.tsx+++ b/packages/core/src/Field/FieldLabel.test.tsx@@ -12,6 +12,7 @@import {describe, it, expect, vi} from 'vitest';import {render, screen} from '@testing-library/react';import {TestIcon} from '../__tests__/TestIcon';+import {InternationalizationProvider} from '../i18n';import {FieldLabel} from './FieldLabel';describe('FieldLabel', () => {@@ -36,6 +37,30 @@ describe('FieldLabel', () => {expect(screen.getByText(/Required/)).toBeInTheDocument();});+ it('localizes the required indicator via the i18n provider', () => {+ render(+ <InternationalizationProvider+ locale="fr"+ overrides={{fr: {'@astryx.field.required': 'Obligatoire'}}}>+ <FieldLabel label="Nom" inputID="name-input" isRequired />+ </InternationalizationProvider>,+ );+ expect(screen.getByText(/Obligatoire/)).toBeInTheDocument();+ expect(screen.queryByText(/Required/)).not.toBeInTheDocument();+ });++ it('localizes the optional indicator via the i18n provider', () => {+ render(+ <InternationalizationProvider+ locale="fr"+ overrides={{fr: {'@astryx.field.optional': 'Facultatif'}}}>+ <FieldLabel label="Nom" inputID="name-input" isOptional />+ </InternationalizationProvider>,+ );+ expect(screen.getByText(/Facultatif/)).toBeInTheDocument();+ expect(screen.queryByText(/Optional/)).not.toBeInTheDocument();+ });+it('shows Optional when both isOptional and isRequired are true', () => {render(<FieldLabel label="Name" inputID="name-input" isOptional isRequired />,diff --git a/packages/core/src/Field/FieldLabel.tsx b/packages/core/src/Field/FieldLabel.tsxindex a018566df9..90fd5008d1 100644--- a/packages/core/src/Field/FieldLabel.tsx+++ b/packages/core/src/Field/FieldLabel.tsx@@ -1,8 +1,10 @@// Copyright (c) Meta Platforms, Inc. and affiliates.+'use client';+/*** @file FieldLabel.tsx- * @input Uses React, Icon, IconType+ * @input Uses React, Icon, IconType, useTranslator* @output Exports FieldLabel component, FieldLabelProps* @position Core label implementation; used by Field, CheckboxInput, Switch*@@ -10,6 +12,7 @@* - /packages/core/src/Field/Field.doc.mjs (props table, features, implementation notes)* - /packages/core/src/Field/index.ts (exports if types change)* - /packages/cli/templates/blocks/components/Field/ (showcase blocks)+ * - /packages/core/locales/en.json (@astryx.field.required / @astryx.field.optional)*/import type {ReactNode} from 'react';@@ -26,6 +29,7 @@ import {} from '../theme/tokens.stylex';import {Icon, renderIconSlot, type IconType} from '../Icon';import {Tooltip} from '../Tooltip';+import {useTranslator} from '../i18n';import {themeProps} from '../utils/themeProps';const styles = stylex.create({@@ -177,7 +181,12 @@ export function FieldLabel({ref,...rest}: FieldLabelProps) {- const statusText = isOptional ? 'Optional' : isRequired ? 'Required' : null;+ const t = useTranslator();+ const statusText = isOptional+ ? t('@astryx.field.optional')+ : isRequired+ ? t('@astryx.field.required')+ : null;// A group label (e.g. for a radiogroup) must not be a literal `<label>`// element: a `<label>` semantically names a single form control and can't be