Localizing published websites
Localizing a published website with Brightspot is similar to that for other Java-based web applications. In Brightspot’s MVVM paradigm, you need to—
- Define resource files that contain key-value lookups for the localized labels.
- Place the lookup and output mechanisms in the view model.
- Insert label placeholders in the Styleguide files.
The steps in this section demonstrate localizing the following short form into French.
Step 1: Make a localization resource file
A localization resource file contains the key-value pairs for localized strings. As a best practice, place all your localization files in a single directory. The directory must be under /src/main/resources/.
-
Under
/src/main/resources/i18n/, create a default resource filesite.propertiesand enter the following text:showLineNumbers1first.name = First name2last.name = Last name -
In the same directory, create a resource file
site_fr.propertiesand enter the following text:showLineNumbers1first.name = Prénom2last.name = Nom de famille -
Depending on your development or run-time environment, you may need to escape non-ASCII characters in the properties files. For example, in the previous file,
Prénomis changed toPr\u00e9nomusing Java’s native2ascii utility.
For information about creating a properties file for localization, see Backing a ResourceBundle with Properties Files. In particular, the two letters before .properties in the file name must be a language code that Java recognizes. For a list of two-letter language codes, see List of ISO 639-1 codes.
Step 2: Add localization property to site
Localization is almost always done at the site level; if one page in a site appears in French, all other pages appear in French. As explained in Step 6 below, the view model determines in which locale to display content by reading a site’s locale field. Brightspot’s Site class does not include a locale field, so you need to add it using the Modification class.
-
Under
/src/main/java/content/article/, create a fileSiteModification.javaand enter the following text:showLineNumbers1import com.psddev.cms.db.Site;2import com.psddev.dari.db.Modification;34public class SiteModification extends Modification<Site> {56SiteModification () {};78private Locale locale;910public Locale getLocale() {11return locale;12}1314public void setLocale(Locale locale) {15this.locale = locale;16}1718}
This model adds the locale field to the Site model, making it visible on the Edit Site widget.
For additional information about extending classes such as Site, see Modifications.
Step 3: Create a signup model
Create a simple model that displays fields for first name and last name in a signup form.
-
Under
/src/main/java/content/article/, create a fileSignup.javaand enter the following text:showLineNumbers1import com.psddev.cms.db.Content;23public class Signup extends Content {45private String firstName;67private String lastName;89/* Setters and getters */10}
Step 4: Create a template
Localization typically involves displaying a locale-specific string for each static label on the GUI. Continuing the example, create a Handlebars file that has a placeholder for each label on the form.
-
Under
/styleguide/content/article/, create a fileSignup.hbsand enter the following text:showLineNumbers1<table>2<tr>3<td>{{firstNameLabel}}</td>4<td><input type="text"/></td>5</tr>6<tr>7<td>{{lastNameLabel}}</td>8<td><input type="text"/></td>9</tr>10</table>
Step 5: Create a data file
Create a data file that has a placeholder for each label in the form.
-
Under
/styleguide/content/article/, create a fileSignup.jsonand enter the following text:showLineNumbers1{2"_template": "Signup.hbs",3"firstNameLabel": "{{words(1)}}",4"lastNameLabel": "{{words(1)}}"5}
Step 6: Create a localized view model
The view model includes the logic for retrieving localized strings based on the requested site’s locale.
-
Under
/src/main/java/content/article/, create a fileSignupViewModel.javaand enter the following text:showLineNumbers {9,13,20,26}1import com.psddev.cms.db.Site;2import com.psddev.cms.view.ViewModel;3import com.psddev.cms.view.PageEntryView;4import com.psddev.cms.view.servlet.CurrentSite;5import styleguide.content.article.SignupView;67public class SignupViewModel extends ViewModel<Signup> implements SignupView, PageEntryView {89@CurrentSite10private Site site;1112@Override13public String getFirstNameLabel() {14ResourceBundle labels = getLabels();15String firstNameLabel = labels.getString("first.name");16return firstNameLabel;17}1819@Override20public String getLastNameLabel() {21ResourceBundle labels = getLabels();22String lastNameLabel = labels.getString("last.name");23return lastNameLabel;24}2526private ResourceBundle getLabels() {27Locale locale = site.as(SiteModification.class).getLocale();28ResourceBundle resourceBundle = ResourceBundle.getBundle("i18n.site",locale);29return resourceBundle;30}3132}- 9. Retrieves the requested site into a
Sitevariable. The annotation@CurrentSitereturns null for Brightspot's global site. Therefore, when creating localized sites, ensure editors publish to a site other than global. - 13. Returns the localized string for
first.name. - 20. Returns the localized string for
last.name. - 26. Returns the resource bundle that contains the localized strings.
- 9. Retrieves the requested site into a
Running the view model displays the expected result.
See also: