Simplify getting language display name on macOS

This commit is contained in:
Kovid Goyal 2020-12-07 17:01:23 +05:30
parent 644625d7d6
commit 0e3bc56602
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 13 additions and 8 deletions

View File

@ -3,7 +3,6 @@
# License: GPL v3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
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()

View File

@ -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) {