Revert "Drop Windows EXE support for gettext"

This reverts commit 995f5036ca0eee575f5a8e15c62766f690ce10e2.
This commit is contained in:
Yen Chi Hsuan 2015-12-13 03:40:09 +08:00
parent d8257c57d8
commit 638a4d8689
2 changed files with 38 additions and 0 deletions

View File

@ -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 = {

View File

@ -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)