19 September 2007

Overwriting valueForKeyPath

I need to access a localalized strings file for the right language and I don't like the Localizer method.
I wanted to access it through Application and in the WOD files by simply adding "application.@ls.MY_STRING_TO_GET_FROM_LOCALIZED_STRING".

Added the following to Application.java:


  public Object valueForKeyPath( String keypath ) {

// Catch all keypaths starting with ls and do whatever you want with it
        if( keypath.startsWith( "@ls" ) )
            return localizedStringForKey( keypath.substring( 4, keypath.length() ) );

        return super.valueForKeyPath( keypath );
    }

    public String localizedStringForKey( String key ) {
        return WOApplication.application().resourceManager().stringForKey( key, "localizedStrings", "!!LOCALIZED_STRING_NOT_FOUND!!", "app", languages() );
    }

No comments: