def i18n_extract(args, fromCLI): splunk_home = comm.splunk_home params_req = ('app', ) params_opt = () comm.validateArgs(params_req, params_opt, args) app_path = os.path.join(splunk_home, 'etc', 'apps', args['app']) app_locale_path = os.path.join(app_path, 'locale') if not os.path.exists(app_locale_path): os.makedirs(app_locale_path) messages_pot = os.path.join(app_locale_path, 'messages.pot') babel_cfg = os.path.join(app_locale_path, 'babel.cfg') if not os.path.exists(babel_cfg): from splunk.appserver.mrsparkle.lib import i18n mrsparkle = os.path.dirname(os.path.dirname(i18n.__file__)) babel_cfg = os.path.join(mrsparkle, 'locale', 'babel.cfg') args = [ 'extract', '-F', babel_cfg, '-o', messages_pot, '-c', 'TRANS:', app_path ] sys.argv[1:] = args frontend.main()
def i18n_extract(args, fromCLI): splunk_home = comm.splunk_home params_req = ('app',) params_opt = () comm.validateArgs(params_req, params_opt, args) app_path = os.path.join(splunk_home, 'etc', 'apps', args['app']) app_locale_path = os.path.join(app_path, 'locale') if not os.path.exists(app_locale_path): os.makedirs(app_locale_path) messages_pot = os.path.join(app_locale_path, 'messages.pot') babel_cfg = os.path.join(app_locale_path, 'babel.cfg') if not os.path.exists(babel_cfg): from splunk.appserver.mrsparkle.lib import i18n mrsparkle = os.path.dirname(os.path.dirname(i18n.__file__)) babel_cfg = os.path.join(mrsparkle, 'locale', 'babel.cfg') args = [ 'extract', '-F', babel_cfg, '-o', messages_pot, '-c', 'TRANS:', app_path ] sys.argv[1:] = args frontend.main()
def main(): if not os.path.isdir('locale'): script_dir = os.path.dirname(os.path.realpath(__file__)) os.chdir(script_dir) if len(sys.argv) == 1: locale_dir = 'locale' elif len(sys.argv) == 2: locale_dir = sys.argv[1] else: print "Usage: i18n-extract.py [<locale path>]" sys.exit(1) locale_dir = os.path.realpath(locale_dir) splunk_home = os.environ.get('SPLUNK_HOME') if not splunk_home: print "SPLUNK_HOME environment variable was not set!" sys.exit(2) print locale_dir print splunk_home if locale_dir.startswith(splunk_home): strip = splunk_home.replace(os.path.sep, '/') strip = (strip+'/share/splunk', strip, 'etc/apps/') template_dir = os.path.join(splunk_home, 'share/splunk/search_mrsparkle') default_dir = os.path.join(splunk_home, 'etc/system/default') search_app = os.path.join(splunk_home, 'etc/apps/search') launcher_app = os.path.join(splunk_home, 'etc/apps/launcher') getting_started_app = os.path.join(splunk_home, 'etc/apps/gettingstarted') stubby_app = os.path.join(splunk_home, 'etc/apps/stubby') datapreview_app = os.path.join(splunk_home, 'etc/apps/splunk_datapreview') else: # assume this is an extraction from the source tree strip = ('../../../../web', '../../../../', 'cfg/bundles/') template_dir = '../../../../web/search_mrsparkle' default_dir = '../../../../cfg/bundles/default' search_app = '../../../../cfg/bundles/search' launcher_app = '../../../../cfg/bundles/launcher' getting_started_app = '../../../../cfg/bundles/gettingstarted' stubby_app = '../../../../cfg/bundles/stubby' datapreview_app = '../../../../cfg/bundles/splunk_datapreview' # this is always relative to the script directory search_helper_dir = '../../searchhelp' args = [ 'extract', '-F', os.path.join(locale_dir, 'babel.cfg'), '-c', 'TRANS:', '-k', 'deferred_ugettext', '-k', 'deferred_ungettext', '.', template_dir, default_dir, search_app, launcher_app, getting_started_app, datapreview_app #stubby_app ] sys.argv[1:] = args # Open the .pot file for write outfile = open(os.path.join(locale_dir, 'messages.pot'), 'w') # Capture Babel's stdout so we can translate the absolute pathnames into relative ones buf = StringIO() stdout_org = sys.stdout sys.stdout = buf # Do the extraction frontend.main() # restore stdout sys.stdout = stdout_org # Start reading the captured data from the top buf.reset() print >>outfile, HEADER currentfn = [] filemapping = {} msgid = msgid_plural = None for line in buf: line = line.strip() if line.startswith('# '): # strip the original comment header continue if line[:3] != '#: ': # filename:linenum references begin with #: print >>outfile, line if currentfn: # capture the translation associated with the current filename(s) if line.startswith('msgid '): msgid = unescape(line[6:]) elif line.startswith('msgid_plural '): msgid_plural = unescape(line[13:]) elif line.startswith('"'): # multi-line translation if msgid is not None: msgid = msgid + unescape(line) elif msgid_plural is not None: msgid_plural = msgid_plural + unescape(line) continue if msgid and currentfn: for fn in currentfn: fn = fn.lower() if fn.find('data' + os.sep + 'ui') > -1: fn = os.path.splitext(os.path.basename(fn))[0] filemapping.setdefault(fn, []).append( (msgid, msgid_plural) ) msgid = msgid_plural = None currentfn = [] newline = '#: ' fnpairs = line[3:].split(' ') for fnpair in fnpairs: fn, ln = fnpair.rsplit(':', 1) for prefix in strip: if fn.startswith(prefix): fn = fn[len(prefix):].strip('/') # keep track of js files if fn.endswith('.js') or fn.find('data' + os.sep + 'ui') > -1: currentfn.append(fn) newline += "%s:%s " % (fn, ln) print >>outfile, newline outfile.close() # collect the final message if msgid and currentfn: for fn in currentfn: if fn.find('data' + os.sep + 'ui') > -1: fn = os.path.splitext(os.path.basename(fn))[0] filemapping.setdefault(fn.lower(), []).append( (msgid, msgid_plural) ) # pickle the lookup data cachefile = open(os.path.join(locale_dir, "messages-filecache.bin"), 'wb') pickle.dump(filemapping, cachefile, 2) cachefile.close()
if __name__ == '__main__': import sys from babel.messages.frontend import main sys.exit(main())
def main(): if not os.path.isdir('locale'): script_dir = os.path.dirname(os.path.realpath(__file__)) os.chdir(script_dir) if len(sys.argv) == 1: locale_dir = 'locale' elif len(sys.argv) == 2: locale_dir = sys.argv[1] else: print "Usage: i18n-extract.py [<locale path>]" sys.exit(1) locale_dir = os.path.realpath(locale_dir) splunk_home = os.environ.get('SPLUNK_HOME') if not splunk_home: print "SPLUNK_HOME environment variable was not set!" sys.exit(2) print locale_dir print splunk_home if locale_dir.startswith(splunk_home): strip = splunk_home.replace(os.path.sep, '/') strip = (strip+'/share/splunk', strip, 'etc/apps/') template_dir = os.path.join(splunk_home, 'share/splunk/search_mrsparkle/templates') js_dir = os.path.join(splunk_home, 'share/splunk/search_mrsparkle/exposed/js') modules_dir = os.path.join(splunk_home, 'share/splunk/search_mrsparkle/modules') default_dir = os.path.join(splunk_home, 'etc/system/default') search_app = os.path.join(splunk_home, 'etc/apps/search') launcher_app = os.path.join(splunk_home, 'etc/apps/launcher') getting_started_app = os.path.join(splunk_home, 'etc/apps/gettingstarted') deployment_app = os.path.join(splunk_home, 'etc/apps/SplunkDeploymentMonitor') stubby_app = os.path.join(splunk_home, 'etc/apps/stubby') else: # assume this is an extraction from the source tree strip = ('../../../../web', '../../../../', 'cfg/bundles/') template_dir = '../../../../web/search_mrsparkle/templates' js_dir = '../../../../web/search_mrsparkle/exposed/js' modules_dir = '../../../../web/search_mrsparkle/modules' default_dir = '../../../../cfg/bundles/default' search_app = '../../../../cfg/bundles/search' launcher_app = '../../../../cfg/bundles/launcher' getting_started_app = '../../../../cfg/bundles/gettingstarted' deployment_app = '../../../../cfg/bundles/SplunkDeploymentMonitor' stubby_app = '../../../../cfg/bundles/stubby' # this is always relative to the script directory search_helper_dir = '../../searchhelp' args = [ 'extract', '-F', os.path.join(locale_dir, 'babel.cfg'), '-c', 'TRANS:', '-k', 'deferred_ugettext', '-k', 'deferred_ungettext', '.', template_dir, js_dir, modules_dir, default_dir, search_app, launcher_app, getting_started_app, deployment_app #stubby_app ] sys.argv[1:] = args # Open the .pot file for write outfile = open(os.path.join(locale_dir, 'messages.pot'), 'w') # Capture Babel's stdout so we can translate the absolute pathnames into relative ones buf = StringIO() stdout_org = sys.stdout sys.stdout = buf # Do the extraction frontend.main() # restore stdout sys.stdout = stdout_org # Start reading the captured data from the top buf.reset() print >>outfile, HEADER currentfn = [] filemapping = {} msgid = msgid_plural = None for line in buf: line = line.strip() if line.startswith('# '): # strip the original comment header continue if line[:3] != '#: ': # filename:linenum references begin with #: print >>outfile, line if currentfn: # capture the translation associated with the current filename(s) if line.startswith('msgid '): msgid = unescape(line[6:]) elif line.startswith('msgid_plural '): msgid_plural = unescape(line[13:]) elif line.startswith('"'): # multi-line translation if msgid is not None: msgid = msgid + unescape(line) elif msgid_plural is not None: msgid_plural = msgid_plural + unescape(line) continue if msgid and currentfn: for fn in currentfn: filemapping.setdefault(fn.lower(), []).append( (msgid, msgid_plural) ) msgid = msgid_plural = None currentfn = [] newline = '#: ' fnpairs = line[3:].split(' ') for fnpair in fnpairs: fn, ln = fnpair.rsplit(':', 1) for prefix in strip: if fn.startswith(prefix): fn = fn[len(prefix):].strip('/') # keep track of js files if fn.endswith('.js'): currentfn.append(fn) newline += "%s:%s " % (fn, ln) print >>outfile, newline outfile.close() # collect the final message if msgid and currentfn: for fn in currentfn: filemapping.setdefault(fn.lower(), []).append( (msgid, msgid_plural) ) # pickle the lookup data cachefile = open(os.path.join(locale_dir, "messages-filecache.bin"), 'wb') pickle.dump(filemapping, cachefile, 2) cachefile.close()