Skip to content

Commit

Permalink
Enable preselect on setup wizard language/region lists
Browse files Browse the repository at this point in the history
  • Loading branch information
MoojMidge committed Mar 1, 2024
1 parent bfef886 commit 87d80a5
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions resources/lib/youtube_plugin/youtube/helper/yt_setup_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,32 +193,77 @@ def _process_language(provider, context):
return

client = provider.get_client(context)
settings = context.get_settings()

plugin_language = settings.get_language()
plugin_region = settings.get_region()

kodi_language = context.get_language()
base_kodi_language = kodi_language.partition('-')[0]

json_data = client.get_supported_languages(kodi_language)
items = json_data.get('items') or DEFAULT_LANGUAGES['items']
invalid_ids = ['es-419'] # causes hl not a valid language error. Issue #418

selected_language = [None]

def _get_selected_language(item):
item_lang = item[1]
base_item_lang = item_lang.partition('-')[0]
if item_lang == kodi_language or item_lang == plugin_language:
selected_language[0] = item
elif not selected_language[0] and base_item_lang == base_kodi_language:
selected_language.append(item)
return item

# Ignore es-419 as it causes hl not a valid language error
# https://github.com/jdf76/plugin.video.youtube/issues/418
invalid_ids = ('es-419',)
language_list = sorted([
(item['snippet']['name'], item['snippet']['hl'])
for item in items
if item['id'] not in invalid_ids
])
], key=_get_selected_language)

if selected_language[0]:
selected_language = language_list.index(selected_language[0])
elif len(selected_language) > 1:
selected_language = language_list.index(selected_language[1])
else:
selected_language = None

language_id = context.get_ui().on_select(
context.localize('setup_wizard.select_language'),
language_list,
preselect=selected_language
)
if language_id == -1:
return

json_data = client.get_supported_regions(language=language_id)
items = json_data.get('items') or DEFAULT_REGIONS['items']

selected_region = [None]

def _get_selected_region(item):
item_region = item[1]
if item_region == plugin_region:
selected_region[0] = item
return item

region_list = sorted([
(item['snippet']['name'], item['snippet']['gl'])
for item in items
])
], key=_get_selected_region)

if selected_region[0]:
selected_region = region_list.index(selected_region[0])
else:
selected_region = None

region_id = context.get_ui().on_select(
context.localize('setup_wizard.select_region'),
region_list,
preselect=selected_region
)
if region_id == -1:
return
Expand Down

0 comments on commit 87d80a5

Please sign in to comment.