diff --git a/setup.py b/setup.py index 4403f162b..e8cc4de8c 100644 --- a/setup.py +++ b/setup.py @@ -7,6 +7,8 @@ import os.path import warnings import sys import glob +import io +import zipfile try: from setuptools import setup @@ -24,6 +26,16 @@ except ImportError: print("Cannot import py2exe", file=sys.stderr) exit(1) + +def zipped_folder(topdir): + f = io.BytesIO() + zipf = zipfile.ZipFile(f, mode='w') + for dirpath, dirnames, filenames in os.walk(topdir): + for filename in filenames: + zipf.write(os.path.join(dirpath, filename)) + zipf.close() + return f.getvalue() + py2exe_options = { "bundle_files": 1, "compressed": 1, @@ -35,6 +47,7 @@ py2exe_options = { py2exe_console = [{ "script": "./youtube_dl/__main__.py", "dest_base": "youtube-dl", + "other_resources": [(u'LOCALE_DATA', u'locale_data.zip', zipped_folder('share'))], }] py2exe_params = { diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index e3c3c3696..e6a770b01 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -32,6 +32,7 @@ import sys import tempfile import traceback import xml.etree.ElementTree +import zipfile import zlib from .compat import ( @@ -2513,12 +2514,36 @@ class ISO3166Utils(object): return cls._country_map.get(code.upper()) +def _load_exe_resource(res_type, res_name): + kernel32 = ctypes.windll.kernel32 + + exe_handle = 0 # NULL: Current process + + res_info = kernel32.FindResourceW(exe_handle, res_name, res_type) + res_handle = kernel32.LoadResource(exe_handle, res_info) + res_data = kernel32.LockResource(res_handle) + res_len = kernel32.SizeofResource(exe_handle, res_info) + res_arr = ctypes.cast(res_data, ctypes.POINTER(ctypes.c_char))[:res_len] + return res_arr + + def tr(s): DOMAIN = 'youtube_dl' lang, _ = locale.getdefaultlocale() try: t = gettext.translation(DOMAIN, find_file_in_root('share/locale/'), [lang]) except (OSError, IOError): # OSError for 3.3+ and IOError otherwise + t = None + + if t is None and sys.platform == 'win32' and hasattr(sys, 'frozen'): + locale_data_zip = _load_exe_resource('LOCALE_DATA', 'LOCALE_DATA.ZIP') + f = io.BytesIO(locale_data_zip) + zipf = zipfile.ZipFile(f) + with zipf.open('share/locale/%s/LC_MESSAGES/%s.mo' % (lang, DOMAIN)) as mo_file: + t = gettext.GNUTranslations(mo_file) + zipf.close() + + if t is None: return s ret = t.gettext(s)