[build] rewrite to build translations on demand (wip)

This commit is contained in:
Rushabh Mehta 2013-06-17 11:55:29 +05:30
parent c2a2f7f925
commit 84fb8fdead
3 changed files with 95 additions and 58 deletions

View file

@ -23,8 +23,47 @@
from __future__ import unicode_literals
from webnotes.utils.minify import JavascriptMinify
"""
Build the `public` folders and setup languages
"""
import os, sys, webnotes
def bundle(no_compress, cms_make=True):
"""concat / minify js files"""
# build js files
check_public()
check_lang()
bundle = Bundle()
bundle.no_compress = no_compress
bundle.make()
if cms_make:
# build index.html and app.html
from website.helpers.make_web_include_files import make
make()
def watch(no_compress):
"""watch and rebuild if necessary"""
import time
bundle = Bundle()
bundle.no_compress = no_compress
while True:
if bundle.dirty():
bundle.make()
time.sleep(3)
def check_public():
from webnotes.install_lib.setup_public_folder import make
make()
def check_lang():
from webnotes.tranlate import update_translations
update_translations()
class Bundle:
"""
Concatenate, compress and mix (if required) js+css files from build.json
@ -184,32 +223,3 @@ class Bundle:
self.appfiles = appfiles
self.bdata = bdata
def check_public():
from webnotes.install_lib.setup_public_folder import make
make()
def bundle(no_compress, cms_make=True):
"""concat / minify js files"""
# build js files
check_public()
bundle = Bundle()
bundle.no_compress = no_compress
bundle.make()
if cms_make:
# build index.html and app.html
from website.helpers.make_web_include_files import make
make()
def watch(no_compress):
"""watch and rebuild if necessary"""
import time
bundle = Bundle()
bundle.no_compress = no_compress
while True:
if bundle.dirty():
bundle.make()
time.sleep(3)

View file

@ -37,6 +37,53 @@ import re
messages = {}
def translate(lang=None):
languages = [lang]
if lang=="all" or lang==None:
languages = get_all_languages()
print "Extracting messages..."
build_message_files()
export_messages(lang, '_lang_tmp.csv')
for lang in languages:
if lang != "en":
filename = 'app/translations/'+lang+'.csv'
print "For " + lang + ":"
print "Compiling messages in one file..."
print "Translating via Google Translate..."
google_translate(lang, '_lang_tmp.csv', filename)
print "Updating language files..."
import_messages(lang, filename)
print "Deleting temp files..."
os.remove('_lang_tmp.csv')
def get_all_languages():
return [f[:-4] for f in os.listdir("app/translations") if f.endswith(".csv")]
def update_translations():
"""
compare language file timestamps with last updated timestamps in `.wnf-lang-status`
if timestamps are missing / changed, build new `.json` files in the `lang folders`
"""
langstatus = {}
languages = get_all_languages()
if os.path.exists(".wnf-lang-status"):
with open(".erpnext-lang-status", "r") as langstatusfile:
langstatus = eval(langstatusfile.read())
for lang in languages:
filename = 'app/translations/'+lang+'.csv'
if langstatus.get(lang, None)!=os.path.getmtime(filename):
print "Setting up lang files for " + lang + "..."
import_messages(lang, filename)
langstatus[lang] = os.path.getmtime(filename)
with open(".wnf-lang-status", "w") as langstatusfile:
langstatus = langstatusfile.write(str(langstatus))
def build_message_files():
"""build from doctypes, pages, database and framework"""
build_for_pages('lib/core')

38
wnf.py
View file

@ -60,8 +60,8 @@ def pull(remote, branch, build=False):
def rebuild():
# build js / css
from webnotes.utils import bundlejs
bundlejs.bundle(False)
from webnotes import build
build.bundle(False)
def apply_latest_patches():
import webnotes.modules.patch_handler
@ -277,17 +277,17 @@ def run():
# build
if options.build:
from webnotes.utils import bundlejs
from webnotes import build
if options.no_cms:
cms_make = False
else:
cms_make = True
bundlejs.bundle(False, cms_make)
build.bundle(False, cms_make)
return
elif options.watch:
from webnotes.utils import bundlejs
bundlejs.watch(True)
from webnotes import build
build.watch(True)
return
# code replace
@ -477,29 +477,9 @@ def run():
google_translate(*options.google_translate)
elif options.translate:
from webnotes.translate import build_message_files, \
export_messages, google_translate, import_messages
languages = [options.translate]
if options.translate=="all":
from startup import lang_list
languages = lang_list
print "Extracting messages..."
build_message_files()
for lang in languages:
if lang != "en":
filename = 'app/translations/'+lang+'.csv'
print "For " + lang + ":"
print "Compiling messages in one file..."
export_messages(lang, '_lang_tmp.csv')
print "Translating via Google Translate..."
google_translate(lang, '_lang_tmp.csv', filename)
print "Updating language files..."
import_messages(lang, filename)
print "Deleting temp files..."
os.remove('_lang_tmp.csv')
from webnotes.translate import translate
translate(options.translate)
elif options.reset_perms:
for d in webnotes.conn.sql_list("""select name from `tabDocType`
where ifnull(istable, 0)=0 and ifnull(custom, 0)=0"""):