3.18.  Language

Overview

Screen readers can read PDF documents out loud for visually handicapped users. It helps if a document can inform the screen readers of its language:

These methods are available for tests:

// Tests for PDF language:

.hasLanguageInfo(..) 
.hasNoLanguageInfo()

Examples

The following example verifies that the document language is set to the English language for Great Britain:

@Test
public void hasLocale_CaseInsensitive() throws Exception {
  String filename = "documentUnderTest.pdf";

  AssertThat.document(filename)  
            .hasLanguageInfo("en-gb")  1
  ;
  AssertThat.document(filename)
            .hasLanguageInfo("en_GB")  2
  ;
}

1

Notation typical for PDF

2

Notation typical for Java

The string for the language is treated case independent. Underscore and hyphen are equivalent.

You can also use java.util.Locale directly:

@Test
public void hasLocale_LocaleInstance_GERMANY() throws Exception {
  String filename = "documentUnderTest.pdf";

  AssertThat.document(filename)
            .hasLanguageInfo(Locale.GERMANY)
  ;
}
@Test
public void hasLocale_LocaleInstance_GERMAN() throws Exception {
  String filename = "documentUnderTest.pdf";

  AssertThat.document(filename)
            .hasLanguageInfo(Locale.GERMAN)
  ;
}

A PDF document with the actual locale "en_GB" is tested successfully when using the locale Locale.en. In the opposite case, a document with the actual locale "en" fails when it is tested against the expected locale Locale.UK.

You can also check that a PDF document does not have a country code:

@Test
public void hasNoLanguageInfo() throws Exception {
  String filename = "documentUnderTest.pdf";

  AssertThat.document(filename)
            .hasNoLanguageInfo()
  ;
}