I need to provide on the page support for two languages ​​(English, Russian).

As I understand it, I need to create translate.properties, translate_en.properties, translate_ru.properties in the Resource Bundle.

Next, I write files = files and files = files to them.

Now. How do I use this to provide two languages? I need to create a language change button on the site, as I understand it, and take the key value from it (which language to display), and how to link it all together with the markup.

<p:layou> <p:layoutUnit> <h3>files</h3> </p:layoutUnit> </p:layout> 

Something else needs to be registered in the class, I suspect. Help me understand the whole chain of actions, please.

    1 answer 1

    Steps to enable multilingual support in JSF:

    1. Create a session component containing information about the current language and having functions for changing the language.

       @ManagedBean @SessionScoped public class LocaleController { public static Locale LOCALE_RU = new Locale("ru"); public static Locale LOCALE_EN = new Locale("en"); private Locale locale = LOCALE_RU; public Locale getLocale() { return locale; } public String selectLanguage(String selectedLanguage) { locale = convert(selectedLanguage); FacesContext.getCurrentInstance().getViewRoot().setLocale(locale); return FacesContext.getCurrentInstance().getViewRoot().getViewId() + "?faces-redirect=true";//перезагружаем страницу } public static Locale convert(String languageName) { if ("en".equals(languageName)) { return LOCALE_EN; } return LOCALE_RU; } public boolean isRu() { return LOCALE_RU.equals(locale); } } 
    2. Specify language on page: <f:view locale="#{localeController.locale}">

    3. Report supported languages ​​and your language resource files to faces-config.xml . The title and namespaces point to the JSF 2.2 specification version; if you have a different version, the title will be different.

       <faces-config version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"> <application> <locale-config> <default-locale>ru</default-locale> <supported-locale>ru</supported-locale> <supported-locale>en</supported-locale> </locale-config> <resource-bundle> <!--bundles - это имя папки с файлами ресурсов --> <base-name>bundles/translate</base-name> <var>translate</var> </resource-bundle> </application> </faces-config> 
    4. Add language change buttons

       <p:commandLink value="English" action="#{localeController.selectLanguage('en')}" rendered="#{localeController.ru}"/> <p:commandLink value="Русский" action="#{localeController.selectLanguage('ru')}" rendered="#{not localeController.ru}"/> 
    5. Fill files with language resources. files=files are added to translate_ru.properties , files=файлы added to translate_ru.properties

    6. Properly use language resources on the page

       <h3>#{translate.files}</h3> 

      where #{translate refers to a var of faces-config.xml

    • one
      I corrected my answer, there should be FacesContext.getCurrentInstance (). GetViewRoot (). GetViewId (). org.omnifaces.util.Faces is a class from the OmniFaces library ( showcase.omnifaces.org ), which contains a number of utilities that simplify life when developing JSF applications. - bobzer
    • javax.servlet.ServletException: /views/home.xhtml @16,113 rendered="#{LocaleController.ru}": Property 'ru' not found on type com.common.LocaleController Where do we find “ru” in LocaleController or what does it mean? - Saint
    • one
      Added isRu () method. Check in yourself, in the expression {LocaleController.ru} the first character (L) in lower case should be. - bobzer
    • With your permission, the last question is more private: Can't find bundle for base name resources/translate, locale ru As you commented: <!--bundles - это имя папки с файлами ресурсов --> <base-name>bundles/translate</base-name> I have them in the resources - Resource Bundle 'translate' folder, in which all translate.properties. But for some reason, on the specified path, the program does not find them .. - Saint
    • one
      Check where the files are actually in the assembled application. For <base-name> bundles / translate </ base-name>, this should be my.war \ WEB-INF \ classes \ bundles \ translate_ru.properties - bobzer