LocalizedStatFormats.java

  1. /*
  2.  * Licensed to the Hipparchus project 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 Hipparchus project 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.  *      https://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.hipparchus.stat;

  18. import java.util.Locale;
  19. import java.util.MissingResourceException;
  20. import java.util.ResourceBundle;

  21. import org.hipparchus.exception.Localizable;
  22. import org.hipparchus.exception.UTF8Control;

  23. /**
  24.  * Enumeration for localized messages formats used in exceptions messages.
  25.  * <p>
  26.  * The constants in this enumeration represent the available formats as
  27.  * localized strings. These formats are intended to be localized using simple
  28.  * properties files, using the constant name as the key and the property value
  29.  * as the message format. The source English format is provided in the constants
  30.  * themselves to serve both as a reminder for developers to understand the
  31.  * parameters needed by each format, as a basis for translators to create
  32.  * localized properties files, and as a default format if some translation is
  33.  * missing.
  34.  * </p>
  35.  */
  36. public enum LocalizedStatFormats implements Localizable {

  37.     /** TIES_ARE_NOT_ALLOWED. */
  38.     TIES_ARE_NOT_ALLOWED("Ties are not allowed."),

  39.     /** INSUFFICIENT_DATA_FOR_T_STATISTIC. */
  40.     INSUFFICIENT_DATA_FOR_T_STATISTIC("insufficient data for t statistic, needs at least 2, got {0}"),

  41.     /** NOT_ENOUGH_DATA_REGRESSION. */
  42.     NOT_ENOUGH_DATA_REGRESSION("the number of observations is not sufficient to conduct regression"),

  43.     /** INVALID_REGRESSION_OBSERVATION. */
  44.     INVALID_REGRESSION_OBSERVATION("length of regressor array = {0} does not match the number of variables = {1} in the model"),

  45.     /** NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS. */
  46.     NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS("not enough data ({0} rows) for this many predictors ({1} predictors)"),

  47.     /** NOT_SUPPORTED_NAN_STRATEGY. */
  48.     NOT_SUPPORTED_NAN_STRATEGY("NaN strategy {0} not supported"),

  49.     /** NO_REGRESSORS. */
  50.     NO_REGRESSORS("Regression model must include at least one regressor"),

  51.     /** COVARIANCE_MATRIX. */
  52.     COVARIANCE_MATRIX("covariance matrix"),

  53.     /** OUT_OF_BOUNDS_QUANTILE_VALUE. */
  54.     OUT_OF_BOUNDS_QUANTILE_VALUE("out of bounds quantile value: {0}, must be in (0, 100]"),

  55.     /** OUT_OF_BOUNDS_CONFIDENCE_LEVEL. */
  56.     OUT_OF_BOUNDS_CONFIDENCE_LEVEL("out of bounds confidence level {0}, must be between {1} and {2}"),

  57.     /** OUT_OF_BOUND_SIGNIFICANCE_LEVEL. */
  58.     OUT_OF_BOUND_SIGNIFICANCE_LEVEL("out of bounds significance level {0}, must be between {1} and {2}"),

  59.     /** SIGNIFICANCE_LEVEL. */
  60.     SIGNIFICANCE_LEVEL("significance level ({0})"),

  61.     /** TOO_MANY_REGRESSORS. */
  62.     TOO_MANY_REGRESSORS("too many regressors ({0}) specified, only {1} in the model"),

  63.     /** TWO_OR_MORE_CATEGORIES_REQUIRED. */
  64.     TWO_OR_MORE_CATEGORIES_REQUIRED("two or more categories required, got {0}"),

  65.     /** TWO_OR_MORE_VALUES_IN_CATEGORY_REQUIRED. */
  66.     TWO_OR_MORE_VALUES_IN_CATEGORY_REQUIRED("two or more values required in each category, one has {0}"),

  67.     /** ILLEGAL_STATE_PCA. */
  68.     ILLEGAL_STATE_PCA("you must fit the PCA projection before calling {0}");

  69.     /** Source English format. */
  70.     private final String sourceFormat;

  71.     /**
  72.      * Simple constructor.
  73.      *
  74.      * @param sourceFormat source English format to use when no localized
  75.      *        version is available
  76.      */
  77.     LocalizedStatFormats(final String sourceFormat) {
  78.         this.sourceFormat = sourceFormat;
  79.     }

  80.     /** {@inheritDoc} */
  81.     @Override
  82.     public String getSourceString() {
  83.         return sourceFormat;
  84.     }

  85.     /** {@inheritDoc} */
  86.     @Override
  87.     public String getLocalizedString(final Locale locale) {
  88.         try {
  89.             final String path = LocalizedStatFormats.class.getName()
  90.                 .replaceAll("\\.", "/");
  91.             ResourceBundle bundle = ResourceBundle
  92.                 .getBundle("assets/" + path, locale, new UTF8Control());
  93.             if (bundle.getLocale().getLanguage().equals(locale.getLanguage())) {
  94.                 final String translated = bundle.getString(name());
  95.                 if ((translated != null) && (translated.length() > 0) &&
  96.                     (!translated.toLowerCase(locale).contains("missing translation"))) {
  97.                     // the value of the resource is the translated format
  98.                     return translated;
  99.                 }
  100.             }

  101.         } catch (MissingResourceException mre) { // NOPMD
  102.             // do nothing here
  103.         }

  104.         // either the locale is not supported or the resource is unknown
  105.         // don't translate and fall back to using the source format
  106.         return sourceFormat;

  107.     }

  108. }