示例#1
0
  /**
   * Gets localization bundle.
   *
   * @param bundleNames name of bundle. If {@code null} all bundles are included.
   * @param locale optional locale. If {@code null} HTTP headers are queried for acceptable language
   * @param sorted optional flag to partially sort result bundle items by value. Default is {@code
   *     true}.
   * @return the bundle
   */
  @GET
  @Produces({MediaType.APPLICATION_JSON})
  public SmartGwtResponse<Item> getBundle(
      @QueryParam(LocalizationResourceApi.ITEM_BUNDLENAME) Set<BundleName> bundleNames,
      @DefaultValue("") @QueryParam(LocalizationResourceApi.GETBUNDLE_LOCALE_PARAM) String locale,
      @DefaultValue("true") @QueryParam(LocalizationResourceApi.GETBUNDLE_SORTED_PARAM)
          boolean sorted) {

    if (bundleNames == null || bundleNames.isEmpty()) {
      bundleNames = EnumSet.allOf(BundleName.class);
    }
    Locale localeObj;
    if (locale == null || locale.isEmpty()) {
      List<Locale> acceptableLanguages = httpHeaders.getAcceptableLanguages();
      localeObj = acceptableLanguages.isEmpty() ? Locale.ENGLISH : acceptableLanguages.get(0);
    } else {
      localeObj = new Locale(locale);
    }

    Control control = ResourceBundle.Control.getControl(ResourceBundle.Control.FORMAT_PROPERTIES);
    ArrayList<Item> result = new ArrayList<Item>();
    for (BundleName bundleName : bundleNames) {
      try {
        if (!BundleName.PROPERTIES.equals(bundleName.getFormat())) {
          continue;
        }
        result.addAll(readBundle(bundleName, localeObj, control, sorted));
      } catch (MissingResourceException ex) {
        LOG.log(Level.WARNING, bundleNames.toString(), ex);
        throw RestException.plainNotFound(
            LocalizationResourceApi.ITEM_BUNDLENAME, bundleName.toString());
      }
    }
    return new SmartGwtResponse<Item>(result);
  }
示例#2
0
 private ArrayList<Item> readBundle(
     BundleName bundleName, Locale localeObj, Control control, boolean sorted) {
   // to read properties file in UTF-8 use PropertyResourceBundle(Reader)
   ResourceBundle rb = ResourceBundle.getBundle(bundleName.toString(), localeObj, control);
   ArrayList<Item> result = new ArrayList<Item>();
   for (String key : rb.keySet()) {
     result.add(new Item(key, rb.getString(key), bundleName.toString()));
   }
   if (sorted) {
     Collections.sort(result, new LocalizedItemComparator(localeObj));
   }
   return result;
 }