<commit_before>[up](../../../../tree/master/Guides/sample_code), [next](../forking.md)

Localization
============

Overview
--------

Mustache and GRMustache have no built-in localization feature. It is thus a matter of injecting our own application code into the template rendering, some code that localizes its input.

[Rendering objects](../rendering_objects.md) are our vector. We'll eventually render the following template:

    {{#localize}}
        {{name1}} and {{name2}}
        {{#count}}
            {{! at least one mutual friend }}
            have
            {{#isPlural(count)}}
                {{! several mutual friend }}
                {{count}} mutual friends
            {{/}}
            {{^isPlural(count)}}
                {{! single mutual friend }}
                one mutual friend
            {{/}}
        {{/}}
        {{^count}}
            {{! no mutual friend }}
            have no mutual friend
        {{/}}.
    {{/localize}}

Into the various renderings below, depending on the current locale:

    Arthur and Barbara have no mutual friend.
    Craig et Dennis ont un ami commun.
    Eugene y Fiona tiene 5 amigos en común.

Yet this will be quite a smartish sample code, and it's better starting with simpler cases. We'll see how to localize:

1. a section of a template
    
        {{#localize}}Hello{{/localize}}
    
2. a value
    
        {{#localize}}{{greeting}}{{/localize}}
    
3. a portion of a template *with arguments*, as above:
    
        {{#localize}}Hello {{name1}}, do you know {{name2}}?{{/localize}}

4. a portion of a template with arguments and *conditions*, as above:
    
        {{#localize}}{{name1}} and {{name2}} {{#count}}have {{#isPlural(count)}}{{count}} mutual friends{{/}}{{^isPlural(count)}}one mutual friend{{/}}{{/count}}{{^count}}have no mutual friend{{/count}}.{{/localize}}

Of course, we'll always eventually use the standard `NSLocalizedString` function.

Localizing a template section
-----------------------------

**[Download the code](../../../../tree/master/Guides/sample_code/localization)**

`Document.mustache`:

    {{#localize}}Hello{{/localize}}

`Render.m`:

```objc
id data = @{
    @"localize": [GRMustache renderingObjectWithBlock:^NSString *(GRMustacheTag *tag, GRMustacheContext *context, BOOL *HTMLSafe, NSError **error) {
        return NSLocalizedString(tag.innerTemplateString, nil);<commit_msg>Progressive Localization Guide<commit_after>[up](../../../../tree/master/Guides/sample_code), [next](../forking.md)

Localization
============

Overview
--------

Mustache and GRMustache have no built-in localization feature. It is thus a matter of injecting our own application code into the template rendering, some code that localizes its input.

We'll see below how to localize:

1. a section of a template
    
        {{#localize}}Hello{{/localize}}
    
    into:
    
        Hello
        Bonjour
        Hola
        
2. a value
    
        {{#localize}}{{greeting}}{{/localize}}
    
    into:
    
        Hello
        Bonjour
        Hola
        
3. a portion of a template *with arguments*, as above:
    
        {{#localize}}Hello {{name1}}, do you know {{name2}}?{{/localize}}
    
    into:
    
        Hello Arthur, do you know Barbara?
        Bonjour Arthur, est-ce que tu connais Barbara ?
        Hola Arthur, sabes Barbara?

4. a portion of a template with arguments and *conditions*, as above:
    
        {{#localize}}{{name1}} and {{name2}} {{#count}}have {{#isPlural(count)}}{{count}} mutual friends{{/}}{{^isPlural(count)}}one mutual friend{{/}}{{/count}}{{^count}}have no mutual friend{{/count}}.{{/localize}}
    
    into:
    
        Arthur and Barbara have no mutual friend.
        Craig et Dennis ont un ami commun.
        Eugene y Fiona tiene 5 amigos en común.

Of course, we'll always eventually use the standard `NSLocalizedString` function.

Localizing a template section
-----------------------------

**[Download the code](../../../../tree/master/Guides/sample_code/localization)**

`Document.mustache`:

    {{#localize}}Hello{{/localize}}

`Render.m`:

```objc
id data = @{
    @"localize": [GRMustache renderingObjectWithBlock:^NSString *(GRMustacheTag *tag, GRMustacheContext *context, BOOL *HTMLSafe, NSError **error) {
        return NSLocalizedString(tag.innerTemplateString, nil);
{'fraction_non_alphanumeric': 0.15948375201659368, 'fraction_numerical': 0.00460935699469924, 'mean_word_length': 2.640939597315436, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7200978', 'n_tokens_mistral': 1269, 'n_tokens_neox': 1227, 'n_words': 396}