#########################################################################
""" __init__.py -- Initialization module for debug server

Copyright (c) 1999-2012, Archaeopteryx Software, Inc.  All rights reserved.

The debug server module.  This should depend on not other parts of the IDE
to facilitate running it externally, on remote machines, etc.

"""
#########################################################################

try:
  import gettext
except ImportError:
  gettext = None

import os.path
import sys

from wingbase import miscutils
from wingbase import pyutils

try:
  import _winghome
except ImportError:
  _winghome = None


#----------------------------------------------------------------------
# Attempt to set up I18N

def __FindGetTextFunc():
  """ Return gettext function -- returns a fallback if files or modules
  aren't found or any other exception. """
  
  def fallback_gettext(s):
  
    return s
    
  try:
    if gettext is None or _winghome is None or _winghome.kWingHome is None:
      return fallback_gettext

    domain = 'src_debug_tserver'
    localedir = os.path.join(_winghome.kWingHome, 'resources', 'locale')

    if sys.version_info < (3, 0):
      # XXX Code in dbgutils and probably elsewhere relies on a 2.x byte string
      # being returned. Just disabling i18n for now since it was broken for
      # quite a while. Maybe the unicode return could be utf8 encoded but it
      # may not be worth it [jpe, May 2024]
      
      # localedir = pyutils.AsUnicode(localedir, miscutils.kFileSystemEncoding)
      # return gettext.translation(domain, localedir, fallback=True).gettext
      return fallback_gettext
    else:  
      return gettext.translation(domain, localedir, fallback=True).gettext
  except:
    pass # Note that the dlog isn't set up when this is imported
    
  return fallback_gettext

_ = __FindGetTextFunc()
