table of contents
play

Table of Contents Berner Fachhochschule-Technik und Informatik - PowerPoint PPT Presentation

Table of Contents Berner Fachhochschule-Technik und Informatik Internationalization - I18n Motivations Advanced Web Technologies I18n in Java Change Language 5) JSF The View Part I18n in JSF Events handling and Navigation Dr. E.


  1. Table of Contents Berner Fachhochschule-Technik und Informatik Internationalization - I18n � Motivations Advanced Web Technologies I18n in Java Change Language 5) JSF The View Part I18n in JSF Events handling and Navigation � Dr. E. Benoist Navigation Events Handling Fall Semester 09-10 Advanced Web Technologies 5) JSF The View Part Advanced Web Technologies 5) JSF The View Part 1 2 Internationalization ? I18n in Java ◮ Multilingual web applications ◮ A Locale object contains i18n configurations • Work of programmers should be used anywhere in the world public Locale(String language) • Translation should not require any informatics knowledge public Locale(String lang, String country) ◮ Structure public Locale(String lang, String ctry, String variant) • Web application without any text, ◮ Resource bundles: • Data Base designed to handle multilingual texts, • Static texts are stored in resource bundles. • Provide facilities for storage and retrieval of all locale-specific information, independently from the application logic ◮ Language • Allow to support multiple locales in a single application • Automatically recognized from the browser, • Allow to extend internationalization easily • Comparison between the site and the browser, • The user can also change the desired language. ◮ The Java Resource bundles classes are: ◮ Priority • ResourceBundle contains locale-specific objects. • browser identification (lowest) • ListResourceBundle abstract subclass of ResourceBundle • Locale in the session • PropertyResourceBundle is a concrete subclass of • Change using an event (higest) ResourceBundle (property files). Advanced Web Technologies 5) JSF The View Part Advanced Web Technologies 5) JSF The View Part Internationalization - I18n: Motivations Internationalization - I18n: I18n in Java 3 4

  2. Resource Bundle (Example) Resource Bundle ◮ Retreve localized information public class MyResourceBundle extends ResourceBundle { To retrieve a localized value for a given key, you should use private String keys = ”Msg1 Msg2 Msg3”; one the methods public Object handleGetObject(String key) { • getObject, if (key.equals(”Msg1”) return ”Hello world!”; • getString or if (key.equals(”Msg2”) return ”Hello i18n!”; • getStringArray ...; from the class ResourceBundle: return null; ◮ public Object getObject(String key) } • first tries to obtain the value using handleGetObject. public Enumeration getKeys() { • If not successful, it calls the getObject method of the parent return new StringTokenizer(keys); resource bundle, assuming it is not null. } • The other two methods are convenience methods that casts } the object returned. Advanced Web Technologies 5) JSF The View Part Advanced Web Technologies 5) JSF The View Part Internationalization - I18n: I18n in Java Internationalization - I18n: Change Language 5 6 Property Resource Bundles Language selection in HTTP ◮ A Property Resource Bundle is a collection of text elements stored ◮ Browser sends its preference in the HTTP Header in a property file. • Which languages are supported • A property file is a text file containing properties. Therein: • Which formats are supported • A property is specified as ”key = value” or ”key : value” • . . . • Line beginning with ”!” or ”#” are comments • ” \ ” is used to indicate line continuation GET http: //cms.hta − bi.bfh.ch/typo3/index.php HTTP/1.1 Host: cms.hta − bi.bfh.ch # File name: I Classes.properties ... ApplicationTitle=Classes in dept. I Accept − Language: fr, fr − ch;q=0.83, en;q=0.66, en − us;q=0.50, \ DisplayButtonText=Display EndButtonText=Exit de;q=0.33, de − ch;q=0.16 I1=I1a, I1b, I1c, \ Accept − Encoding: gzip, deflate, compress;q=0.9 I1p, I1q, I1r Accept − Charset: ISO − 8859 − 1, utf − 8;q=0.66, ∗ ;q=0.66 I2=I2a, I2b, I2c, \ Keep − Alive: 300 I2p, I2q, I2r I3=I3SE, I3TM, I3WI, I3p, I3q Proxy − Connection: keep − alive I4=I4t, I4v, I4w Advanced Web Technologies 5) JSF The View Part Advanced Web Technologies 5) JSF The View Part Internationalization - I18n: Change Language Internationalization - I18n: I18n in JSF 7 8

  3. I18n in JSF Declare locales in faces-config.xml ◮ Declare the supported Locales < faces − config > • In the faces-config.xml file < application > ◮ Create for each supported language a Properity file < locale − config > • project/src/ch/bfh/toto/Toto.properites < default − locale > en < /default − locale > ◮ Replace any output string in the JSP files with a < supported − locale > fr < /supported − locale > message < supported − locale > de < /supported − locale > < supported − locale > es < /supported − locale > < h:outputText value=”# { bundle.title } ”/ > < /locale − config > ◮ Give the possiblity for the user to change language < /application > ... Advanced Web Technologies 5) JSF The View Part Advanced Web Technologies 5) JSF The View Part Internationalization - I18n: I18n in JSF Internationalization - I18n: I18n in JSF 9 10 Create a property file Change Expressions in the JSP files ◮ Create files in your src arborescence • Example project/src/ch/bfh/jsf/Resources.properties ◮ Load the bundle, according to the Locale • project/src/ch/bfh/jsf/Resources_fr.properties < f:loadBundle basename=”carstore.bundles.Resources” # var=”bundle”/ > # This file is used to store localized expressions ◮ Write a message # Autor: E.Benoist # < h:outputText styleClass=”maintitle” title=Hello world value=”# { bundle.chooseLocale } ” / > message=Try to find out what I think congratulation=Congratulation Advanced Web Technologies 5) JSF The View Part Advanced Web Technologies 5) JSF The View Part Internationalization - I18n: I18n in JSF Internationalization - I18n: I18n in JSF 11 12

  4. Change the Locale Return a Localized message ◮ Reacting to an event • Generate an ActionEvent in a form (works also with a button): < d:map id=”worldMap” current=”NAmericas” immediate=”true” action=”storeFront” ◮ Messages can be returned by a bean actionListener=”# { carstore.chooseLocaleFromMap } ” > • Manged beans are used for this purpose • Or • They should be internationalized < h:commandLink id=”NAmerica” action=”storeFront” ◮ Idea actionListener=”# { carstore.chooseLocaleFromLink } ” > • Write all messages in a MessageBundle (with a property file) • In the Bean • Load this class in your program • Answer to getXXX with a localized message. public void chooseLocaleFromMap(ActionEvent actionEvent) { AreaSelectedEvent event = (AreaSelectedEvent) actionEvent; String current = event.getMapComponent().getCurrent(); FacesContext context = FacesContext.getCurrentInstance(); context.getViewRoot().setLocale((Locale) locales.get(current)); resetMaps(); } Advanced Web Technologies 5) JSF The View Part Advanced Web Technologies 5) JSF The View Part Internationalization - I18n: I18n in JSF Internationalization - I18n: I18n in JSF 13 14 Example of Localized Message Navigation Rules ◮ In the constructor of the bean ◮ Design the way to surf from one page to the next • Form and link contain action attribute FacesContext context = FacesContext.getCurrentInstance(); ResourceBundle data = null; < h:commandLink id=”SAmerica” action=”storeFront” > Enumeration keys = null; < h:outputText value=”# { bundle.spanish } ” / > < /h:commandLink > components = new HashMap(); • This link should correspond to an entry in the // load the labels faces-config.xml resources = < navigation − rule > ResourceBundle.getBundle(CarStore.CARSTORE PREFIX + < from − view − id > /chooseLocale.jsp < /from − view − id > ”.bundles.Resources”, < navigation − case > context.getViewRoot().getLocale()); < from − outcome > storeFront < /from − outcome > < to − view − id > /storeFront.jsp < /to − view − id > ◮ When a message is requested < /navigation − case > < /navigation − rule > optionLabel = resources.getString(optionKey); Advanced Web Technologies 5) JSF The View Part Advanced Web Technologies 5) JSF The View Part Internationalization - I18n: I18n in JSF Events handling and Navigation: Navigation 15 16

Recommend


More recommend