Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need some fallback for Namespace. #67

Closed
Virbhadrasinh opened this issue Jan 22, 2013 · 2 comments
Closed

Need some fallback for Namespace. #67

Virbhadrasinh opened this issue Jan 22, 2013 · 2 comments

Comments

@Virbhadrasinh
Copy link

I have following JSON for language, Handlebars helper and HTML

  1. JSON for language

    var resources = {
    "dev" : {
        "translation" : {
            "title" : "Mr."
        }
    },
    "en" : {
        "translation" : {
            "last" : "Chauhan"
        },
        "name" : {
            "second" : "Jashvantsinh"
        }
    },
    "en-US" : {
        "name" : {
            "first" : "Virbhadrasinh"
        }
    }
    };
    
  2. Handlebars helper

    Handlebars.registerHelper('t', function(key, nameSpace ) {
      return $.i18n.t(key,nameSpace);
    });
    
  3. HTML
    var nameSpace = {
    ns : 'name'
    };

    <div>
    <span>{{t "title" nameSpace}}</span>
    <span>{{t "first" nameSpace}}</span>
    <span>{{t "second" nameSpace}}</span>
    <span>{{t "last" nameSpace}}</span>
    </div>

Now, whenever i am trying to render this HTML, it will not give me values for keys "title" and "last" because they are not in "name" nameSpace.

So, i need some fallback which look into default nameSpace if key is not found in given nameSpace.

So, I added "found = translate(key);" inside last if condition of "_translate" function and its working fine but its recursive function call. So, it may affect the performance.

Code :

     if (found === undefined) {
            notfound = applyReplacement(notfound, options);
            notfound = applyReuse(notfound, options);
            //Added line for namespace fallback...
            found = translate(key);
     }
@jamuhl
Copy link
Member

jamuhl commented Jan 22, 2013

not sure if i like to add an recursive lookup. might add this as an option - but not yet to sure.

personally i prefer adding the namespace like this:

<div>
<span>{{t "title"}}</span>
<span>{{t "first"}}</span>
<span>{{t "name:second"}}</span>
<span>{{t "name:last"}}</span>
</div>

@jamuhl
Copy link
Member

jamuhl commented Feb 11, 2013

starting from v1.5.11 (release should be this week) you could set option fallbackToDefaultNS = true in init options to turn on this behaviour. (default is false)

@jamuhl jamuhl closed this as completed Feb 11, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants