Last active
June 24, 2021 04:51
-
-
Save Gerst20051/8ce190572790d60dbcc2c06255536b14 to your computer and use it in GitHub Desktop.
Python 3 Language Dropdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#--SETTINGS-IMPORTER------------------------ | |
class SettingsImporter(): | |
LANGUAGES = [ | |
('ar', 'Arabic'), | |
('de', 'German'), | |
('el', 'Greek'), | |
('en', 'English'), | |
('es', 'Spanish'), | |
('fr', 'French'), | |
('pt', 'Portuguese'), | |
('it', 'Italian'), | |
('tr', 'Turkish'), | |
('ja', 'Japanese'), | |
] | |
#--TEMPLATE-RENDERER------------------------ | |
class Component(): | |
@classmethod | |
def render(cls): | |
print('Component') | |
class TemplateRenderer(): | |
def __init__(self, components: [Component]): | |
self.components = components | |
def render_page(self): | |
for c in self.components: | |
c.render() | |
#--COMPONENTS------------------------------ | |
class PageTitle(Component): | |
@classmethod | |
def render(cls): | |
print('--- Demo Page ---') | |
class Dropdown(Component): | |
INPUT_NAME = 'Dropdown' | |
@classmethod | |
def as_list(cls): | |
return [] | |
def render(self): | |
print(f'{self.INPUT_NAME}:') | |
for key, value in self.as_list(): | |
print(f' {key} => {value}') | |
class LangDropdown(Dropdown): | |
INPUT_NAME = 'Language' | |
POPULAR_LANGUAGES = [] | |
def __init__(self, input_name, popular_languages): | |
self.INPUT_NAME = input_name | |
self.POPULAR_LANGUAGES = popular_languages | |
def as_list(self): | |
sorted_languages = [] | |
popular_languages = [ x for x in SettingsImporter.LANGUAGES if x[0] in self.POPULAR_LANGUAGES ] | |
nonpopular_languages = [ x for x in SettingsImporter.LANGUAGES if x[0] not in self.POPULAR_LANGUAGES ] | |
if popular_languages: | |
sorted_languages.extend(sorted(popular_languages, key = lambda x: self.POPULAR_LANGUAGES.index(x[0]))) | |
sorted_languages.append(('--', '──────────')) | |
sorted_languages.extend(sorted(nonpopular_languages, key = lambda x: x[1])) | |
return sorted_languages | |
#--MAIN--------------------------------- | |
main_language_dropdown = LangDropdown('Language', ['en', 'es', 'de', 'fr']) | |
secondary_language_dropdown = LangDropdown('Secondary Language', ['fr', 'de']) | |
components = [PageTitle, main_language_dropdown, secondary_language_dropdown] | |
TemplateRenderer(components).render_page() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment