Java Locale filterTags() Method



Description

The Java Locale filterTags(List<Locale.LanguageRange> priorityList, Collection<String> tags) method returns a list of matching languages tags using the basic filtering mechanism defined in RFC 4647. This is equivalent to filterTags(List, Collection, FilteringMode) when mode is Locale.FilteringMode.AUTOSELECT_FILTERING. This filter operation on the given tags ensures that only unique matching tag(s) are returned with preserved case. In case of duplicate matching tags with the case difference, the first matching tag with preserved case is returned.

Declaration

Following is the declaration for java.util.Locale.filterTags() method

public static List<String> filter​Tags(List<Locale.LanguageRange> priorityList, Collection<String> tags)

Parameters

priorityList − user's Language Priority List in which each language tag is sorted in descending order based on priority or weight.

tags − language tags.

Return Value

This method returns a list of matching language tags sorted in descending order based on priority or weight, or an empty list if nothing matches. The list is modifiable.

Exception

NullPointerException − if priorityList or locales is null

Java Locale filterTagsList<Locale.LanguageRange> priorityList, Collection<String> locales, Locale.FilteringMode mode) Method

Description

The Java Locale filterTagsList<Locale.LanguageRange> priorityList, Collection<String> locales, Locale.FilteringMode mode) method returns a list of matching languages tags using the basic filtering mechanism defined in RFC 4647. This filterTags operation on the given tags ensures that only unique matching tag(s) are returned with preserved case. In case of duplicate matching tags with the case difference, the first matching tag with preserved case is returned.

Declaration

Following is the declaration for java.util.Locale.filterTags() method

public static List<Locale> filterTags​(List<Locale.LanguageRange> priorityList, Collection<Locale> locales)

Parameters

priorityList − user's Language Priority List in which each language tag is sorted in descending order based on priority or weight.

locales − Locale instances used for matching.

mode − filtering mode.

Return Value

This method returns a list of matching language tags sorted in descending order based on priority or weight, or an empty list if nothing matches. The list is modifiable.

Exception

NullPointerException − if priorityList or locales is null

IllegalArgumentException − if one or more extended language ranges are included in the given list when Locale.FilteringMode.REJECT_EXTENDED_RANGES is specified

Filtering Language Tag from a List of Locales Example

The following example shows the usage of Java Locale filterTagsList<Locale.LanguageRange>, Collection<Locale>) method. We're creating a string of comma separated locales and using that string, we've created a locale preference list. Now a list of locale is created and few locales are added. Using filterTags() method, locales are filtered and result is printed.

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class LocaleDemo {
   public static void main(String[] args) {

      String tags = Locale.ENGLISH.toLanguageTag() 
         + ","
         + Locale.FRENCH.toLanguageTag();

      List<Locale.LanguageRange> priorityList = Locale.LanguageRange.parse(tags);
      List<String> localeList = new ArrayList<>();
      localeList.add("en");
      localeList.add("jp");

      List<String> filteredTags = Locale.filterTags(priorityList, localeList);

      System.out.println(filteredTags);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

[en]

Filtering Language Tag from a List of Locales Example

The following example shows the usage of Java Locale filterTagsList<Locale.LanguageRange>, Collection<Locale>) method. We're creating a different string of comma separated locales and using that string, we've created a locale preference list. Now a list of locale is created and few locales are added. Using filterTags() method, locales are filtered and result is printed.

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class LocaleDemo {
   public static void main(String[] args) {

      String tags = Locale.ENGLISH.toLanguageTag() 
         + ","
         + Locale.FRENCH.toLanguageTag();

      List<Locale.LanguageRange> priorityList = Locale.LanguageRange.parse(tags);
      List<String> localeList = new ArrayList<>();
      localeList.add("en");
      localeList.add("en_CA");
      localeList.add("fr");

      List<String> filteredTags = Locale.filterTags(priorityList, localeList);

      System.out.println(filteredTags);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

[en, fr]

Filtering Language Tag from a List of Locales With Filtering Mode Example

The following example shows the usage of Java Locale filterTagsList<Locale.LanguageRange>, Collection<Locale>,Locale.FilteringMode) method. We're creating a string of comma separated locales and using that string, we've created a locale preference list. Now a list of locale is created and few locales are added. Using filterTags() method, locales are filtered and result is printed.

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class LocaleDemo {
   public static void main(String[] args) {

      String tags = Locale.ENGLISH.toLanguageTag() 
         + ","
         + Locale.FRENCH.toLanguageTag();

      List<Locale.LanguageRange> priorityList = Locale.LanguageRange.parse(tags);
      List<String> localeList = new ArrayList<>();
      localeList.add("en");
      localeList.add("jp");

      List<String> filteredTags = Locale.filterTags(priorityList, localeList,Locale.FilteringMode.AUTOSELECT_FILTERING);

      System.out.println(filteredTags);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

[en]
java_util_locale.htm
Advertisements