File Uploader localization
The File Uploader localization support enables the customization of UI texts and translations. It supports pluralization and provides a way to create a tailored user experience by adapting the interface to various languages and preferences.
Let’s take a look at how the locale definition is described.
Locale definition
The locale definition is described as a plain JS object like this:
Let’s break down the properties.
Locale id and social source lang
The first two properties aren’t localized strings themselves:
locale-id
- the Unicode locale identifier string. It’s used for pluralization rules. See MDN Intl documentation for more details.social-source-lang
- the language code for social sources. It’s used to set the language for social sources like Facebook. The social sources aren’t a part of the File Uploader; they are loaded in the iframe, so the localization strings can’t be changed. The only available languages are:'de', 'en', 'es', 'fi', 'fr', 'he', 'it', 'nl', 'pl', 'pt', 'ru', 'tr', 'uk', 'zh-TW', 'zh'
Localization strings
The other properties are localized strings. In the example below, we have:
upload-file
- the string for the upload button with the textUpload file
.file__one
- the string for the singular form of the wordfile
with the valuefile
.file__other
- the string for the plural form of the wordfile
with the valuefiles
.header-uploading
- the string for the header during the uploading process with the valueUploading {{count}} {{plural:file(count)}}
.
Pluralization
Plural forms are described as key__category
, where the key
is the base key,
and the category
is the plural category.
The plural categories could be zero
, one
, few
, many
, and other
.
They are used to select the appropriate variant of the string based on the count of items.
The allowed categories depend on the locale.
You can find the list of plural categories for each locale in
the CLDR Plural Rules documentation.
Some localization strings are parameterized.
The parameters are enclosed in double curly braces {{
and }}
.
For example, {{count}}
is a parameter that will be replaced with the number
of files being uploaded.
The {{plural:file(count)}}
syntax is used to apply plural rules to the word file
.
I.e. string Uploading {{count}} {{plural:file(count)}}
will be rendered
as Uploading 1 file
or Uploading 5 files
depending on the count.
You can find a whole English locale definition on GitHub.
Let’s take a look at localization API.
Method defineLocale
The defineLocale
method is used to define a new locale within the File Uploader.
It accepts the locale definition object or the async function that returns
the locale definition object.
The defineLocale
method has the following signature:
Let’s break down the arguments:
localeName
is the internal locale name. It should be unique and match thelocale-name
option value.locale
is the locale definition object or the async function that returns the locale definition object.
WARNING: The defineLocale
method should be called before the defineComponents
method or before the File Uploader components appear in the DOM.
Option locale-name
The locale-name
option is used to set the locale name for the File Uploader components.
It should match the locale name passed to the defineLocale
method.
The locale-name
option is optional. If it’s not set, the default English locale will be used.
It could be set as an attribute or DOM property on the uc-config
component:
Get YOUR_PUBLIC_KEY
from API keys.
See the File Uploader options for more details.
Option localeDefinitionOverride
The localeDefinitionOverride
option is used to override the specific locale definition.
It should be set as a DOM property on the uc-config
component. HTML attribute is not supported.
Here is the type definition:
Here is how to use it:
So, let’s combine all the information and see how to define a locale.
How to define a locale
By default, the File Uploader uses the English locale. To use a different locale, you must provide the locale definition object to the File Uploader component.
To do this, we have an API method called defineLocale
that accepts the locale
definition object itself or the async function that returns the locale definition object.
We have a set of ready-made locales that you can use out of the box. They are in the locales folder on GitHub.
To use a ready-made locale, you need to import it and pass it using the defineLocale
method.
There are two ways to define a locale: sync and async. Let’s take a look at both of them.
Sync way
This way is useful when you want to use the defined locale immediately. For example, if your website is in Portuguese, you want to use the Portuguese locale in the File Uploader.
The locale-name
attribute is used to set the locale name.
The locale name should match the first argument passed to the defineLocale
method.
Async way
This way is useful when you want to load the locale definition asynchronously. For example, you have a language switcher on your website and want to load the locale definition only when the user changes the language.
The locale-name
attribute is used to set the locale name.
The locale name should match the first argument passed to the defineLocale
method.
Now, we’re going to take a look at how to override the existing locale.
How to update existing localization
To do so, you need to use the localeDefinitionOverride
option described below.
It allows you to override the specific locale definition.
In this example, we override the upload-file
string in the English
locale with the value Choose your documents
.
How to create a new localization
Creating a new localization is as simple as defining an existing locale.
You need to provide your own locale definition object to the defineLocale
method.
As a template, you can use the English locale definition on GitHub.
You are encouraged to create a pull request with your locale definition for the GitHub repository.