Simple translator based on local dictionary

This commit is contained in:
Maximilian Giller 2019-05-09 17:41:34 +02:00
parent d2fa6d8fd7
commit 710255604a
3 changed files with 109 additions and 2 deletions

View file

@ -2,6 +2,7 @@ default_language = "en"
'''Characters following '*' are placeholders and will be replaced by some number/text/etc.''' '''Characters following '*' are placeholders and will be replaced by some number/text/etc.'''
'''Events'''
more_events = { more_events = {
'en' : '+ *0 more', 'en' : '+ *0 more',
'de' : '+ *0 weitere' 'de' : '+ *0 weitere'
@ -13,4 +14,86 @@ multiday_events = {
allday_events = { allday_events = {
'en' : 'All-day', 'en' : 'All-day',
'de' : 'Ganztägig' 'de' : 'Ganztägig'
} }
'''Weather'''
rain_weather = {
'en' : 'Rain',
'de' : 'Regen'
}
clear_weather = {
'en' : 'Clear',
'de' : 'Klar'
}
clouds_weather = {
'en' : 'Clouds',
'de' : 'Wolken'
}
thunderstorm_weather = {
'en' : 'Thunderstorm',
'de' : 'Gewitter'
}
drizzle_weather = {
'en' : 'Drizzle',
'de' : 'Niesel'
}
snow_weather = {
'en' : 'Snow',
'de' : 'Schnee'
}
mist_weather = {
'en' : 'Mist',
'de' : 'Nebel'
}
smoke_weather = {
'en' : 'Smoke',
'de' : 'Rauch'
}
haze_weather = {
'en' : 'Haze',
'de' : 'Nebel'
}
dust_weather = {
'en' : 'Dust',
'de' : 'Staub'
}
fog_weather = {
'en' : 'Fog',
'de' : 'Nebel'
}
sand_weather = {
'en' : 'Sand',
'de' : 'Sand'
}
ash_weather = {
'en' : 'Ash',
'de' : 'Asche'
}
squall_weather = {
'en' : 'Squall',
'de' : 'Sturm'
}
tornado_weather = {
'en' : 'Tornado',
'de' : 'Tornado'
}
dictionary_collection = [
rain_weather,
clear_weather,
dust_weather,
squall_weather,
tornado_weather,
clouds_weather,
thunderstorm_weather,
smoke_weather,
ash_weather,
sand_weather,
fog_weather,
haze_weather,
mist_weather,
drizzle_weather,
snow_weather,
more_events,
allday_events,
multiday_events
]

View file

@ -3,6 +3,7 @@ from WeatherInterface import WeatherInterface
import pyowm import pyowm
from datetime import datetime from datetime import datetime
from settings import units, language from settings import units, language
from Translator import translate
class OwmForecasts (WeatherInterface): class OwmForecasts (WeatherInterface):
"""Fetches weather through the Openweathermap-api.""" """Fetches weather through the Openweathermap-api."""
@ -62,7 +63,7 @@ class OwmForecasts (WeatherInterface):
forecast_object.icon = weather.get_weather_icon_name() forecast_object.icon = weather.get_weather_icon_name()
forecast_object.air_humidity = str(weather.get_humidity()) forecast_object.air_humidity = str(weather.get_humidity())
forecast_object.clouds = str(weather.get_clouds()) forecast_object.clouds = str(weather.get_clouds())
forecast_object.short_description = str(weather.get_status()) forecast_object.short_description = translate(str(weather.get_status()))
forecast_object.detailed_description = str(weather.get_detailed_status()) forecast_object.detailed_description = str(weather.get_detailed_status())
forecast_object.air_pressure = str(weather.get_pressure()['press']) forecast_object.air_pressure = str(weather.get_pressure()['press'])

23
Calendar/Translator.py Normal file
View file

@ -0,0 +1,23 @@
from Dictionary import default_language, dictionary_collection
from settings import language
'''Looks up a phrase in a given dictionary-collection
and returns the translated phrase'''
def translate(phrase, target_lang = language, dictionary_collection = dictionary_collection) :
dictionary = find_dictionary(dictionary_collection, phrase)
if dictionary == None:
return phrase
if target_lang in dictionary.keys():
return dictionary[target_lang]
else:
return dictionary[default_language]
def find_dictionary(dictionary_collection, phrase):
for dictionary in dictionary_collection:
if phrase in dictionary.values():
return dictionary
return None