View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.example.demo;
21  
22  import javax.enterprise.context.SessionScoped;
23  import javax.enterprise.event.Observes;
24  import javax.faces.application.Application;
25  import javax.faces.context.FacesContext;
26  import javax.inject.Named;
27  import java.io.Serializable;
28  import java.util.ArrayList;
29  import java.util.Enumeration;
30  import java.util.List;
31  import java.util.Locale;
32  import java.util.ResourceBundle;
33  
34  import static org.apache.myfaces.tobago.util.ResourceUtils.TOBAGO_RESOURCE_BUNDLE;
35  
36  @Named
37  @SessionScoped
38  public class BundleController implements Serializable {
39  
40    private List<BundleEntry> resources = new ArrayList<>();
41    private List<BundleEntry> messages = new ArrayList<>();
42  
43    public void clear(@Observes LocaleChanged event) {
44      resources.clear();
45      messages.clear();
46    }
47  
48    public List<BundleEntry> getResources() {
49      if (resources.size() == 0) {
50        final FacesContext facesContext = FacesContext.getCurrentInstance();
51        final ResourceBundle bundle =
52            facesContext.getApplication().getResourceBundle(facesContext, TOBAGO_RESOURCE_BUNDLE);
53        final Enumeration<String> keys = bundle.getKeys();
54        while (keys.hasMoreElements()) {
55          final String key = keys.nextElement();
56          resources.add(new BundleEntry(key, bundle.getString(key)));
57        }
58      }
59      return resources;
60    }
61  
62    public List<BundleEntry> getMessages() {
63      if (messages.size() == 0) {
64        final FacesContext facesContext = FacesContext.getCurrentInstance();
65        final Application application = facesContext.getApplication();
66        final String bundleName = application.getMessageBundle();
67        final Locale locale = facesContext.getViewRoot() != null
68            ? facesContext.getViewRoot().getLocale() : application.getDefaultLocale();
69        ResourceBundle bundle = ResourceBundle.getBundle(bundleName, locale);
70        final Enumeration<String> keys = bundle.getKeys();
71        while (keys.hasMoreElements()) {
72          final String key = keys.nextElement();
73          messages.add(new BundleEntry(key, bundle.getString(key)));
74        }
75      }
76      return messages;
77    }
78  
79  }