001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.nabla;
018    
019    import java.util.Locale;
020    import java.util.MissingResourceException;
021    import java.util.ResourceBundle;
022    
023    import org.apache.commons.math3.exception.util.Localizable;
024    
025    /**
026     * Enumeration for localized messages formats used in exceptions messages.
027     * <p>
028     * The constants in this enumeration represent the available
029     * formats as localized strings. These formats are intended to be
030     * localized using simple properties files, using the constant
031     * name as the key and the property value as the message format.
032     * The source English format is provided in the constants themselves
033     * to serve both as a reminder for developers to understand the parameters
034     * needed by each format, as a basis for translators to create
035     * localized properties files, and as a default format if some
036     * translation is missing.
037     * </p>
038     * @version $Id$
039     */
040    public enum NablaMessages implements Localizable {
041    
042        // CHECKSTYLE: stop MultipleVariableDeclarations
043        // CHECKSTYLE: stop JavadocVariable
044    
045        CANNOT_READ_CLASS("class {0} cannot be read ({1})"),
046        CANNOT_INSTANTIATE_ABSTRACT_CLASS("abstract class {0} cannot be instantiated ({1})"),
047        ILLEGAL_ACCESS_TO_CONSTRUCTOR("illegal access to class {0} constructor ({1})"),
048        CANNOT_BUILD_CLASS_FROM_OTHER_CLASS("class {0} cannot be built from an instance of class {1} ({2})"),
049        CANNOT_INSTANTIATE_CLASS_FROM_OTHER_INSTANCE("class {0} instantiation from an instance of class {1} failed ({2})"),
050        INCORRECT_GENERATED_CODE("class {0} code generated from an instance of class {1} is incorrect ({2})"),
051        INTERFACE_NOT_FOUND_WHILE_DIFFERENTIATING("interface {0} not found while differentiating class {1}"),
052        CLASS_DOES_NOT_IMPLEMENT_INTERFACE("the {0} class does not implement the {1} interface"),
053        UNABLE_TO_ANALYZE_METHOD("unable to analyze the {0}.{1} method ({2})"),
054        UNKNOWN_METHOD("unknown method {0}.{1}"),
055        NUMBER_OF_TEMPORARY_VARIABLES_OUT_OF_RANGE("number of temporary variable ({0}) outside of [{1}, {2}] range"),
056        INDEX_OF_LOCAL_VARIABLE_OUT_OF_RANGE("index of size {0} local variable ({1}) outside of [{2}, {3}] range"),
057        UNEXPECTED_INSTRUCTION("unexpected instruction with opcode {0}"),
058        UNABLE_TO_HANDLE_INSTRUCTION("unable to handle instruction with opcode {0}"),
059        CANNOT_GET_VOID_FIELD("unable to get value of void type field {0}"),
060        ILLEGAL_LDC_CONSTANT("illegal LDC constant {0}"),
061        INTERNAL_ERROR("internal error, please fill a bug report at {0}");
062    
063        // CHECKSTYLE: resume JavadocVariable
064        // CHECKSTYLE: resume MultipleVariableDeclarations
065    
066    
067        /** Source English format. */
068        private final String sourceFormat;
069    
070        /** Simple constructor.
071         * @param sourceFormat source English format to use when no
072         * localized version is available
073         */
074        private NablaMessages(final String sourceFormat) {
075            this.sourceFormat = sourceFormat;
076        }
077    
078        /** {@inheritDoc} */
079        public String getSourceString() {
080            return sourceFormat;
081        }
082    
083        /** {@inheritDoc} */
084        public String getLocalizedString(final Locale locale) {
085            try {
086                final String path = NablaMessages.class.getName().replaceAll("\\.", "/");
087                final ResourceBundle bundle =
088                        ResourceBundle.getBundle("assets/" + path, locale);
089                if (bundle.getLocale().getLanguage().equals(locale.getLanguage())) {
090                    // the value of the resource is the translated format
091                    return bundle.getString(toString());
092                }
093    
094            } catch (MissingResourceException mre) {
095                // do nothing here
096            }
097    
098            // either the locale is not supported or the resource is unknown
099            // don't translate and fall back to using the source format
100            return sourceFormat;
101    
102        }
103    
104    }