From 0e3bc56602a11b0b68f1211fd4052c858adef296 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 7 Dec 2020 17:01:23 +0530 Subject: [PATCH] Simplify getting language display name on macOS --- src/calibre/gui2/tts/macos_config.py | 10 ++++------ src/calibre/gui2/tts/nsss.m | 11 +++++++++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/calibre/gui2/tts/macos_config.py b/src/calibre/gui2/tts/macos_config.py index 44b05c00ce..ccfa08c4d3 100644 --- a/src/calibre/gui2/tts/macos_config.py +++ b/src/calibre/gui2/tts/macos_config.py @@ -3,7 +3,6 @@ # License: GPL v3 Copyright: 2020, Kovid Goyal from contextlib import suppress -from calibre_extensions.cocoa import locale_names from PyQt5.Qt import ( QAbstractItemView, QAbstractTableModel, QFontMetrics, QFormLayout, QItemSelectionModel, QSortFilterProxyModel, QSlider, Qt, QTableView, QWidget @@ -24,10 +23,11 @@ class VoicesModel(QAbstractTableModel): def gender(x): return gmap.get(x, x) - self.current_voices = tuple((x['name'], x['locale_id'], x['age'], gender(x['gender'])) for x in voice_data.values()) + def language(x): + return x.get('language_display_name') or x['locale_id'] or '' + + self.current_voices = tuple((x['name'], language(x), x['age'], gender(x['gender'])) for x in voice_data.values()) self.voice_ids = tuple(voice_data) - all_locales = tuple(filter(None, (x[1] for x in self.current_voices))) - self.locale_map = dict(zip(all_locales, locale_names(*all_locales))) self.column_headers = _('Name'), _('Language'), _('Age'), _('Gender') def rowCount(self, parent=None): @@ -50,8 +50,6 @@ class VoicesModel(QAbstractTableModel): data = self.current_voices[row - 1] col = index.column() ans = data[col] or '' - if col == 1: - ans = self.locale_map.get(ans, ans) return ans if role == Qt.ItemDataRole.UserRole: row = index.row() diff --git a/src/calibre/gui2/tts/nsss.m b/src/calibre/gui2/tts/nsss.m index 9886d1ac43..805b8a6c78 100644 --- a/src/calibre/gui2/tts/nsss.m +++ b/src/calibre/gui2/tts/nsss.m @@ -104,13 +104,20 @@ static PyObject* NSSS_get_all_voices(NSSS *self, PyObject *args) { PyObject *ans = PyDict_New(); if (!ans) return NULL; + NSLocale *locale = [NSLocale autoupdatingCurrentLocale]; for (NSSpeechSynthesizerVoiceName voice_id in [NSSpeechSynthesizer availableVoices]) { NSDictionary *attributes = [NSSpeechSynthesizer attributesForVoice:voice_id]; if (attributes) { + NSObject *lang_key = [attributes objectForKey:NSVoiceLocaleIdentifier]; + const char *lang_name = NULL; + if (lang_key && [lang_key isKindOfClass:[NSString class]]) { + NSString *display_name = [locale displayNameForKey:NSLocaleIdentifier value:(NSString*)lang_key]; + if (display_name) lang_name = [display_name UTF8String]; + } #define E(x, y) #x, as_python([attributes objectForKey:y]) - PyObject *v = Py_BuildValue("{sN sN sN sN sN}", + PyObject *v = Py_BuildValue("{sN sN sN sN sN sz}", E(name, NSVoiceName), E(age, NSVoiceAge), E(gender, NSVoiceGender), - E(demo_text, NSVoiceDemoText), E(locale_id, NSVoiceLocaleIdentifier)); + E(demo_text, NSVoiceDemoText), E(locale_id, NSVoiceLocaleIdentifier), "language_display_name", lang_name); if (!v) { Py_DECREF(ans); return NULL; } #undef E if (PyDict_SetItemString(ans, [voice_id UTF8String], v) != 0) {