Repository: MycroftAI/mycroft-core

Issue:
remove hardcoded en-us from CLI
When asking for confirmation with a yes-no question
(ask_yesno in mycroft/skills/core.py) mycroft expects the answer to be either yes or no, independent of the language.

So in any other language other than English, ask yes/no does not work.

## Try to provide steps that we can use to replicate the Issue

  * Set language to any other language other than English.
  * Start an alarm
  * try to cancel the alarm.
  * Answer in your native language to yes/no, instead of yes/no

## Be as specific as possible about the expected condition, and the deviation from expected condition. 

either ask_yesno should translate the native yes/no like (ja/nein), (si/no) to English and return yes/no, or it should return true for yes and false for no.


## Provide log files or other output to help us see the error
Should be attached. Output of
```
cat /var/log/mycroft/skills.log | grep -v urllib3 | grep -v "TimeSkillupdate_display" | grep -v UpdateRemote
```


Discussion:
[logfile.txt](https://github.com/MycroftAI/mycroft-core/files/2710894/logfile.txt)

Is \mycroft\res\text\XX-XX\yes.voc and no.voc localized?  Also, from the log it looks like your language is still set to English  -- see all of the "lang": "en-us" 
Note -- inside the _code_ it will return the values 'yes' for the programming logic, regardless of the localization setting.  That makes writing the code easy, such as

```
   if self.ask_yesno("question") is "yes":
       # handle the 'yes' response, even if the spoken response was "ja" or "si'
```
Thanks for the respond. Did not know about the res/text/XX-XX files. Definitely good to know, but yes, they are localized.

I found out, why there is so much en-us in my logs.
I switched between typing in commands and using the mic, as I was testing some stuff and most of the time it is more convenient to use the command line.

Most plausible reason for my bug above: The text client does not respect the users choice of language.
Found in mycroft/client/text/text_client.py:1261
In function gui_main:

Whenever something is typed on the command line, the GUI Main emits a message to the bus with the language hardcoded to "en-us"

Could you either make the command line message lang a variable in the mycroft_cli.conf file, or get it from mycroft.conf?
Ah, that's a really good catch. Would explain this quite neatly!
I changed the hardcoded value in the CLI interface for now, but still mycroft does not accept "ja" as a response.
[fixed CLI.txt](https://github.com/MycroftAI/mycroft-core/files/2712313/fixed.CLI.txt)

Tried it again with "Ja" as it could be an upper/lower case problem:
[fixed CLI2.txt](https://github.com/MycroftAI/mycroft-core/files/2712322/fixed.CLI2.txt)
but it still does not seem to work.

Kind regards
Fabian

Hmm, I tested in Swedish and for me it works using the cli out of the box without any modifications. Will keep investigating.

Edit:
Wait a minute. that was just get_response I tried not ask_yesno...retrying

Edit 2:
Swedish vocs were missing for yes and no but when adding them it works just fine. 
Did you try it with the alarm skill?

I will try to set up another environment on my development machine and try it again there with a simpler skill. Maybe it is somewhere else.
This was using a simplified case where the only thing the intent handler did was to run the ask_yesno method.

I'll try with the alarm skill as well...
Tried with removing an alarm and that worked as expected.
NOTE: the wrong "en-us" language tag causes another issue as the wrong normalize-function (`mycroft/util/lang/parse_XX.py`) is called when typing a phrase in `mycroft_cli_client`. 
The english normalize removes article "an" which causes german phrase "schalte das licht an" ("turn the light on") not being recognized by intent parser as the action keyword "an" is missing.
The same phrase spoken is recognized correctly by intent parser as lang-tag is then "de-de".
Thanks for identifying that issue as well. Would you like to submit a PR with a fix or do you want me to do it?
Is `self.lang` accessible in this context? If yes it is probably sufficient to change line 1259 of `mycroft/client/text/text_client.py`to something like:

`bus.emit(Message("recognizer_loop:utterance", {'utterances': [line.strip()], 'lang': self.lang}))`

Unfortunately I don't have my dev-environment available (and it will take me a few days to get access to it), so I can't test it or create a PR - please go ahead if you have a working solution...
Hmm Seems like the cli is only using a cli specific configuration at the moment so it's not easily available...

Settings are basically loaded once in the `load_settings()` function so I'd add code there to load the mycroft config and then store the lang code in a global variable.

Resolving patch:
diff --git a/mycroft/client/text/__main__.py b/mycroft/client/text/__main__.py
--- a/mycroft/client/text/__main__.py
+++ b/mycroft/client/text/__main__.py
@@ -19,7 +19,7 @@
 from mycroft.util import get_ipc_directory
 from .text_client import (
         load_settings, save_settings, simple_cli, gui_main,
-        start_log_monitor, start_mic_monitor, connect_to_messagebus
+        start_log_monitor, start_mic_monitor, connect_to_mycroft
     )
 
 sys.stdout = io.StringIO()
@@ -47,7 +47,7 @@ def main():
     # Monitor IPC file containing microphone level info
     start_mic_monitor(os.path.join(get_ipc_directory(), "mic_level"))
 
-    connect_to_messagebus()
+    connect_to_mycroft()
     if '--simple' in sys.argv:
         sys.stdout = sys.__stdout__
         sys.stderr = sys.__stderr__
diff --git a/mycroft/client/text/text_client.py b/mycroft/client/text/text_client.py
--- a/mycroft/client/text/text_client.py
+++ b/mycroft/client/text/text_client.py
@@ -31,6 +31,7 @@
 from mycroft.messagebus.client.ws import WebsocketClient
 from mycroft.messagebus.message import Message
 from mycroft.util.log import LOG
+from mycroft.configuration import Configuration
 
 import locale
 # Curses uses LC_ALL to determine how to display chars set it to system
@@ -40,6 +41,7 @@
 
 bSimple = False
 bus = None  # Mycroft messagebus connection
+config = {}  # Will be populated by the Mycroft configuration
 event_thread = None
 history = []
 chat = []   # chat history, oldest at the lowest index
@@ -127,6 +129,25 @@ def handleNonAscii(text):
 config_file = os.path.join(os.path.expanduser("~"), ".mycroft_cli.conf")
 
 
+def load_mycroft_config(bus):
+    """ Load the mycroft config and connect it to updates over the messagebus.
+    """
+    Configuration.init(bus)
+    return Configuration.get()
+
+
+def connect_to_mycroft():
+    """ Connect to the mycroft messagebus and load and register config
+        on the bus.
+
+        Sets the bus and config global variables
+    """
+    global bus
+    global config
+    bus = connect_to_messagebus()
+    config = load_mycroft_config(bus)
+
+
 def load_settings():
     global log_filters
     global cy_chat_area
@@ -396,8 +417,12 @@ def handle_utterance(event):
     set_screen_dirty()
 
 
-def connect():
-    # Once the websocket has connected, just watch it for speak events
+def connect(bus):
+    """ Run the mycroft messagebus referenced by bus.
+
+        Arguments:
+            bus:    Mycroft messagebus instance
+    """
     bus.run_forever()
 
 
@@ -1128,6 +1153,7 @@ def gui_main(stdscr):
     global history
     global screen_lock
     global show_gui
+    global config
 
     scr = stdscr
     init_screen()
@@ -1258,7 +1284,7 @@ def gui_main(stdscr):
                     # Treat this as an utterance
                     bus.emit(Message("recognizer_loop:utterance",
                                      {'utterances': [line.strip()],
-                                      'lang': 'en-us'}))
+                                      'lang': config.get('lang', 'en-us')}))
                 hist_idx = -1
                 line = ""
             elif code == 16 or code == 545:  # Ctrl+P or Ctrl+Left (Previous)
@@ -1355,9 +1381,14 @@ def simple_cli():
 
 
 def connect_to_messagebus():
-    global bus
+    """ Connect to the mycroft messagebus and launch a thread handling the
+        connection.
+
+        Returns: WebsocketClient
+    """
     bus = WebsocketClient()  # Mycroft messagebus connection
 
-    event_thread = Thread(target=connect)
+    event_thread = Thread(target=connect, args=[bus])
     event_thread.setDaemon(True)
     event_thread.start()
+    return bus


Test patch: