#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) 2015-2017:
#   Matthieu Estrada, ttamalfor@gmail.com
#
# This file is part of (AlignakApp).
#
# (AlignakApp) is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# (AlignakApp) is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with (AlignakApp).  If not, see <http://www.gnu.org/licenses/>.

"""
    Locales manage localization of Alignak-app
"""

import os
import sys

from logging import getLogger
from gettext import GNUTranslations, NullTranslations

from alignak_app.core.utils import get_app_config, get_main_folder

logger = getLogger(__name__)


def init_localization():
    """
    Application localization

    :return: gettext translator method
    :rtype: method
    """
    try:
        # Language message file
        if 'win32' not in sys.platform:
            lang_filename = os.path.join(
                os.path.abspath(os.path.dirname(__file__)),
                "../locales/%s.mo" % get_app_config('Config', 'locale')
            )
        else:
            lang_filename = get_main_folder() + \
                "\locales\%s.mo" % get_app_config('Config', 'locale')
        logger.info(
            "Opening message file %s for locale %s",
            lang_filename, get_app_config('Config', 'locale')
        )
        translation = GNUTranslations(open(lang_filename, "rb"))
        translation.install()
        _ = translation.gettext
    except IOError:
        logger.error("Locale not found. Using default language messages (English)")
        null_translation = NullTranslations()
        null_translation.install()
        _ = null_translation.gettext
    except Exception as e:  # pragma: no cover - should not happen
        logger.error("Locale not found. Exception: %s", str(e))
        null_translation = NullTranslations()
        null_translation.install()
        _ = null_translation.gettext

    return _

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) 2015-2017:
#   Matthieu Estrada, ttamalfor@gmail.com
#
# This file is part of (AlignakApp).
#
# (AlignakApp) is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# (AlignakApp) is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with (AlignakApp).  If not, see <http://www.gnu.org/licenses/>.

"""
    Locales manage localization of Alignak-app
"""

import os
import sys

from logging import getLogger
from gettext import GNUTranslations, NullTranslations

from alignak_app.core.utils import get_app_config, get_main_folder

logger = getLogger(__name__)


def init_localization():
    """
    Application localization

    :return: gettext translator method
    :rtype: method
    """
    try:
        # Language message file
        if 'win32' not in sys.platform:
            lang_filename = os.path.join(
                os.path.abspath(os.path.dirname(__file__)),
                "../locales/%s.mo" % get_app_config('Config', 'locale')
            )
        else:
            lang_filename = get_main_folder() + "\\locales\\%s.mo" % get_app_config(
                'Config', 'locale'
            )
        logger.info(
            "Opening message file %s for locale %s",
            lang_filename, get_app_config('Config', 'locale')
        )
        translation = GNUTranslations(open(lang_filename, "rb"))
        translation.install()
        _ = translation.gettext
    except IOError:
        logger.error("Locale not found. Using default language messages (English)")
        null_translation = NullTranslations()
        null_translation.install()
        _ = null_translation.gettext
    except Exception as e:  # pragma: no cover - should not happen
        logger.error("Locale not found. Exception: %s", str(e))
        null_translation = NullTranslations()
        null_translation.install()
        _ = null_translation.gettext

    return _

Fix pylint anomalous-backslash-in-string