<commit_before># Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt

from __future__ import unicode_literals

"""
Contributing:
1. Add the .csv file
2. Run import
3. Then run translate
"""

# loading
# doctype, page, report
# boot(startup)
# frappe.require
# frappe._

import frappe, os, re, codecs, json
from frappe.utils.jinja import render_include
from jinja2 import TemplateError

def guess_language_from_http_header(lang):
	"""set frappe.local.lang from HTTP headers at beginning of request"""
	if not lang:
		return frappe.local.lang

	guess = None
	lang_list = get_all_languages() or []

	if ";" in lang: # not considering weightage
		lang = lang.split(";")[0]
	if "," in lang:
		lang = lang.split(",")
	else:
		lang = [lang]

	for l in lang:
		code = l.strip()
		if code in lang_list:
			guess = code
			break

		# check if parent language (pt) is setup, if variant (pt-BR)
		if "-" in code:
			code = code.split("-")[0]
			if code in lang_list:
				guess = code
				break

	return guess or frappe.local.lang

def get_user_lang(user=None):
	"""set frappe.local.lang from user preferences on session beginning or resumption"""
	if not user:
		user = frappe.session.user

	# via cache
	lang = frappe.cache().get_value("lang:" + user)

	if not lang:

		# if defined in user profile
		user_lang = frappe.db.get_value("User", user, "language")
		if user_lang and user_lang!="Loading...":
			lang = get_lang_dict().get(user_lang)
		else:
			default_lang = frappe.db.get_default("lang")
			lang = default_lang or frappe.local.lang

		frappe.cache().set_value("lang:" + user, lang or "en")

	return lang

def set_default_language(language):
	lang = get_lang_dict()[language]
	frappe.db.set_default("lang", lang)
	frappe.local.lang = lang

def get_all_languages():
	return [a.split()[0] for a in get_lang_info()]

def get_lang_dict():
	return dict([[a[1], a[0]] for a in [a.split(None, 1) for a in get_lang_info()]])

def get_lang_info():
	return frappe.cache().get_value("langinfo",
		lambda:frappe.get_file_items(os.path.join(frappe.local.sites_path, "languages.txt")))

def get_dict(fortype, name=None):
	fortype = fortype.lower()
	cache = frappe.cache()
	cache_key = "translation_assets:" + frappe.local.lang
	asset_key = fortype + ":" + (name or "-")
	translation_assets = cache.get_value(cache_key) or {}

	if not asset_key in translation_assets:
		if fortype=="doctype":
			messages = get_messages_from_doctype(name)
		elif fortype=="page":
			messages = get_messages_from_page(name)
		elif fortype=="report":
			messages = get_messages_from_report(name)
		elif fortype=="include":
			messages = get_messages_from_include_files()
		elif fortype=="jsfile":
			messages = get_messages_from_file(name)
		elif fortype=="boot":
			messages = get_messages_from_include_files()
			messages += frappe.db.sql_list("select name from tabDocType")
			messages += frappe.db.sql_list("select name from tabRole")
			messages += frappe.db.sql_list("select name from `tabModule Def`")
<commit_msg>translate issue<commit_after># Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt

from __future__ import unicode_literals

"""
Contributing:
1. Add the .csv file
2. Run import
3. Then run translate
"""

# loading
# doctype, page, report
# boot(startup)
# frappe.require
# frappe._

import frappe, os, re, codecs, json
from frappe.utils.jinja import render_include
from jinja2 import TemplateError

def guess_language_from_http_header(lang):
	"""set frappe.local.lang from HTTP headers at beginning of request"""
	if not lang:
		return frappe.local.lang

	guess = None
	lang_list = get_all_languages() or []

	if ";" in lang: # not considering weightage
		lang = lang.split(";")[0]
	if "," in lang:
		lang = lang.split(",")
	else:
		lang = [lang]

	for l in lang:
		code = l.strip()
		if code in lang_list:
			guess = code
			break

		# check if parent language (pt) is setup, if variant (pt-BR)
		if "-" in code:
			code = code.split("-")[0]
			if code in lang_list:
				guess = code
				break

	return guess or frappe.local.lang

def get_user_lang(user=None):
	"""set frappe.local.lang from user preferences on session beginning or resumption"""
	if not user:
		user = frappe.session.user

	# via cache
	lang = frappe.cache().get_value("lang:" + user)

	if not lang:

		# if defined in user profile
		user_lang = frappe.db.get_value("User", user, "language")
		if user_lang and user_lang!="Loading...":
			lang = get_lang_dict().get(user_lang)
		else:
			default_lang = frappe.db.get_default("lang")
			lang = default_lang or frappe.local.lang

		frappe.cache().set_value("lang:" + user, lang or "en")

	return lang

def set_default_language(language):
	lang = get_lang_dict()[language] or "en"
	frappe.db.set_default("lang", lang)
	frappe.local.lang = lang

def get_all_languages():
	return [a.split()[0] for a in get_lang_info()]

def get_lang_dict():
	return dict([[a[1], a[0]] for a in [a.split(None, 1) for a in get_lang_info()]])

def get_lang_info():
	return frappe.cache().get_value("langinfo",
		lambda:frappe.get_file_items(os.path.join(frappe.local.sites_path, "languages.txt")))

def get_dict(fortype, name=None):
	fortype = fortype.lower()
	cache = frappe.cache()
	cache_key = "translation_assets:" + frappe.local.lang or "en"
	asset_key = fortype + ":" + (name or "-")
	translation_assets = cache.get_value(cache_key) or {}

	if not asset_key in translation_assets:
		if fortype=="doctype":
			messages = get_messages_from_doctype(name)
		elif fortype=="page":
			messages = get_messages_from_page(name)
		elif fortype=="report":
			messages = get_messages_from_report(name)
		elif fortype=="include":
			messages = get_messages_from_include_files()
		elif fortype=="jsfile":
			messages = get_messages_from_file(name)
		elif fortype=="boot":
			messages = get_messages_from_include_files()
			messages += frappe.db.sql_list("select name from tabDocType")
			messages += frappe.db.sql_list("select name from tabRole")
			messages += frappe.db.sql_list("select name from `tabModule Def`")

{'fraction_non_alphanumeric': 0.1284072249589491, 'fraction_numerical': 0.004597701149425287, 'mean_word_length': 4.487387387387387, 'pattern_counts': {'":': 16, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5131227', 'n_tokens_mistral': 2268, 'n_tokens_neox': 2155, 'n_words': 567}