Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added localization framework to mssql-cli #272

Merged
merged 6 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ junit/
.DS_Store

# Translations
*.mo
*.pot

# Django stuff:
Expand Down
40 changes: 40 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import uuid
import platform
import utility
import polib


# Environment variables below allow the build process to use a python interpreter and pip version different
# from the user's PATH. When building our linux packages on various distros, we want to be build machine agnostic, so we
Expand Down Expand Up @@ -169,6 +171,44 @@ def validate_actions(user_actions, valid_targets):
sys.exit(1)


def generate_mo(extraction_target_path, lang_name, trans_mappings, domain, localedir=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should separate out the po and mo generation, but we can do this in another PR.

"""
Extracts strings from 'extraction_target_path', and creates pot, po, mo file with 'trans_mappings' information.\
'extraction_target_path' can be file or directory.
"""
extraction_target_dir = extraction_target_path\
if os.path.isdir(extraction_target_path) else os.path.dirname(extraction_target_path)
localedir = localedir if (not localedir is None) else os.path.join(extraction_target_dir, 'locale')
mo_dir = os.path.join(localedir, lang_name, 'LC_MESSAGES')
create_dir([extraction_target_dir, 'locale', lang_name, 'LC_MESSAGES'])

pot_file = '{0}.pot'.format(os.path.join(localedir, domain))
po_file = '{0}.po'.format(os.path.join(mo_dir, domain))
mo_file = '{0}.mo'.format(os.path.join(mo_dir, domain))

extract_command = "pybabel extract {0} -o {1}".format(extraction_target_path, pot_file)
utility.exec_command(extract_command, extraction_target_dir)

po = polib.pofile(pot_file)
for entry in po:
if (entry.msgid in trans_mappings):
entry.msgstr = trans_mappings[entry.msgid]
po.save(po_file)
po.save_as_mofile(mo_file)
return domain, localedir


def create_dir(path):
curr_path = None
for p in path:
if (curr_path is None):
curr_path = os.path.abspath(p)
else:
curr_path = os.path.join(curr_path, p)
if (not os.path.exists(curr_path)):
os.mkdir(curr_path)


if __name__ == '__main__':
default_actions = ['build', 'unit_test']

Expand Down
Binary file added mssqlcli/locale/ko_KR/LC_MESSAGES/mssql-cli.mo
Binary file not shown.
19 changes: 19 additions & 0 deletions mssqlcli/localized_strings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import gettext
import os
from importlib import import_module

PATH = '{0}.py'.format(os.path.splitext(__file__)[0])
DOMAIN = 'mssql-cli'
LOCALE_DIR = os.path.join(os.path.dirname(__file__), 'locale')
LANGUAGES = None

def translation(domain=DOMAIN, localedir=LOCALE_DIR, languages=None):
geleems marked this conversation as resolved.
Show resolved Hide resolved
languages = languages if (not languages is None) else LANGUAGES
return gettext.translation(domain, localedir, languages, fallback=True)

translation().install()


## Localized Strings
def goodbye():
return _(u'Goodbye!')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a newline?

7 changes: 3 additions & 4 deletions mssqlcli/mssql_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from collections import namedtuple
from time import time


from mssqlcli.config import (
get_casing_file,
config_location,
Expand All @@ -40,17 +39,16 @@
from prompt_toolkit.shortcuts.prompt import PromptSession, CompleteStyle
from prompt_toolkit.completion import DynamicCompleter, ThreadedCompleter
from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode

from prompt_toolkit.document import Document
from prompt_toolkit.filters import HasFocus, IsDone

from prompt_toolkit.lexers import PygmentsLexer
from prompt_toolkit.layout.processors import (ConditionalProcessor,
HighlightMatchingBracketProcessor,
TabsProcessor)
from prompt_toolkit.history import FileHistory
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from pygments.lexers.sql import PostgresLexer
import mssqlcli.localized_strings as localized


# Query tuples are used for maintaining history
Expand Down Expand Up @@ -411,7 +409,8 @@ def run(self):
except EOFError:
self.mssqlcliclient_main.shutdown()
if not self.less_chatty:
print('Goodbye!')
print(localized.goodbye())


def _build_cli(self, history):

Expand Down
3 changes: 1 addition & 2 deletions mssqlcli/mssqlclioptionsparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ def create_parser():
dest=u'multi_subnet_failover',
action=u'store_true',
default=False,
help=u'If application is connecting to AlwaysOn AG on different subnets, setting this provides faster '
u'detection and connection to currently active server.')
help=u'If application is connecting to AlwaysOn AG on different subnets, setting this provides faster detection and connection to currently active server.')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's revert this change as it's not related to loc.


args_parser.add_argument(
u'-a', u'--packet-size',
Expand Down
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ configobj >= 5.0.6
humanize >= 0.5.1
cli_helpers >= 0.2.3, < 1.0.0
mock>=1.0.1
babel>=2.7.0
polib>=1.1.0
18 changes: 18 additions & 0 deletions tests/test_localization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
import unittest
import sys
import mssqlcli.localized_strings as localized


class LocalizationTests(unittest.TestCase):

def test_product(self):
original = self.unicode(localized.goodbye())
localized.translation(languages=['ko']).install()
translated = self.unicode(localized.goodbye())
assert original != translated

def unicode(self, s):
if (sys.version_info.major < 3 and isinstance(s, str)):
return s.decode('utf-8')
return s