Fixed #0000654 - Startpage translation

This commit is contained in:
Yorik van Havre 2012-03-27 12:36:11 -03:00
parent 0f6d6d4b37
commit 54f8375cde

View File

@ -1,4 +1,4 @@
import os,FreeCAD,FreeCADGui,tempfile,time,zipfile,urllib,re
import os,FreeCAD,FreeCADGui,tempfile,time,zipfile,urllib,re,cStringIO
from PyQt4 import QtGui
from xml.etree.ElementTree import parse
@ -6,8 +6,19 @@ FreeCADGui.addLanguagePath(":/translations")
FreeCADGui.updateLocale()
def translate(context,text):
"convenience function for the Qt translator"
return str(QtGui.QApplication.translate(context, text, None, QtGui.QApplication.UnicodeUTF8).toUtf8())
"convenience function for the Qt translator"
# return str(QtGui.QApplication.translate(context, text, None, QtGui.QApplication.UnicodeUTF8).toUtf8())
u = QtGui.QApplication.translate(context, text, None,
QtGui.QApplication.UnicodeUTF8).toUtf8()
s = cStringIO.StringIO()
for i in u:
if ord(i) == 39:
s.write("\\'")
else:
s.write(i)
t = s.getvalue()
s.close()
return t
# texts to be translated
@ -66,144 +77,164 @@ page = """
<html>
<head>
<title>FreeCAD - Start page</title>
<script language="javascript">
function JSONscriptRequest(fullUrl) {
// REST request path
this.fullUrl = fullUrl;
// Get the DOM location to put the script tag
this.headLoc = document.getElementsByTagName("head").item(0);
// Generate a unique script tag id
this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
}
// Static script ID counter
JSONscriptRequest.scriptCounter = 1;
function JSONscriptRequest(fullUrl) {
// REST request path
this.fullUrl = fullUrl;
// Get the DOM location to put the script tag
this.headLoc = document.getElementsByTagName("head").item(0);
// Generate a unique script tag id
this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
}
JSONscriptRequest.prototype.buildScriptTag = function () {
// Create the script tag
this.scriptObj = document.createElement("script");
// Add script object attributes
this.scriptObj.setAttribute("type", "text/javascript");
this.scriptObj.setAttribute("charset", "utf-8");
this.scriptObj.setAttribute("src", this.fullUrl);
this.scriptObj.setAttribute("id", this.scriptId);
}
// Static script ID counter
JSONscriptRequest.scriptCounter = 1;
JSONscriptRequest.prototype.buildScriptTag = function () {
// Create the script tag
this.scriptObj = document.createElement("script");
// Add script object attributes
this.scriptObj.setAttribute("type", "text/javascript");
this.scriptObj.setAttribute("charset", "utf-8");
this.scriptObj.setAttribute("src", this.fullUrl);
this.scriptObj.setAttribute("id", this.scriptId);
}
JSONscriptRequest.prototype.removeScriptTag = function () {
// Destroy the script tag
this.headLoc.removeChild(this.scriptObj);
}
JSONscriptRequest.prototype.addScriptTag = function () {
// Create the script tag
this.headLoc.appendChild(this.scriptObj);
}
function show(theText) {
ddiv = document.getElementById("description");
if (theText == "") theText = "&nbsp;";
ddiv.innerHTML = theText;
}
function loadFeeds() {
ddiv = document.getElementById("youtube");
ddiv.innerHTML = "Fetching data from the web...";
var obj=new JSONscriptRequest('http://gdata.youtube.com/feeds/base/users/FreeCADNews/favorites?alt=json-in-script&v=2&orderby=published&callback=showLinks');
obj.buildScriptTag(); // Build the script tag
obj.addScriptTag(); // Execute (add) the script tag
ddiv.innerHTML = "Done fetching";
ddiv = document.getElementById("news");
ddiv.innerHTML = "Fetching data from the web...";
var tobj=new JSONscriptRequest('http://twitter.com/status/user_timeline/FreeCADNews.json?count=10&callback=showTweets');
tobj.buildScriptTag(); // Build the script tag
tobj.addScriptTag(); // Execute (add) the script tag
ddiv.innerHTML = "Done fetching";
}
function showLinks(data) {
ddiv = document.getElementById('youtube');
ddiv.innerHTML = "Received";
var feed = data.feed;
var entries = feed.entry || [];
var html = ['<ul>'];
for (var i = 0; i < 5; i++) {
html.push('<li><a href="',entries[i].link[0].href,'">', entries[i].title.$t, '</a></li>');
JSONscriptRequest.prototype.removeScriptTag = function () {
// Destroy the script tag
this.headLoc.removeChild(this.scriptObj);
}
html.push('</ul>');
ddiv.innerHTML = html.join('');
}
function showTweets(data) {
ddiv = document.getElementById('news');
ddiv.innerHTML = "Received";
var html = ['<ul>'];
for (var i = 0; i < Math.min(5,data.length); i++) {
tf = placeLinks(data[i].text);
html.push('<li>',tf,'</li>');
JSONscriptRequest.prototype.addScriptTag = function () {
// Create the script tag
this.headLoc.appendChild(this.scriptObj);
}
html.push('</ul>');
ddiv.innerHTML = html.join('');
}
function placeLinks(text) {
result=text.replace(/(https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?)/g,'<a href="$1">$1</a>');
return result;
}
function show(theText) {
ddiv = document.getElementById("description");
if (theText == "") theText = "&nbsp;";
ddiv.innerHTML = theText;
}
function loadFeeds() {
ddiv = document.getElementById("youtube");
ddiv.innerHTML = "Fetching data from the web...";
var obj=new JSONscriptRequest('http://gdata.youtube.com/feeds/base/users/FreeCADNews/favorites?alt=json-in-script&v=2&orderby=published&callback=showLinks');
obj.buildScriptTag(); // Build the script tag
obj.addScriptTag(); // Execute (add) the script tag
ddiv.innerHTML = "Done fetching";
ddiv = document.getElementById("news");
ddiv.innerHTML = "Fetching data from the web...";
var tobj=new JSONscriptRequest('http://twitter.com/status/user_timeline/FreeCADNews.json?count=10&callback=showTweets');
tobj.buildScriptTag(); // Build the script tag
tobj.addScriptTag(); // Execute (add) the script tag
ddiv.innerHTML = "Done fetching";
}
function showLinks(data) {
ddiv = document.getElementById('youtube');
ddiv.innerHTML = "Received";
var feed = data.feed;
var entries = feed.entry || [];
var html = ['<ul>'];
for (var i = 0; i < 5; i++) {
html.push('<li><a href="',entries[i].link[0].href,'">', entries[i].title.$t, '</a></li>');
}
html.push('</ul>');
ddiv.innerHTML = html.join('');
}
function showTweets(data) {
ddiv = document.getElementById('news');
ddiv.innerHTML = "Received";
var html = ['<ul>'];
for (var i = 0; i < Math.min(5,data.length); i++) {
tf = placeLinks(data[i].text);
html.push('<li>',tf,'</li>');
}
html.push('</ul>');
ddiv.innerHTML = html.join('');
}
function placeLinks(text) {
result=text.replace(/(https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?)/g,'<a href="$1">$1</a>');
return result;
}
</script>
<style type="text/css">
body {
background: #171A2B url(Background.jpg);
color: white;
font-family: Arial, Helvetica, Sans;
font-size: 11px;
}
a {
color: #0092E8;
font-weight: bold;
text-decoration: none;
padding: 2px;
}
a:hover {
color: white;
background: #0092E8;
border-radius: 5px;
}
p {
text-align: justify;
}
.left {
text-align: left;
}
h1 {
font-size: 3em;
letter-spacing: 2px;
padding: 20px 0 0 80px;
align: bottom;
}
h2 {
font-size: 1.2em;
}
ul {
list-style-type: none;
padding: 0;
}
.column {
width: 300px;
float: left;
margin-left: 10px;
}
.block {
background: rgba(30,31,33,0.6);;
border-radius: 5px;
padding: 8px;
margin-bottom: 10px;
}
.options {
clear: both;
}
.from {
font-size: 0.7em;
font-weight: normal;
}
body {
background: #171A2B url(Background.jpg);
color: white;
font-family: Arial, Helvetica, Sans;
font-size: 11px;
}
a {
color: #0092E8;
font-weight: bold;
text-decoration: none;
padding: 2px;
}
a:hover {
color: white;
background: #0092E8;
border-radius: 5px;
}
p {
text-align: justify;
}
.left {
text-align: left;
}
h1 {
font-size: 3em;
letter-spacing: 2px;
padding: 20px 0 0 80px;
align: bottom;
}
h2 {
font-size: 1.2em;
}
ul {
list-style-type: none;
padding: 0;
}
.column {
width: 300px;
float: left;
margin-left: 10px;
}
.block {
background: rgba(30,31,33,0.6);;
border-radius: 5px;
padding: 8px;
margin-bottom: 10px;
}
.options {
clear: both;
}
.from {
font-size: 0.7em;
font-weight: normal;
}
</style>
</head>
<body onload="loadFeeds()">
@ -269,204 +300,202 @@ page = """
"""
def getWebExamples():
return """
<ul>
<li><a href="http://freecad-project.de/svn/ExampleData/FileFormates/Schenkel.stp">""" + text15 + """</a></li>
<li><a href="http://freecad-project.de/svn/ExampleData/Examples/CAD/Complex.FCStd">""" + text16 + """</a></li>
</ul>"""
return """
<ul>
<li><a href="http://freecad-project.de/svn/ExampleData/FileFormates/Schenkel.stp">""" + text15 + """</a></li>
<li><a href="http://freecad-project.de/svn/ExampleData/Examples/CAD/Complex.FCStd">""" + text16 + """</a></li>
</ul>"""
def getExamples():
return """
<ul>
<li><a href="LoadSchenkel.py">""" + text10 + """</a></li>
<li><a href="LoadPartDesignExample.py">""" + text11 + """</a></li>
<li><a href="LoadDrawingExample.py">""" + text12 + """</a></li>
<li><a href="LoadRobotExample.py">""" + text13 + """</a></li>
</ul>"""
return """
<ul>
<li><a href="LoadSchenkel.py">""" + text10 + """</a></li>
<li><a href="LoadPartDesignExample.py">""" + text11 + """</a></li>
<li><a href="LoadDrawingExample.py">""" + text12 + """</a></li>
<li><a href="LoadRobotExample.py">""" + text13 + """</a></li>
</ul>"""
def getLinks():
return """
<ul>
<li><img src="web.png">&nbsp;
<a onMouseover="show('<p>""" + text07 + """</p>')"
onMouseout="show('')"
href="http://free-cad.sf.net/">""" + text08 + """</a></li>
<li><img src="web.png">&nbsp;
<a onMouseover="show('<p>""" + text45 + """</p>')"
onMouseout="show('')"
href=""" + text38 + """>""" + text37 + """</a></li>
<li><img src="web.png">&nbsp;
<a onMouseover="show('<p>""" + text46 + """</p>')"
onMouseout="show('')"
href="http://sourceforge.net/apps/mediawiki/free-cad/index.php?title=Tutorials">""" + text39 + """</a></li>
<li><img src="web.png">&nbsp;
<a onMouseover="show('<p>""" + text47 + """</p>')"
onMouseout="show('')"
href="http://sourceforge.net/apps/mediawiki/free-cad/index.php?title=Power_users_hub">""" + text40 + """</a></li>
<li><img src="web.png">&nbsp;
<a onMouseover="show('<p>""" + text48 + """</p>')"
onMouseout="show('')"
href="http://freecad-tutorial.blogspot.com/">""" + text43 + """</a></li>
</ul>"""
return """
<ul>
<li><img src="web.png">&nbsp;
<a onMouseover="show('<p>""" + text07 + """</p>')"
onMouseout="show('')"
href="http://free-cad.sf.net/">""" + text08 + """</a></li>
<li><img src="web.png">&nbsp;
<a onMouseover="show('<p>""" + text45 + """</p>')"
onMouseout="show('')"
href=""" + text38 + """>""" + text37 + """</a></li>
<li><img src="web.png">&nbsp;
<a onMouseover="show('<p>""" + text46 + """</p>')"
onMouseout="show('')"
href="http://sourceforge.net/apps/mediawiki/free-cad/index.php?title=Tutorials">""" + text39 + """</a></li>
<li><img src="web.png">&nbsp;
<a onMouseover="show('<p>""" + text47 + """</p>')"
onMouseout="show('')"
href="http://sourceforge.net/apps/mediawiki/free-cad/index.php?title=Power_users_hub">""" + text40 + """</a></li>
<li><img src="web.png">&nbsp;
<a onMouseover="show('<p>""" + text48 + """</p>')"
onMouseout="show('')"
href="http://freecad-tutorial.blogspot.com/">""" + text43 + """</a></li>
</ul>"""
def getWorkbenches():
return """
<ul>
<li><img src="PartDesign.png">&nbsp;
<a onMouseover="show('<h3>""" + text19 + """</h3> \
<p>""" + text20 + """</p><p><small>""" + text21 + """ \
:</small></p><img src=PartDesignExample.png>')"
onMouseout="show('')"
href="PartDesign.py">""" + text22 + """</a></li>
<li><img src="ArchDesign.png">&nbsp;
<a onMouseover="show('<h3>""" + text23 + """</h3> \
<p>""" + text24 + """</p><p><small>""" + text21 + """ \
:</small></p><img src=ArchExample.png>')"
onMouseout="show('')"
href="ArchDesign.py">""" + text25 + """</a></li>
<li><img src="Mesh.png">&nbsp;
<a onMouseover="show('<h3>""" + text26 + """</h3> \
<p>""" + text27 + """</p><p>""" + text28 + """</p>')"
onMouseout="show('')"
href="Mesh.py">""" + text29 + """</a></li>
<li><img src="Complete.png">&nbsp;
<a onMouseover="show('<h3>""" + text30 +"""</h3> \
<p>This is the <b>""" + text31 + """</b>, \
""" + text32 + """</p>')"
onMouseout="show('')"
href="DefaultWorkbench.py">""" + text31 + """</a></li>
</ul>
"""
return """
<ul>
<li><img src="PartDesign.png">&nbsp;
<a onMouseover="show('<h3>""" + text19 + """</h3> \
<p>""" + text20 + """</p><p><small>""" + text21 + """ \
:</small></p><img src=PartDesignExample.png>')"
onMouseout="show('')"
href="PartDesign.py">""" + text22 + """</a>
</li>
<li><img src="ArchDesign.png">&nbsp;
<a onMouseover="show('<h3>""" + text23 + """</h3> \
<p>""" + text24 + """</p><p><small>""" + text21 + """ \
:</small></p><img src=ArchExample.png>')"
onMouseout="show('')"
href="ArchDesign.py">""" + text25 + """</a>
</li>
<li><img src="Mesh.png">&nbsp;
<a onMouseover="show('<h3>""" + text26 + """</h3> \
<p>""" + text27 + """</p><p>""" + text28 + """</p>')"
onMouseout="show('')"
href="Mesh.py">""" + text29 + """</a>
</li>
<li><img src="Complete.png">&nbsp;
<a onMouseover="show('<h3>""" + text30 +"""</h3> \
<p>This is the <b>""" + text31 + """</b>, \
""" + text32 + """</p>')"
onMouseout="show('')"
href="DefaultWorkbench.py">""" + text31 + """</a>
</li>
</ul>"""
def getInfo(filename):
"returns available file information"
"returns available file information"
def getLocalTime(timestamp):
"returns a local time from a timestamp"
return time.strftime("%m/%d/%Y %H:%M:%S",time.localtime(timestamp))
def getLocalTime(timestamp):
"returns a local time from a timestamp"
return time.strftime("%m/%d/%Y %H:%M:%S",time.localtime(timestamp))
def getSize(size):
"returns a human-readable size"
if size > 1024*1024:
hsize = str(size/(1024*1024)) + "Mb"
elif size > 1024:
hsize = str(size/1024) + "Kb"
else:
hsize = str(size) + "b"
return hsize
html = '<h3>'+os.path.basename(filename)+'</h3>'
if os.path.exists(filename):
# get normal file info
s = os.stat(filename)
html += "<p>" + text33 + " " + getSize(s.st_size) + "<br/>"
html += text34 + " " + getLocalTime(s.st_ctime) + "<br/>"
html += text35 + " " + getLocalTime(s.st_mtime) + "<br/>"
html += "<span>" + text36 + " " + filename + "</span></p>"
# get additional info from fcstd files
if os.path.splitext(filename)[1] in [".fcstd",".FcStd"]:
zfile=zipfile.ZipFile(filename)
files=zfile.namelist()
# check for meta-file if it's really a FreeCAD document
if files[0] == "Document.xml":
html += "<p>FreeCAD Standard File</p>"
image="thumbnails/Thumbnail.png"
if image in files:
image=zfile.read(image)
thumbfile = tempfile.mkstemp(suffix='.png')[1]
thumb = open(thumbfile,"wb")
thumb.write(image)
thumb.close()
html += '<img src=file://'
html += thumbfile + '><br/>'
def getSize(size):
"returns a human-readable size"
if size > 1024*1024:
hsize = str(size/(1024*1024)) + "Mb"
elif size > 1024:
hsize = str(size/1024) + "Kb"
else:
html += "<p>" + text41 + "</p>"
return html
hsize = str(size) + "b"
return hsize
html = '<h3>'+os.path.basename(filename)+'</h3>'
if os.path.exists(filename):
# get normal file info
s = os.stat(filename)
html += "<p>" + text33 + " " + getSize(s.st_size) + "<br/>"
html += text34 + " " + getLocalTime(s.st_ctime) + "<br/>"
html += text35 + " " + getLocalTime(s.st_mtime) + "<br/>"
html += "<span>" + text36 + " " + filename + "</span></p>"
# get additional info from fcstd files
if os.path.splitext(filename)[1] in [".fcstd",".FcStd"]:
zfile=zipfile.ZipFile(filename)
files=zfile.namelist()
# check for meta-file if it's really a FreeCAD document
if files[0] == "Document.xml":
html += "<p>FreeCAD Standard File</p>"
image="thumbnails/Thumbnail.png"
if image in files:
image=zfile.read(image)
thumbfile = tempfile.mkstemp(suffix='.png')[1]
thumb = open(thumbfile,"wb")
thumb.write(image)
thumb.close()
html += '<img src=file://'
html += thumbfile + '><br/>'
else:
html += "<p>" + text41 + "</p>"
return html
def getRecentFiles():
"returns a list of 3 latest recent files"
rf=FreeCAD.ParamGet("User parameter:BaseApp/Preferences/RecentFiles")
ct=rf.GetInt("RecentFiles")
html = '<ul>'
for i in range(3):
if i < ct:
mr = rf.GetString("MRU%d" % (i))
fn = os.path.basename(mr)
html += '<li><a '
html += 'onMouseover="show(\''+getInfo(mr)+'\')" '
html += 'onMouseout="show(\'\')" '
html += 'href="LoadMRU'+str(i)+'.py">'
html += fn
html += '</a></li>'
html += '</ul>'
return html
"returns a list of 3 latest recent files"
rf = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/RecentFiles")
ct = rf.GetInt("RecentFiles")
html = '<ul>'
for i in range(3):
if i < ct:
mr = rf.GetString("MRU%d" % (i))
fn = os.path.basename(mr)
html += '<li><a '
html += 'onMouseover="show(\''+getInfo(mr)+'\')" '
html += 'onMouseout="show(\'\')" '
html += 'href="LoadMRU'+str(i)+'.py">'
html += fn
html += '</a></li>'
html += '</ul>'
return html
def getFeed(url,numitems=3):
"returns a html list with links from the given RSS feed url"
xml = parse(urllib.urlopen(url)).getroot()
items = []
channel = xml.find('channel')
for element in channel.findall('item'):
items.append({
'title': element.find('title').text,
'description': element.find('description').text,
'link': element.find('link').text,
})
if len(items) > numitems:
items = items[:numitems]
resp = '<ul>'
for item in items:
descr = re.compile("style=\".*?\"").sub('',item['description'])
descr = re.compile("alt=\".*?\"").sub('',descr)
descr = re.compile("\"").sub('',descr)
d1 = re.findall("<img.*?>",descr)[0]
d2 = re.findall("<span>.*?</span>",descr)[0]
descr = "<h3>" + item['title'] + "</h3>"
descr += d1 + "<br/>"
descr += d2
resp += '<li><a onMouseover="show(\''
resp += descr
resp += '\')" onMouseout="show(\'\')" href="'
resp += item['link']
resp += '">'
resp += item['title']
resp += '</a></li>'
resp += '</ul>'
print resp
return resp
"returns a html list with links from the given RSS feed url"
xml = parse(urllib.urlopen(url)).getroot()
items = []
channel = xml.find('channel')
for element in channel.findall('item'):
items.append({'title': element.find('title').text,
'description': element.find('description').text,
'link': element.find('link').text})
if len(items) > numitems:
items = items[:numitems]
resp = '<ul>'
for item in items:
descr = re.compile("style=\".*?\"").sub('',item['description'])
descr = re.compile("alt=\".*?\"").sub('',descr)
descr = re.compile("\"").sub('',descr)
d1 = re.findall("<img.*?>",descr)[0]
d2 = re.findall("<span>.*?</span>",descr)[0]
descr = "<h3>" + item['title'] + "</h3>"
descr += d1 + "<br/>"
descr += d2
resp += '<li><a onMouseover="show(\''
resp += descr
resp += '\')" onMouseout="show(\'\')" href="'
resp += item['link']
resp += '">'
resp += item['title']
resp += '</a></li>'
resp += '</ul>'
print resp
return resp
def getCustomBlocks():
"fetches custom html files in FreeCAD user dir"
output = ""
return output
"fetches custom html files in FreeCAD user dir"
output = ""
return output
def handle():
"returns the complete html startpage"
# add recent files
recentfiles = getRecentFiles()
html = page.replace("recentfiles",recentfiles)
"returns the complete html startpage"
# add recent files
recentfiles = getRecentFiles()
html = page.replace("recentfiles",recentfiles)
# add default workbenches
html = html.replace("defaultworkbenches",getWorkbenches())
# add default workbenches
html = html.replace("defaultworkbenches",getWorkbenches())
# add default web links
html = html.replace("defaultlinks",getLinks())
# add default web links
html = html.replace("defaultlinks",getLinks())
# add default examples
html = html.replace("defaultexamples",getExamples())
# add default examples
html = html.replace("defaultexamples",getExamples())
# add web examples
#html = html.replace("webexamples",getWebExamples())
# add web examples
#html = html.replace("webexamples",getWebExamples())
# add custom blocks
html = html.replace("customblocks",getCustomBlocks())
return html
# add custom blocks
html = html.replace("customblocks",getCustomBlocks())
return html