2013-12-16 4 views
4

나는 Mustache 사양과 호환되는 Hogan.js를 사용하고 있습니다. 그리고 복수화를 수행하는 확실한 방법을 구현하는 데 문제가 있습니다.호간 (Hogan)에서 복수화 처리 방법

{{#plural(count)}} 
    I have {{count}} apples! 
{{/plural(count)}} 

데이터 :

{ 
    count: 2, 
    'plural(count)': function() { 
    return function() { 
     return _t[arguments[0].trim()][this['count']] 
    } 
    } 
} 

이 나는 호건을 계속 사용하고이 같은 일을

이 간단한 경우

TPL

작동 처리 국제화에 대한 http://i18next.com/을 사용하고 싶습니다 필요한 복수 메소드 (복수 (key.val) 등)를 모두 생성 할 수 있도록 별도의 단계에서 구문 분석/스캐닝/렌더링이 필요합니다. 하지만 괜찮습니다. 서버 부팅시 한 번만 수행하면됩니다.

는 {{#plural (key.nested)}} 데이터가

{ 
    'plural(key': { 
    'val)': ... 
    } 
} 

처럼 보였다 경우 일치합니다

이 또한 필요

같은 것들에 휴식

나에게 수동으로 문맥의 값을 주요 문제가 아닌 조회하고 있지만 해결할 수없는 람다/부분적인 경우가있다.

,기본 변환 매핑에 대한

에게, 일이 훨씬 덜 복잡하고,

+0

일부 콧수염 구현은 다음을 처리 할 수 ​​있습니다. https://github.com/groue/GRMustache/issues/50 –

+0

@Gwendal Roué Thanks. 나는 또한 그것을 게시하기 전에 발견했다. 그게 내 예제 코드에서 비롯됩니다. 그러나 람다의 컨텍스트에서 렌더러 없이는 어렵고 lib isnt javascript (더 명확하게 태그로 자바 스크립트를 추가합니다) –

+0

"콧수염 템플릿 복수화"에 대한 Google 검색은 몇 가지를 산출합니다. 다른 결과들도 ... 거기서 해결책을 찾지 않겠습니까? –

답변

0

좋아, 내가 생각하는 방식이 문제를 처리하는 것이 가장 좋습니다 발견 처리하기 쉬운 이잖아 : 대체

var tpl_data = fs.readFileSync('./tpl/test.hjs', 'utf8'); 
var scan = Hogan.scan(tpl_data); 
var tree = Hogan.parse(scan); 
var gen = Hogan.generate(tree, tpl_data, {asString:false}); 
var out = gen.render(data); 

트리를 변경을 n가 i18next에 대한 옵션을 추가하여 패턴 /i18n .+/

내가 {{#i18n {count: count, text: 'I have <%count%> apples!'} }} 사용

등을 일치 i18n 모든 tag 키그래서 모든 n 's이 (가)

Hogan.codegen.i18n = function (node, context) { 
    context.code += 't.b(t.v(t.i18n("' + esc(node.n) + '",c,p,0)));'; 
} 

그 내부

Hogan.Template.prototype.i18n = function (key, ctx, partials, returnFound) { 
    //here the ctx is an array with from right to left the render scopes 
    // most right is the most inner scope, most left is the full render data 
    //1. get the config from the key, 
    //2. get the values out of the scope 
    //3. get the values for the translation 
    //4. lookup the translation, and repleace the values in the translation 
    //5. return the translated string 
}; 

참고 Hogan.Template의 프로토 타입에 i18n 방법을 추가 Hogan.codegen하는 i18n를 추가 i18n

로 시작 일치 Hogan.Template.prototype.i18n 템플릿의 모든 방법에 액세스 할 수 있습니다.