Merge pull request #1604 from rohitwaghchaure/multi-lingual-2
[enhancement] multi-lingual print formats
This commit is contained in:
commit
803feec337
11 changed files with 105 additions and 22 deletions
|
|
@ -51,9 +51,9 @@ def _(msg, lang=None):
|
|||
|
||||
if lang == "en":
|
||||
return msg
|
||||
|
||||
|
||||
return get_full_dict(local.lang).get(msg) or msg
|
||||
|
||||
|
||||
def get_lang_dict(fortype, name=None):
|
||||
"""Returns the translated language dict for the given type and name.
|
||||
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ import frappe.desk.desk_page
|
|||
from frappe.utils import get_gravatar
|
||||
from frappe.desk.form.load import get_meta_bundle
|
||||
from frappe.utils.change_log import get_versions
|
||||
from frappe.translate import get_lang_dict
|
||||
|
||||
def get_bootinfo():
|
||||
"""build and return boot info"""
|
||||
|
|
@ -77,7 +78,8 @@ def get_bootinfo():
|
|||
|
||||
bootinfo.error_report_email = frappe.get_hooks("error_report_email")
|
||||
bootinfo.calendars = sorted(frappe.get_hooks("calendars"))
|
||||
|
||||
bootinfo["lang_dict"] = get_lang_dict()
|
||||
|
||||
return bootinfo
|
||||
|
||||
def load_conf_settings(bootinfo):
|
||||
|
|
|
|||
|
|
@ -83,6 +83,11 @@ def get_data():
|
|||
"name": "Error Snapshot",
|
||||
"description": _("Log of error during requests.")
|
||||
},
|
||||
{
|
||||
"type": "doctype",
|
||||
"name": "Translation",
|
||||
"description": _("For user specific translations")
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
|
|||
|
|
@ -125,7 +125,7 @@ def add_all_roles_to(name):
|
|||
|
||||
@frappe.whitelist()
|
||||
def load_messages(language):
|
||||
"""Load translation messages for given langauge from all `setup_wizard_requires`
|
||||
"""Load translation messages for given language from all `setup_wizard_requires`
|
||||
javascript files"""
|
||||
frappe.clear_cache()
|
||||
set_default_language(language)
|
||||
|
|
|
|||
|
|
@ -17,13 +17,13 @@ frappe.ui.form.PrintPreview = Class.extend({
|
|||
this.wrapper.find(".btn-print-close").click(function() {
|
||||
me.frm.hide_print();
|
||||
});
|
||||
|
||||
|
||||
// hide print view on pressing escape, only if there is no focus on any input
|
||||
$(document).on("keydown", function(e) {
|
||||
if (e.which===27 && me.frm && e.target===document.body) {
|
||||
me.frm.hide_print();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
this.print_formats = frappe.meta.get_print_formats(this.frm.meta.name);
|
||||
this.print_letterhead = this.wrapper
|
||||
|
|
@ -35,14 +35,15 @@ frappe.ui.form.PrintPreview = Class.extend({
|
|||
this.print_sel = this.wrapper
|
||||
.find(".print-preview-select")
|
||||
.on("change", function() {
|
||||
if(me.is_old_style()) {
|
||||
me.wrapper.find(".btn-download-pdf").toggle(false);
|
||||
me.set_style();
|
||||
me.preview_old_style();
|
||||
} else {
|
||||
me.wrapper.find(".btn-download-pdf").toggle(true);
|
||||
me.preview();
|
||||
}
|
||||
me.multilingual_preview()
|
||||
});
|
||||
|
||||
//On selection of language get code and pass it to preview method
|
||||
this.language_sel = this.wrapper
|
||||
.find(".languages")
|
||||
.on("change", function(){
|
||||
me.lang_code = me.language_sel.val()
|
||||
me.multilingual_preview()
|
||||
});
|
||||
|
||||
this.wrapper.find(".btn-print-print").click(function() {
|
||||
|
|
@ -67,7 +68,8 @@ frappe.ui.form.PrintPreview = Class.extend({
|
|||
+"doctype="+encodeURIComponent(me.frm.doc.doctype)
|
||||
+"&name="+encodeURIComponent(me.frm.doc.name)
|
||||
+"&format="+me.selected_format()
|
||||
+"&no_letterhead="+(me.with_letterhead() ? "0" : "1"));
|
||||
+"&no_letterhead="+(me.with_letterhead() ? "0" : "1")
|
||||
+"&_lang="+me.lang_code);
|
||||
if(!w) {
|
||||
msgprint(__("Please enable pop-ups")); return;
|
||||
}
|
||||
|
|
@ -97,6 +99,25 @@ frappe.ui.form.PrintPreview = Class.extend({
|
|||
}
|
||||
});
|
||||
},
|
||||
set_user_lang: function(){
|
||||
this.lang_code = this.frm.doc.language;
|
||||
// Load all languages in the field
|
||||
this.language_sel.empty()
|
||||
.add_options(frappe.get_languages_dict())
|
||||
.val(this.lang_code);
|
||||
this.preview();
|
||||
},
|
||||
multilingual_preview: function(){
|
||||
var me = this;
|
||||
if(this.is_old_style()) {
|
||||
me.wrapper.find(".btn-download-pdf").toggle(false);
|
||||
me.set_style();
|
||||
me.preview_old_style();
|
||||
} else {
|
||||
me.wrapper.find(".btn-download-pdf").toggle(true);
|
||||
me.preview();
|
||||
}
|
||||
},
|
||||
preview: function() {
|
||||
var me = this;
|
||||
this.get_print_html(function(out) {
|
||||
|
|
@ -114,7 +135,8 @@ frappe.ui.form.PrintPreview = Class.extend({
|
|||
+"&name="+encodeURIComponent(me.frm.doc.name)
|
||||
+(printit ? "&trigger_print=1" : "")
|
||||
+"&format="+me.selected_format()
|
||||
+"&no_letterhead="+(me.with_letterhead() ? "0" : "1"));
|
||||
+"&no_letterhead="+(me.with_letterhead() ? "0" : "1")
|
||||
+"&_lang="+me.lang_code);
|
||||
if(!w) {
|
||||
msgprint(__("Please enable pop-ups")); return;
|
||||
}
|
||||
|
|
@ -125,7 +147,8 @@ frappe.ui.form.PrintPreview = Class.extend({
|
|||
args: {
|
||||
doc: this.frm.doc,
|
||||
print_format: this.selected_format(),
|
||||
no_letterhead: !this.with_letterhead() ? 1 : 0
|
||||
no_letterhead: !this.with_letterhead() ? 1 : 0,
|
||||
_lang: this.lang_code
|
||||
},
|
||||
callback: function(r) {
|
||||
if(!r.exc) {
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
<div class="form-print-wrapper">
|
||||
<div class="print-toolbar row">
|
||||
<div class="col-xs-3">
|
||||
<div class="col-xs-2">
|
||||
<select class="print-preview-select input-sm form-control"></select></div>
|
||||
<div class="col-xs-3">
|
||||
<div class="col-xs-2">
|
||||
<select class="languages input-sm form-control"></select></div>
|
||||
<div class="col-xs-2">
|
||||
<div class="checkbox small" style="margin-top: 7px; margin-bottom: 0px;">
|
||||
<label>
|
||||
<input type="checkbox" class="print-letterhead text-muted" style="margin-top: 1px;"/>
|
||||
|
|
|
|||
|
|
@ -15,3 +15,11 @@ frappe._ = function(txt, replace) {
|
|||
return ret;
|
||||
};
|
||||
window.__ = frappe._
|
||||
|
||||
frappe.get_languages_dict = function() {
|
||||
var lang_dict = []
|
||||
$.each(frappe.boot.lang_dict, function(lang, val){
|
||||
lang_dict.push({'label': lang, 'value': val})
|
||||
})
|
||||
return lang_dict
|
||||
};
|
||||
|
|
|
|||
|
|
@ -76,6 +76,8 @@ frappe.views.CommunicationComposer = Class.extend({
|
|||
fieldname:"attach_document_print"},
|
||||
{label:__("Select Print Format"), fieldtype:"Select",
|
||||
fieldname:"select_print_format"},
|
||||
{label:__("Select Languages"), fieldtype:"Select",
|
||||
fieldname:"language_sel"},
|
||||
{fieldtype: "Column Break"},
|
||||
{label:__("Select Attachments"), fieldtype:"HTML",
|
||||
fieldname:"select_attachments"}
|
||||
|
|
@ -84,6 +86,7 @@ frappe.views.CommunicationComposer = Class.extend({
|
|||
|
||||
prepare: function() {
|
||||
this.setup_subject_and_recipients();
|
||||
this.setup_print_language()
|
||||
this.setup_print();
|
||||
this.setup_attach();
|
||||
this.setup_email();
|
||||
|
|
@ -215,6 +218,26 @@ frappe.views.CommunicationComposer = Class.extend({
|
|||
return frappe.last_edited_communication[cur_frm.doctype][key];
|
||||
},
|
||||
|
||||
setup_print_language: function() {
|
||||
var me = this;
|
||||
var doc = cur_frm.doc;
|
||||
var fields = this.dialog.fields_dict;
|
||||
|
||||
//Load default print language from doctype
|
||||
this.lang_code = doc.language
|
||||
|
||||
//On selection of language retrieve language code
|
||||
$(fields.language_sel.input).click(function(){
|
||||
me.lang_code = this.value
|
||||
})
|
||||
|
||||
// Load all languages in the select field language_sel
|
||||
$(fields.language_sel.input)
|
||||
.empty()
|
||||
.add_options(frappe.get_languages_dict())
|
||||
.val(doc.language)
|
||||
},
|
||||
|
||||
setup_print: function() {
|
||||
// print formats
|
||||
var fields = this.dialog.fields_dict;
|
||||
|
|
@ -373,7 +396,8 @@ frappe.views.CommunicationComposer = Class.extend({
|
|||
print_format: print_format,
|
||||
communication_medium: form_values.communication_medium,
|
||||
sent_or_received: form_values.sent_or_received,
|
||||
attachments: selected_attachments
|
||||
attachments: selected_attachments,
|
||||
_lang : me.lang_code
|
||||
},
|
||||
btn: btn,
|
||||
callback: function(r) {
|
||||
|
|
|
|||
|
|
@ -173,6 +173,7 @@ _f.Frm.prototype.print_doc = function() {
|
|||
}
|
||||
this.print_preview.refresh_print_options().trigger("change");
|
||||
this.page.set_view("print");
|
||||
this.print_preview.set_user_lang()
|
||||
}
|
||||
|
||||
_f.Frm.prototype.hide_print = function() {
|
||||
|
|
|
|||
|
|
@ -94,7 +94,7 @@ data-fieldname="{{ df.fieldname }}" data-fieldtype="{{ df.fieldtype }}"
|
|||
{% elif df.fieldtype=="HTML" %}
|
||||
{{ frappe.render_template(df.options, {"doc":doc}) }}
|
||||
{% else %}
|
||||
{{ doc.get_formatted(df.fieldname, parent_doc or doc) }}
|
||||
{{ _(doc.get_formatted(df.fieldname, parent_doc or doc)) }}
|
||||
{% endif %}
|
||||
{%- endmacro %}
|
||||
|
||||
|
|
|
|||
|
|
@ -179,6 +179,10 @@ def get_full_dict(lang):
|
|||
# cache lang
|
||||
frappe.cache().hset("lang_full_dict", lang, frappe.local.lang_full_dict)
|
||||
|
||||
# get user specific transaltion data
|
||||
user_translations = get_user_translations(lang)
|
||||
if user_translations:
|
||||
frappe.local.lang_full_dict.update(user_translations)
|
||||
return frappe.local.lang_full_dict
|
||||
|
||||
def load_lang(lang, apps=None):
|
||||
|
|
@ -210,6 +214,20 @@ def get_translation_dict_from_file(path, lang, app):
|
|||
|
||||
return cleaned
|
||||
|
||||
def get_user_translations(lang):
|
||||
out = frappe.cache().hget('lang_user_translations', lang)
|
||||
if not out:
|
||||
out = {}
|
||||
for fields in frappe.get_all('Translation',
|
||||
fields= ["source_name", "target_name"],filters={'language_code': lang}):
|
||||
out.update({fields.source_name: fields.target_name})
|
||||
frappe.cache().hset('lang_user_translations', lang, out)
|
||||
return out
|
||||
|
||||
# def get_user_translation_key():
|
||||
# return 'lang_user_translations:{0}'.format(frappe.local.site)
|
||||
|
||||
|
||||
def clear_cache():
|
||||
"""Clear all translation assets from :meth:`frappe.cache`"""
|
||||
cache = frappe.cache()
|
||||
|
|
@ -532,7 +550,7 @@ def write_translations_file(app, lang, full_dict=None, app_messages=None):
|
|||
|
||||
:param app: `app` for which translations are to be written.
|
||||
:param lang: Language code.
|
||||
:param full_dict: Full translated langauge dict (optional).
|
||||
:param full_dict: Full translated language dict (optional).
|
||||
:param app_messages: Source strings (optional).
|
||||
"""
|
||||
if not app_messages:
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue