2012-01-30 3 views
4

리소스 번들에서 메시지/번역 목록을 가져 오려고했지만 실패 (예외 발생)했습니다. 앱이 IDEA의 Tomcat에서 실행 중입니다.Grails : 리소스 번들 읽기

Locale locale = new Locale("en"); 
ResourceBundle bundle = ResourceBundle.getBundle('i18n/dictionary', locale); 

잘못된 점이 무엇입니까? i18n/사전이 클래스 경로에 있습니다. 'i18n/dictionary'일 수 있습니다. 잘못되었습니다.

나는 메시지 소스를 얻을 수 있어요,하지만 난이 (SPRING) 객체에서 키를 얻을 수 없습니다

def messageSource = grailsAttributes.getApplicationContext().getBean("messageSource"); 

답변

1

자원 경로가 불완전. 프런트 엔드에 변환 표가 필요한 경우, 아마도 다음과 같은 컨트롤러가 유용 할 수있다 :

class ClientMessagesController { 

def index = { 
    Locale locale = session.getAttribute('locale') ?: new Locale("en"); 
    ResourceBundle bundle = ResourceBundle.getBundle('\\grails-app\\i18n\\clientMessages', locale); 

    def sep = ''; 
    def sb = new StringBuilder(); 
    sb.append('<script type="text/javascript">\n'); 
    sb.append('_i18n = {\n'); 
    bundle.getKeys().each {key -> 
     sb.append(sep); 
     sb.append(key.replace('.', '_')); 
     sb.append(': "'); 
     sb.append(bundle.getString(key).replace('"', '&quot;')); 
     sb.append('"\n'); 
     sep = ','; 
    } 
    sb.append('};\n</script>\n') 
    render(text: sb.toString()); 
} 

}

+4

json으로 직접 구축하지 마십시오,'JsonBuilder'를 사용 –