language.ajax

Since: DataTables 3.0

Load language information from remote locations.

Description

All of the language options DataTables provides can be stored in a file on the server, which DataTables will look up if this parameter is passed. The file must be a valid JSON file, and the object it contains has the same properties as the language object in the initialiser object (a subset of those properties is also valid).

Note that you can use a remote language file as well as local language options, and the local options will take priority.

There are a wide range of translations readily available on this site, in the internationalisation plug-ins.

Note that when this parameter is set, DataTables' initialisation will be asynchronous due to the Ajax data load. That is to say that the table will not be drawn until the Ajax request has completed. As such, any actions that require the table to have completed its initialisation should be placed into the initComplete callback.

Types

string

When used as a string, this parameter will designate a URL that DataTables should use to load a language definition file.

DataTable.AjaxOptions

As an object, this parameter will cause DataTables to make an Ajax request to get the language data, but it provides the ability to control the parameters of that Ajax request - e.g. adding request headers, data or controlling the HTTP method. Please see DataTable.AjaxOptions for the options available for this object, but note that you must supply a url parameter so it knows where to make the Ajax request to!

Furthermore, do not provide a success option. DataTables makes use of this parameter for its own callback.

ajax( settings, callback )

As a function, getting the language data is up to you. You can use a custom Ajax call, web-sockets, localStorage or anything else. The key is to make sure that you call the callback function, passing in the language object once it has been obtained. An empty object can be used if needed, in which case the default language strings would be used.

Parameters:

Examples

Load language information from a remote file:

new DataTable('#myTable', {
	language: {
		ajax: '/dataTables/i18n/de_DE.lang'
	}
});

Remote file and local string override:

new DataTable('#myTable', {
	language: {
		search: 'Buscar en la tabla:',
		ajax: '/dataTables/i18n/es_ES.lang'
	}
});

Ajax headers - sending a CSRF token:

new DataTable('#myTable', {
	language: {
		ajax: {
			url: '//cdn.datatables.net/plug-ins/2.3.7/i18n/fr-FR.json',
			headers: {
				csrf: 'token-value'
			}
		}
	}
});

Custom function:

new DataTable('#myTable', {
	language: {
		ajax: function (settings, callback) {
			// Get language tokens that have previously been loaded
			let lang = JSON.parse(localStorage.getItem('dt-language'));

			callback(lang);
		}
	}
});

Related

The following options are directly related and may also be useful in your application development.