Locale toLanguageTag() Method in Java with Examples
Last Updated :
01 May, 2019
The toLanguageTag() method of Locale class in Java is used to return a well-formed IETF BCP 47 language tag representing this locale object. Now there can be few complications when a language, country or variant does not satisfy the above-mentioned tag which is handled well by this method:
- If the mentioned language does not satisfy the tag, then it will be emitted as Undetermined or “und”.
- If the mentioned country does not satisfy the tag, it will be omitted.
- If the same occurs with the Variant, then each sub-segment is emitted as a subtag.
Syntax:
public String toLanguageTag()
Parameters: This method does not take any parameters.
Return Value: This method returns IETF BCP 47 language tag representation of this locale.
Below programs illustrate the working of toLanguageTag() method:
Program 1:
import java.util.*;
public class Locale_Demo {
public static void main(String[] args)
{
Locale first_locale
= new Locale( "Germany" );
System.out.println( "Locale: "
+ first_locale);
System.out.println( "The LanguageTag: "
+ first_locale.toLanguageTag());
}
}
|
Output:
Locale: germany
The LanguageTag: germany
Program 2:
import java.util.*;
public class Locale_Demo {
public static void main(String[] args)
{
Locale first_locale
= new Locale( "en" , "USA" );
System.out.println( "Locale: "
+ first_locale);
System.out.println( "The LanguageTag: "
+ first_locale.toLanguageTag());
}
}
|
Output:
Locale: en_USA
The LanguageTag: en
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Locale.html#toLanguageTag()
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...