Improved I18N support:

* GamePageData now includes the selected language's code.
* Added support for multiple fetches in one call to the Translator
service.
This commit is contained in:
Emmanuel BENOîT 2012-04-05 11:25:08 +02:00
parent c7949e41cc
commit 9a7bc03171
9 changed files with 316 additions and 50 deletions
legacyworlds-server-beans-i18n/src/main/java/com/deepclone/lw/beans/i18n

View file

@ -239,6 +239,20 @@ class I18NData
}
/**
* Access the store for some language
*
* @param language
* the language to access
*
* @return the language store, or <code>null</code> if the language is not defined.
*/
LanguageStore getStore( String language )
{
return this.languages.get( language );
}
/**
* Sets or creates the translation for a given language/string identifier pair.
*
@ -257,7 +271,8 @@ class I18NData
* @throws IllegalArgumentException
* if the string does not exist
*/
String setTranslation( final int administrator , final String language , final String string , final String translation )
String setTranslation( final int administrator , final String language , final String string ,
final String translation )
{
// Get existing translation
LanguageStore store = this.languages.get( language );

View file

@ -1,7 +1,10 @@
package com.deepclone.lw.beans.i18n;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
@ -13,10 +16,13 @@ import com.deepclone.lw.interfaces.i18n.UnknownStringException;
/**
* Translation component
*
* <p>
* The translator bean's implementation uses the contents of the {@link I18NData} instance, which it
* only accesses in read-only mode.
*
* @author tseeker
* @author <a href="mailto:tseeker@legacyworlds.com">E. Benoît</a>
*/
public class TranslatorBean
implements Translator
@ -83,6 +89,23 @@ public class TranslatorBean
}
/* Documentation in Translator interface */
@Override
public String getLanguageName( String language )
throws UnknownLanguageException
{
this.data.readLock( ).lock( );
try {
if ( !this.data.isLanguageComplete( language ) ) {
throw new UnknownLanguageException( language );
}
return this.data.getLanguageName( language );
} finally {
this.data.readLock( ).unlock( );
}
}
/* Documentation in Translator interface */
@Override
public String translate( String language , String string )
@ -103,17 +126,30 @@ public class TranslatorBean
}
/* Documentation in Translator interface */
/**
* Access the store for the specified language then extract translations
*/
@Override
public String getLanguageName( String language )
throws UnknownLanguageException
public Map< String , String > translate( String language , Collection< String > strings )
throws UnknownStringException , UnknownLanguageException
{
this.data.readLock( ).lock( );
try {
if ( !this.data.isLanguageComplete( language ) ) {
throw new UnknownLanguageException( language );
}
return this.data.getLanguageName( language );
LanguageStore store = this.data.getStore( language );
HashMap< String , String > result = new HashMap< String , String >( );
for ( String identifier : strings ) {
String value = store.getTranslation( identifier );
if ( value == null ) {
throw new UnknownStringException( identifier );
}
result.put( identifier , value );
}
return result;
} finally {
this.data.readLock( ).unlock( );
}