private static String sanitizeAcceptLanguage()

in locales-utils/src/main/java/com/spotify/i18n/locales/utils/acceptlanguage/AcceptLanguageUtils.java [97:116]


  private static String sanitizeAcceptLanguage(final String acceptLanguage) {
    final String sanitizedValue =
        acceptLanguage
            // Remove all spaces
            .replaceAll(" ", "")
            // A locale like en_SG@calendar=buddhist should be parseable, but the parse method
            // fails if the character "@" is present ... so we replace it by the "-u-" character
            // chain, which then parses nicely.
            .replaceAll("@", "-u-")
            // Replace all underscores with hyphens, to successfully parse badly formatted
            // values
            .replaceAll("_", "-")
            // Remove all locale extensions (as per BCP47), because they cannot be parsed
            .replaceAll("-u-([^,;]*)", "");
    // Negative q values are not authorized ... but we receive them and therefore should handle
    // them. We replace the negative values by zero.
    // https://www.rfc-editor.org/rfc/rfc9110#field.accept-language
    // https://www.rfc-editor.org/rfc/rfc9110#quality.values
    return PATTERN_NEGATIVE_WEIGHTS.matcher(sanitizedValue).replaceAll(";q=0.0");
  }