Localization in React AutoComplete

24 Sep 202612 minutes to read

The Localization library allows you to localize the static text of the noRecordsTemplate and actionFailureTemplate properties based on the React AutoComplete’s current culture. The default text values for the en-US culture are shown below; these property names are also used as the keys passed to L10n.load().

Property Default text (en-US)
noRecordsTemplate No Records Found
actionFailureTemplate The Request Failed

The placeholder text shown in the samples is set directly via the placeholder prop and is not part of the L10n translation object.

Setting the locale

Set the locale property on the React AutoComplete to the desired culture code (for example, fr-BE for French as used in Belgium). Culture codes follow the BCP-47 standard (e.g., fr-BE, de-DE, es-ES); use the appropriate code for the culture you want to apply. The L10n.load() method must provide matching translations for that code.

Loading translations

To load a translation object into the application, use the L10n.load() method of the L10n class. The translation keys for the React AutoComplete must be scoped under the dropdowns namespace so the component can resolve them at runtime. Call L10n.load() before the component renders — in a class component, place it in componentDidMount or at module level; in a functional component, call it inside a useEffect hook.

In the following sample, the fr-BE culture is applied to the React AutoComplete. To demonstrate the noRecordsTemplate, the query uses take(0) so that no records are returned initially; the noRecordsTemplate text appears in French. To see the actionFailureTemplate in French, simulate a failed request by going offline (disconnect the network) or by pointing the DataManager URL to an invalid endpoint — the French failure text appears when the request fails.

[Class Component]

// import L10n class for load function
import { L10n } from '@syncfusion/ej2-base';
// import DataManager related classes
import { DataManager, ODataV4Adaptor, Query } from '@syncfusion/ej2-data';
import { AutoCompleteComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component {
    // bind remotedata to showcase actionFailureTemplate in offline.
    customerData = new DataManager({
        adaptor: new ODataV4Adaptor,
        crossDomain: true,
        url: 'https://services.odata.org/V4/Northwind/Northwind.svc/Customers'
    });
    // maps the appropriate column to fields property
    fields = { value: 'ContactName' };
    // take 0 item to showcase noRecordsTemplate property.
    query = new Query().select(['ContactName', 'CustomerID']).take(0);
    // set locale culture to AutoComplete
    componentDidMount() {
        L10n.load({
            'fr-BE': {
                'dropdowns': {
                    'actionFailureTemplate': "Modèle d'échec d'action",
                    'noRecordsTemplate': "Aucun enregistrement trouvé",
                }
            }
        });
    }
    render() {
        return (
        // specifies the tag for render the AutoComplete component
        <AutoCompleteComponent id="atcelement" fields={this.fields} locale="fr-BE" query={this.query} dataSource={this.customerData} placeholder="Trouver un client"/>);
    }
}
ReactDOM.render(<App />, document.getElementById('sample'));
// import L10n class for load function
import { L10n } from '@syncfusion/ej2-base';
// import DataManager related classes
import { DataManager, ODataV4Adaptor, Query } from '@syncfusion/ej2-data';
import { AutoCompleteComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

export default class App extends React.Component<{}, {}> {
    // bind remotedata to showcase actionFailureTemplate in offline.
    private customerData: DataManager = new DataManager({
      adaptor: new ODataV4Adaptor,
      crossDomain: true,
      url: 'https://services.odata.org/V4/Northwind/Northwind.svc/Customers'
    });

    // maps the appropriate column to fields property
    private fields: object = { value: 'ContactName' };

    // take 0 item to showcase noRecordsTemplate property.
    private query: Query = new Query().select(['ContactName', 'CustomerID']).take(0);

    // set locale culture to AutoComplete
    public componentDidMount() {
        L10n.load({
            'fr-BE': {
                'dropdowns': {
                  'actionFailureTemplate': "Modèle d'échec d'action",
                    'noRecordsTemplate': "Aucun enregistrement trouvé",
                }
            }
        });
    }

    public render() {
        return (
             // specifies the tag for render the AutoComplete component
            <AutoCompleteComponent id="atcelement" fields={this.fields} locale="fr-BE" query={this.query} dataSource={this.customerData} placeholder="Trouver un client"/>
        );
    }
}
ReactDOM.render(<App />, document.getElementById('sample'));

[Functional Component]

// import L10n class for load function
import { L10n } from '@syncfusion/ej2-base';
// import DataManager related classes
import { DataManager, ODataV4Adaptor, Query } from '@syncfusion/ej2-data';
import { AutoCompleteComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
function App() {
    // bind remotedata to showcase actionFailureTemplate in offline.
    const customerData = new DataManager({
        adaptor: new ODataV4Adaptor,
        crossDomain: true,
        url: 'https://services.odata.org/V4/Northwind/Northwind.svc/Customers'
    });
    // maps the appropriate column to fields property
    const fields = { value: 'ContactName' };
    // take 0 item to showcase noRecordsTemplate property.
    const query = new Query().select(['ContactName', 'CustomerID']).take(0);
    // set locale culture to AutoComplete
    React.useEffect(() => {
        L10n.load({
            'fr-BE': {
                'dropdowns': {
                    'actionFailureTemplate': "Modèle d'échec d'action",
                    'noRecordsTemplate': "Aucun enregistrement trouvé",
                }
            }
        });
    }, []);
    return (
    // specifies the tag for render the AutoComplete component
    <AutoCompleteComponent id="atcelement" fields={fields} locale="fr-BE" query={query} dataSource={customerData} placeholder="Trouver un client"/>);
}
ReactDOM.render(<App />, document.getElementById('sample'));
// import L10n class for load function
import { L10n } from '@syncfusion/ej2-base';
// import DataManager related classes
import { DataManager, ODataV4Adaptor, Query } from '@syncfusion/ej2-data';
import { AutoCompleteComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

function App() {
    // bind remotedata to showcase actionFailureTemplate in offline.
    const customerData: DataManager = new DataManager({
      adaptor: new ODataV4Adaptor,
      crossDomain: true,
      url: 'https://services.odata.org/V4/Northwind/Northwind.svc/Customers'
    });

    // maps the appropriate column to fields property
    const fields: object = { value: 'ContactName' };

    // take 0 item to showcase noRecordsTemplate property.
    const query: Query = new Query().select(['ContactName', 'CustomerID']).take(0);

    // set locale culture to AutoComplete
    React.useEffect(() => {
     L10n.load({
            'fr-BE': {
                'dropdowns': {
                  'actionFailureTemplate': "Modèle d'échec d'action",
                    'noRecordsTemplate': "Aucun enregistrement trouvé",
                }
            }
        });
}, []);
        return (
             // specifies the tag for render the AutoComplete component
            <AutoCompleteComponent id="atcelement" fields={fields} locale="fr-BE" query={query} dataSource={customerData} placeholder="Trouver un client"/>
        );
}
ReactDOM.render(<App />, document.getElementById('sample'));

See also