def find_trans_keys_for_file(filename): """ Lookup strings to be translated for a given file. This looks for a cache pickle file first and falls back to the slower method of crawling the .pot file if not found """ global _js_trans_keys filename = os.path.realpath(filename).lower() app_prefix = util.get_apps_dir().lower() app_name = None orgfilename = filename if filename.startswith(app_prefix): # if this is for a file defined in an app, then check its local cache file app_name = filename[len(app_prefix):].split(os.path.sep, 2)[1] cachefile = os.path.join(app_prefix, app_name, 'locale', 'messages-filecache.bin') filename = filename[len(app_prefix) + 1:] if app_name in INTERNAL_APPS: cachefile = os.path.join(LOCALE_PATH, 'messages-filecache.bin') else: # else use the system cache file cachefile = os.path.join(LOCALE_PATH, 'messages-filecache.bin') offset = filename.find('search_mrsparkle') if offset >= 0: filename = filename[offset:] else: # we only support dynamic lookups from the web directory currently return [] result = [] filename = filename.replace(os.path.sep, '/') if cachefile in _js_trans_keys: data = _js_trans_keys[cachefile] else: f = None try: f = open(cachefile, 'rb') data = pickle.load(f) except IOError: # fallback to the older/slower scan logger.debug("Failed to find i18n cache file for %s" % (orgfilename, )) return find_trans_keys_for_file_direct(orgfilename) finally: if f: f.close() _js_trans_keys[cachefile] = data if data: return data.get(filename, []) return []
def find_trans_keys_for_file(filename): """ Lookup strings to be translated for a given file. This looks for a cache pickle file first and falls back to the slower method of crawling the .pot file if not found """ global _js_trans_keys filename = os.path.realpath(filename).lower() app_prefix = util.get_apps_dir().lower() app_name = None orgfilename = filename if filename.startswith(app_prefix): # if this is for a file defined in an app, then check its local cache file app_name = filename[len(app_prefix):].split(os.path.sep, 2)[1] cachefile = os.path.join(app_prefix, app_name, 'locale', 'messages-filecache.bin') filename = filename[len(app_prefix)+1:] if app_name in INTERNAL_APPS: cachefile = os.path.join(LOCALE_PATH, 'messages-filecache.bin') else: # else use the system cache file cachefile = os.path.join(LOCALE_PATH, 'messages-filecache.bin') offset = filename.find('search_mrsparkle') if offset >= 0: filename = filename[offset:] else: # we only support dynamic lookups from the web directory currently return [] result = [] filename = filename.replace(os.path.sep, '/') if cachefile in _js_trans_keys: data = _js_trans_keys[cachefile] else: f = None try: f = open(cachefile, 'rb') data = pickle.load(f) except IOError: # fallback to the older/slower scan logger.debug("Failed to find i18n cache file for %s" % (orgfilename,)) return find_trans_keys_for_file_direct(orgfilename) finally: if f: f.close() _js_trans_keys[cachefile] = data if data: return data.get(filename, []) return []
def find_trans_keys_for_file_direct(filename): """ Lookup a list of strings to be translated for a given file This relies on the messages.pot file being correctly marked up with the name of the source file associated with each string to be translated """ filename = os.path.realpath(filename).lower() app_prefix = util.get_apps_dir().lower() app_name = None if filename.startswith(app_prefix): # if this is for a file defined in an app, then check it's local messages.pot file app_name = filename[len(app_prefix):].split(os.path.sep, 2)[1] potfile = os.path.join(app_prefix, app_name, 'locale', 'messages.pot') if app_name in INTERNAL_APPS: potfile = os.path.join(LOCALE_PATH, 'messages.pot') else: # else use the system messages.pot potfile = os.path.join(LOCALE_PATH, 'messages.pot') result = [] try: f = open(potfile, 'r') except IOError: if app_name: logger.debug( "Application %s does not contain a messages.pot file" % app_name) return result else: raise # Can't find the appserver messages.pot file == fatal. matched = False msgid = msgid_plural = None for line in f: line = line.strip() if line[:2] == '#:': fn = line[2:].rsplit(':', 1)[0].strip() # split off the line number try: # strip the relative portion of the filename fn = fn[fn.rindex('../') + 3:] except ValueError: pass # hack for search_mrsparkle directories if fn.startswith('web/'): fn = fn[4:] if msgid: # process the existing entry for the last match result.append((msgid, msgid_plural)) msgid = msgid_plural = None matched = False if not matched: # multiple files may use this string; we only need to know if 1 matched matched = fn.lower() == filename[-len(fn):].replace( os.path.sep, "/") elif matched: if line.startswith('msgid '): msgid = unescape(line[6:]) elif line.startswith('msgid_plural '): msgid_plural = unescape(line[13:]) elif line.startswith('"'): if msgid is not None: msgid = msgid + unescape(line) elif msgid_plural is not None: msgid_plural = msgid_plural + unescape(line) if msgid: result.append((msgid, msgid_plural)) f.close() return result
def find_trans_keys_for_file_direct(filename): """ Lookup a list of strings to be translated for a given file This relies on the messages.pot file being correctly marked up with the name of the source file associated with each string to be translated """ filename = os.path.realpath(filename).lower() app_prefix = util.get_apps_dir().lower() app_name = None if filename.startswith(app_prefix): # if this is for a file defined in an app, then check it's local messages.pot file app_name =filename[len(app_prefix):].split(os.path.sep, 2)[1] potfile = os.path.join(app_prefix, app_name, 'locale', 'messages.pot') if app_name in INTERNAL_APPS: potfile = os.path.join(LOCALE_PATH, 'messages.pot') else: # else use the system messages.pot potfile = os.path.join(LOCALE_PATH, 'messages.pot') result = [] try: f = open(potfile, 'r') except IOError: if app_name: logger.debug("Application %s does not contain a messages.pot file" % app_name) return result else: raise # Can't find the appserver messages.pot file == fatal. matched = False msgid = msgid_plural = None for line in f: line = line.strip() if line[:2] == '#:': fn = line[2:].rsplit(':',1)[0].strip() # split off the line number try: # strip the relative portion of the filename fn = fn[fn.rindex('../')+3:] except ValueError: pass # hack for search_mrsparkle directories if fn.startswith('web/'): fn = fn[4:] if msgid: # process the existing entry for the last match result.append( (msgid, msgid_plural) ) msgid = msgid_plural = None matched = False if not matched: # multiple files may use this string; we only need to know if 1 matched matched = fn.lower() == filename[-len(fn):].replace(os.path.sep, "/") elif matched: if line.startswith('msgid '): msgid = unescape(line[6:]) elif line.startswith('msgid_plural '): msgid_plural = unescape(line[13:]) elif line.startswith('"'): if msgid is not None: msgid = msgid + unescape(line) elif msgid_plural is not None: msgid_plural = msgid_plural + unescape(line) if msgid: result.append( (msgid, msgid_plural) ) f.close() return result