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

Added option to translate schema titles #199

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jakobrosenberg
Copy link

Users can now translate schema titles by adding title_processing to their JSONEditor options.

Examle:
JSONEditor.defaults.options = {
templates: 'handlebars',
theme: 'bootstrap3',
title_processing: function(title){
return I18n(title);
}
};

Users can now translate schema titles by adding title_processing to their JSONEditor options.

Examle:
 JSONEditor.defaults.options = {
    templates: 'handlebars',
    theme: 'bootstrap3',
    title_processing: function(title){
      return I18n(title);
    }
  };
@SebT
Copy link
Contributor

SebT commented Jul 24, 2014

👍

IMO, you should also pass the key as a parameter to the processing function :

if(this.jsoneditor.options.title_processing) return this.jsoneditor.options.title_processing(title, key);

because the key is a safer indication that the title itself.

@jakobrosenberg
Copy link
Author

@SebT they key is already passed automatically. The current title function uses the key unless a title is defined.

@SebT
Copy link
Contributor

SebT commented Jul 29, 2014

@jakobrosenberg Yes I know. But your custom function only takes 1 parameter. And inside the scope of your function, we can't know for sure if this parameter is the title or the key.

// Let's say the custom function is:
function title_processing(param) {
  // here, param can be the key or the title, we don't know
}

Because var title = this.schema.title || this.key; makes title equal to the property title if it is not empty and exists. Else, it will be equal to the key instead, not both. So calling this.jsoneditor.options.title_processing(title); afterwards just passes ONE parameter.

@blabno
Copy link
Contributor

blabno commented Jul 30, 2014

Could we have more generic approach to i18n, so that also enum values are translated?

 getTitle: function() {
    return this.jsoneditor.translate(this.key, null, this.schema);
  },
JSONEditor.defaults.translate = function(key, variables, translatable) {
  var lang = JSONEditor.defaults.languages[JSONEditor.defaults.language];
  if(!lang) throw "Unknown language "+JSONEditor.defaults.language;

  var string;
  if(translatable) {
    if(translatable.translation) {
      string = translatable.translation[JSONEditor.defaults.language] || translatable.translation[JSONEditor.defaults.default_language];
    } else {
      string = translatable[JSONEditor.defaults.language] || translatable[JSONEditor.defaults.default_language];
    }
  } else {
    string = lang[key] || JSONEditor.defaults.languages[JSONEditor.defaults.default_language][key];
  }

  if(typeof string === "undefined")  {
    throw "Unknown translate string "+key;
  }

  if(variables) {
    for(var i=0; i<variables.length; i++) {
      string = string.replace(new RegExp('\\{\\{'+i+'}}','g'),variables[i]);
    }
  }

  return string;
};```

@blabno blabno mentioned this pull request Jul 30, 2014
@jakobrosenberg
Copy link
Author

Sorry for the late reply. I disagree on your approach. I feel my solution is far more generic and flexible. In only two additional lines it allows for manipulation of the key or title. Anyone is free to use their own i18n library if they want translation; they can capitalize and manipulate the titles or decorate them with tags.

It's a simple hook to manipulate the title and it offers more options for a much smaller increase in file size.

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

Successfully merging this pull request may close these issues.

3 participants