Search code examples
javaplayframeworklocaleepub

How to change the Locale of epubcheck when using it as a library


I have a play! 2.4 webapp, that validates epubs with epubcheck 4.0.1.

The epubcheck.jar contains localizations for several languages.

When I run the jar from command line I get localized error messages.

java -Duser.language=es -jar epubcheck.jar some.epub 

Validación usando la versión de reglas epub 2.0.1.
INFO(CSS-007): some.epub/OEBPS/css/style.css(10,3): La propiedad 'font-face' OEBPS/fonts/ABeeZee-Regular.woff hace referencia a un tipo de fuente no estándar application/octet-stream.
INFO(CSS-007): some.epub/OEBPS/css/style.css(16,3): La propiedad 'font-face' OEBPS/fonts/ABeeZee-Italic.woff hace referencia a un tipo de fuente no estándar application/octet-stream.
No se han detectado errores o advertencias.
epubcheck completado

But when I run it as a library it always defaults to english.

I tried:

// set locale for translated messages
Locale locale = new Locale(language); 
Locale.setDefault(locale);
language = Locale.getDefault().getLanguage(); // is "es" now

How can I make epubcheck use it's other localizations?

EDIT:

I get the output of epubcheck like that:

// catch stderror output of epubvalidator
ByteArrayOutputStream ba = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(ba);        
System.setOut(ps);
System.setErr(ps);
EpubCheck epubCheck = new EpubCheck(file);
// .. get stdout as string
String resultString = new String(ba.toByteArray(), "UTF-8");

Solution

  • Edit 11/12/2015: I added code to support per-instance localization and issued a pull request to the EpubCheck repo. You can see the code here:

    I'll continue to track this and update this post with developments.

    Original answer:

    Epubcheck supports i18n internationalization since 4.0. You can check out the internationalization page on the GitHub wiki for the project. Currently supports en, ja, de, es, fr, it.

    The following code should do what you need:

    File file = new File("/home/matthew/Desktop/RobinHood-1.0.epub");
    Locale l = new Locale("es");
    Locale.setDefault(l);
    
    // Create a stream to hold the output
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream old = System.out;
    System.setOut(new PrintStream(baos));
    
    // Do epubcheck stuff...
    EpubCheck epubCheck = new EpubCheck(file);
    epubCheck.validate();
    
    // Flush and reset output stream
    System.out.flush();
    System.setOut(old);
    
    System.out.println("Result >> " + baos.toString());
    

    The default output displays in Spanish now:

    Result >> Validación usando la versión de reglas epub 2.0.1.

    Edit: Since Locale.setDefault() changes the JVM's global locale, it's not appropriate for server-side use.