2014-10-07 1 views

답변

0

가 잘 작동 :

def getMessage(String code, Object[] args = null) 
{ 
    // assuming the test cwd is the project dir (where application.properties is) 
    URL url = new File('grails-app/i18n').toURI().toURL() 
    def messageSource = new ResourceBundleMessageSource() 
    messageSource.bundleClassLoader = new URLClassLoader(url) 
    messageSource.basename = 'messages' 
    messageSource.getMessage(code, args, Locale.ENGLISH) 
} 

그리고 설치 기능에서 나는 (위의 기능을 사용하는) 내 기능을 조롱했습니다 :

ResponsHelper.metaClass.static.jsonError = { String code, Object[] args = null -> 
     getMessage(code, args) 
    } 

    ResponsHelper.metaClass.static.jsonErrorsNoArgsInList = { List errors -> 
     errors.collect { 
      getMessage(it, []) 
     } 
    } 
3

공식 문서 확인 - http://grails.org/doc/2.2.x/guide/i18n.html i18n 메시지를 설정하고 읽는 방법에 대한 흥미로운 정보를 찾을 수 있습니다. 본질적으로

:

에 국제화 메시지를 넣어 또, grails-app/i18n/messages_ [LANG_CODE] [LANG_CODE] 당신이이 파일은 간단한 INI와 같은 파일입니다에 관심이있는 언어 코드를 나타냅니다 .properties의, 그래서.이 그것은 키 = 값을 포함합니다 eg

site_title=Your site internationalized site name 

이 메시지를 사용할 수 있습니다. 당신이 볼 수 있듯이, "코드"의 속성에 메시지의 키와 관련된 속성

<g:message code="site_title" /> 

가 파일 : 당신이 당신의 GSP보기에 표시하려는 경우, 간단한 taglig를 사용합니다. 당신이 컨트롤러에 국제화 메시지가하고 싶은 경우에, 당신은 주입해야 할 것 "MessageSource를"콩 :

def messageSource 

그리고 그 후이 방법을 사용하여 컨트롤러 액션에 메시지를받을 수 있습니다 :

messageSource.getMessage('site_title', null, request.locale, 'here you can put some default value if i18n label is not found') 

'site_title'키와 관련된 메시지를 반환합니다.

한 가지 더 - Grails 앱은 브라우저 로캘을 사용합니다. 변경하려는 경우 URL 요청에 lang 매개 변수를 전달합니다. 예 : http://yourappdomain.com?lang=en

+0

이 링크를 더 일찍 보았습니다. 그것에 대해 좋은 사례를 찾아야합니다. – roeygol

0

런타임시 변경 방법을 의미합니까?

은 주로 당신은 봄/resource.groovy 파일에 같이 <g:message code="this.is.your.code.localization.hellolable"/>

를 사용합니다. 그리고 지나가는 컨트롤러 방식?

Look this answer here

유 그 주입했는지 또는 사용할 수 있도록 .. 어떤 종류의 ... 메시징 1. message bean itself 에 관여하는 두 개의 클래스가 있습니다. 이 서비스조차도 코드를 사용하여 메시지에 액세스 할 수 있습니다.

+0

사실, 내 문제는 Spock 유닛 테스트를 실행할 때 메시지가 표시됩니다. 파일을 읽을 수없는 것처럼 보입니다. 나는 항상 'org.springframework.context.NoSuchMessageException : 메시지를 찾을 수 없습니다' 로케일 'en_US'. '*에 대한'default.access.denied.message '는이 로케일 또는이 메시지가있는 다른 로켈에 대한 것입니다. – roeygol

+0

괜찮아요 대답을 – danielad

+0

내가 groovy 클래스에, 그리고 GSP 파일에있을 때이 함수를 호출해야합니다 – roeygol

0

사용하면 SRC 내부 g.message 태그를 사용하려는 경우이 코드 :

def grailsApplication = ApplicationHolder.application 
def g = grailsApplication.mainContext.getBean(
    'org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib') 
g.message(code:'your.code.in.message.property', locale:'locale.name') // set locale only for this message 

아니면 응용 프로그램 수준이 사용할 수 있습니다 : 내가 사용하는 경우

def localeResolver = RequestContextUtils.getLocaleResolver(request); // returns a SessionLocaleResolver 
def locale = new Locale("fr", "CA"); 
localeResolver.setLocale(request, response, locale); 
+0

첫 번째 옵션은 예외를 throw합니다 - * org.springframework.beans.factory.NoSuchBeanDefinitionException : 'org.codehaus'라는 Bean이 없습니다. groovy.grails.plugins.web.taglib.ValidationTagLib '가 정의되었습니다 *. 두 번째로 요청 개체를 가져올 수 없습니다. 코드가있을 수 있습니다. – roeygol

관련 문제