View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.nabla;
18  
19  import java.util.Locale;
20  import java.util.MissingResourceException;
21  import java.util.ResourceBundle;
22  
23  import org.apache.commons.math3.exception.util.Localizable;
24  
25  /**
26   * Enumeration for localized messages formats used in exceptions messages.
27   * <p>
28   * The constants in this enumeration represent the available
29   * formats as localized strings. These formats are intended to be
30   * localized using simple properties files, using the constant
31   * name as the key and the property value as the message format.
32   * The source English format is provided in the constants themselves
33   * to serve both as a reminder for developers to understand the parameters
34   * needed by each format, as a basis for translators to create
35   * localized properties files, and as a default format if some
36   * translation is missing.
37   * </p>
38   * @version $Id$
39   */
40  public enum NablaMessages implements Localizable {
41  
42      // CHECKSTYLE: stop MultipleVariableDeclarations
43      // CHECKSTYLE: stop JavadocVariable
44  
45      CANNOT_READ_CLASS("class {0} cannot be read ({1})"),
46      CANNOT_INSTANTIATE_ABSTRACT_CLASS("abstract class {0} cannot be instantiated ({1})"),
47      ILLEGAL_ACCESS_TO_CONSTRUCTOR("illegal access to class {0} constructor ({1})"),
48      CANNOT_BUILD_CLASS_FROM_OTHER_CLASS("class {0} cannot be built from an instance of class {1} ({2})"),
49      CANNOT_INSTANTIATE_CLASS_FROM_OTHER_INSTANCE("class {0} instantiation from an instance of class {1} failed ({2})"),
50      INCORRECT_GENERATED_CODE("class {0} code generated from an instance of class {1} is incorrect ({2})"),
51      INTERFACE_NOT_FOUND_WHILE_DIFFERENTIATING("interface {0} not found while differentiating class {1}"),
52      CLASS_DOES_NOT_IMPLEMENT_INTERFACE("the {0} class does not implement the {1} interface"),
53      UNABLE_TO_ANALYZE_METHOD("unable to analyze the {0}.{1} method ({2})"),
54      UNKNOWN_METHOD("unknown method {0}.{1}"),
55      NUMBER_OF_TEMPORARY_VARIABLES_OUT_OF_RANGE("number of temporary variable ({0}) outside of [{1}, {2}] range"),
56      INDEX_OF_LOCAL_VARIABLE_OUT_OF_RANGE("index of size {0} local variable ({1}) outside of [{2}, {3}] range"),
57      UNEXPECTED_INSTRUCTION("unexpected instruction with opcode {0}"),
58      UNABLE_TO_HANDLE_INSTRUCTION("unable to handle instruction with opcode {0}"),
59      CANNOT_GET_VOID_FIELD("unable to get value of void type field {0}"),
60      ILLEGAL_LDC_CONSTANT("illegal LDC constant {0}"),
61      INTERNAL_ERROR("internal error, please fill a bug report at {0}");
62  
63      // CHECKSTYLE: resume JavadocVariable
64      // CHECKSTYLE: resume MultipleVariableDeclarations
65  
66  
67      /** Source English format. */
68      private final String sourceFormat;
69  
70      /** Simple constructor.
71       * @param sourceFormat source English format to use when no
72       * localized version is available
73       */
74      private NablaMessages(final String sourceFormat) {
75          this.sourceFormat = sourceFormat;
76      }
77  
78      /** {@inheritDoc} */
79      public String getSourceString() {
80          return sourceFormat;
81      }
82  
83      /** {@inheritDoc} */
84      public String getLocalizedString(final Locale locale) {
85          try {
86              final String path = NablaMessages.class.getName().replaceAll("\\.", "/");
87              final ResourceBundle bundle =
88                      ResourceBundle.getBundle("assets/" + path, locale);
89              if (bundle.getLocale().getLanguage().equals(locale.getLanguage())) {
90                  // the value of the resource is the translated format
91                  return bundle.getString(toString());
92              }
93  
94          } catch (MissingResourceException mre) {
95              // do nothing here
96          }
97  
98          // either the locale is not supported or the resource is unknown
99          // don't translate and fall back to using the source format
100         return sourceFormat;
101 
102     }
103 
104 }