Added ca, es and en language support

This commit is contained in:
spla 2021-05-21 13:50:16 +02:00
pare d54306e919
commit f157e821bb
S'han modificat 4 arxius amb 353 adicions i 220 eliminacions

17
app/locales/ca.txt Normal file
Veure arxiu

@ -0,0 +1,17 @@
current_users_str: usuaris actuals
users_before_str: usuaris abans
users_hour_str: usuaris per hora
new_users_last_hour_str: nous usuaris darrera hora
new_users_last_day_str: nous usuaris darrer dia
new_users_last_week_str: nous usuaris darrera setmana
hourly_users_evolution_str: evolució horaria d'usuaris
in_the_last_hour_str: en la darrera hora
daily_users_evolution_str: evolució diaria d'usuaris
in_the_last_day_str: en el darrer dia
weekly_users_evolution_str: evolució setmanal d'usuaris
in_the_last_week_str: en la darrera setmana
daily_evolution_str: Evolució diaria
weekly_evolution_str: Evolució setmanal
welcomes_str: dona la benvinguda a
we_have_str: Ja som
users_str: usuaris

17
app/locales/en.txt Normal file
Veure arxiu

@ -0,0 +1,17 @@
current_users_str: current users
users_before_str: users before
users_hour_str: users per hour
new_users_last_hour_str: new users last hour
new_users_last_day_str: new users last day
new_users_last_week_str: new users last week
hourly_users_evolution_str: hourly users evolution
in_the_last_hour_str: in the last hour
daily_users_evolution_str: daily users evolution
in_the_last_day_str: in the last day
weekly_users_evolution_str: weekly users evolution
in_the_last_week_str: in the last week
daily_evolution_str: Daily evolution
weekly_evolution_str: Weekly evolution
welcomes_str: welcomes
we_have_str: We have
users_str: users

17
app/locales/es.txt Normal file
Veure arxiu

@ -0,0 +1,17 @@
current_users_str: usuarios actuales
users_before_str: usuarios anteriores
users_hour_str: usuarios por hora
new_users_last_hour_str: nuevos usuarios última hora
new_users_last_day_str: nuevos usuarios último dia
new_users_last_week_str: nuevos usuaris última setmana
hourly_users_evolution_str: evolución horaria de usuarios
in_the_last_hour_str: en la última hora
daily_users_evolution_str: evolución diaria de usuarios
in_the_last_day_str: en el último dia
weekly_users_evolution_str: evolución semanal de usuarios
in_the_last_week_str: en la última semana
daily_evolution_str: Evolución diaria
weekly_evolution_str: Evolución semanal
welcomes_str: da la bienvenida a
we_have_str: Ya somos
users_str: usuarios

Veure arxiu

@ -45,6 +45,30 @@ def db_config():
return (mastodon_db, mastodon_db_user, welcome_db, welcome_db_user) return (mastodon_db, mastodon_db_user, welcome_db, welcome_db_user)
def get_locale( parameter, welcome_lang):
if welcome_lang not in ['ca','es','en']:
print("lang must be 'ca', 'es' or 'en'")
sys.exit(0)
language_filepath = f"app/locales/{welcome_lang}.txt"
if not os.path.isfile(language_filepath):
print("File %s not found, exiting."%language_filepath)
sys.exit(0)
with open( language_filepath ) as f:
for line in f:
if line.startswith( parameter ):
return line.replace(parameter + ":", "").strip()
print(language_filepath + " Missing parameter %s "%parameter)
sys.exit(0)
def usage():
print('usage: python ' + sys.argv[0] + ' --ca (or --es or --en)')
# Returns the parameter from the specified file # Returns the parameter from the specified file
def get_parameter( parameter, file_path ): def get_parameter( parameter, file_path ):
# Check if secrets file exists # Check if secrets file exists
@ -67,6 +91,34 @@ def get_parameter( parameter, file_path ):
if __name__ == '__main__': if __name__ == '__main__':
# usage modes
if len(sys.argv) == 1:
usage()
elif len(sys.argv) >= 2:
welcome_lang = ''
if sys.argv[1] == '--ca':
welcome_lang = 'ca'
if sys.argv[1] == '--es':
welcome_lang = 'es'
elif sys.argv[1] == '--en':
welcome_lang = 'en'
if not welcome_lang in ['ca', 'es', 'en']:
print("\nOnly 'ca', 'es' and 'en' languages are supported.\n")
sys.exit(0)
mastodon, mastodon_hostname = mastodon() mastodon, mastodon_hostname = mastodon()
mastodon_db, mastodon_db_user, welcome_db, welcome_db_user = db_config() mastodon_db, mastodon_db_user, welcome_db, welcome_db_user = db_config()
@ -142,10 +194,14 @@ if __name__ == '__main__':
conn.close() conn.close()
current_users_str = get_locale("current_users_str", welcome_lang)
users_before_str = get_locale("users_before_str", welcome_lang)
users_hour_str = get_locale("users_hour_str", welcome_lang)
print("-----------------") print("-----------------")
print("current users: "+str(current_id)) print(current_users_str + ': ' + str(current_id))
print("users before: "+str(users_before)) print(users_before_str + ': ' + str(users_before))
print("users / hour: "+str(users_hour)) print(users_hour_str + ': ' + str(users_hour))
insert_sql = """INSERT INTO welcome(datetime, users, users_hour) insert_sql = """INSERT INTO welcome(datetime, users, users_hour)
VALUES(%s,%s,%s) RETURNING datetime;""" VALUES(%s,%s,%s) RETURNING datetime;"""
@ -305,10 +361,14 @@ if __name__ == '__main__':
day_inc = current_id - users_last_day day_inc = current_id - users_last_day
week_inc = current_id - users_last_week week_inc = current_id - users_last_week
new_users_last_hour_str = get_locale("new_users_last_hour_str", welcome_lang)
new_users_last_day_str = get_locale("new_users_last_day_str", welcome_lang)
new_users_last_week_str = get_locale("new_users_last_week_str", welcome_lang)
print("------------------------") print("------------------------")
print("new users last hour: "+str(hour_inc)) print(new_users_last_hour_str + ': ' + str(hour_inc))
print("new users last day: "+str(day_inc)) print(new_users_last_day_str + ': ' + str(day_inc))
print("new users last week: "+str(week_inc)) print(new_users_last_week_str + ': ' + str(week_inc))
################################################################################### ###################################################################################
@ -316,44 +376,62 @@ if __name__ == '__main__':
if hour_inc != 0: if hour_inc != 0:
users_hourly_change = current_id - users_last_hour users_hourly_change = current_id - users_last_hour
print("Hourly users evolution: %s"%users_hourly_change)
hourly_users_evolution_str = get_locale("hourly_users_evolution_str", welcome_lang)
in_the_last_hour_str = get_locale("in_the_last_hour_str", welcome_lang)
print(hourly_users_evolution_str + ': %s'%users_hourly_change)
if users_hourly_change > 0: if users_hourly_change > 0:
hourly_change_string = "+" + format(users_hourly_change, ",d") + " en la darrera hora\n" hourly_change_string = "+" + format(users_hourly_change, ",d") + in_the_last_hour_str + "\n"
# Daily change # Daily change
if day_inc != 0: if day_inc != 0:
daily_change = current_id - users_last_day daily_change = current_id - users_last_day
print("Daily users evolution: %s"%daily_change)
daily_users_evolution_str = get_locale("daily_users_evolution_str", welcome_lang)
in_the_last_day_str = get_locale("in_the_last_day_str", welcome_lang)
print(daily_users_evolution_str + ': %s'%daily_change)
if daily_change > 0: if daily_change > 0:
daily_change_string = "+" + format(daily_change, ",d") + " in the last day\n" daily_change_string = "+" + format(daily_change, ",d") + in_the_last_day_str + "\n"
# Weekly change # Weekly change
if week_inc != 0: if week_inc != 0:
weekly_change = current_id - users_last_week weekly_change = current_id - users_last_week
print("Weekly users evolution: %s"%weekly_change)
weekly_users_evolution_str = get_locale("weekly_users_evolution_str", welcome_lang)
in_the_last_week_str = get_locale("in_the_last_week_str", welcome_lang)
print(weekly_users_evolution_str + ': %s'%weekly_change)
if weekly_change > 0: if weekly_change > 0:
weekly_change_string = "+" + format(weekly_change, ",d") + " in the last week\n" weekly_change_string = "+" + format(weekly_change, ",d") + in_the_last_week_str + "\n"
elif users_hourly_change < 0: elif users_hourly_change < 0:
hourly_change_string = format(users_hourly_change, ",d") + " in the last hour\n" hourly_change_string = format(users_hourly_change, ",d") + in_the_last_hour_str + "\n"
# Daily change # Daily change
if day_inc != 0: if day_inc != 0:
daily_change = current_id - users_last_day daily_change = current_id - users_last_day
print("Daily evolution: %s"%daily_change)
daily_evolution_str = get_locale("daily_evolution_str", welcome_lang)
print(daily_evolution_str + ': %s'%daily_change)
if daily_change < 0: if daily_change < 0:
daily_change_string = format(daily_change, ",d") + " in the last day\n" daily_change_string = format(daily_change, ",d") + in_the_last_day_str + "\n"
# Weekly change # Weekly change
if week_inc != 0: if week_inc != 0:
weekly_change = current_id - users_last_week weekly_change = current_id - users_last_week
print("Weekly evolution: %s"%weekly_change)
weekly_evolution_str = get_locale("weekly_evolution_str", welcome_lang)
print(weekly_evolution_str + ': %s'%weekly_change)
if weekly_change < 0: if weekly_change < 0:
weekly_change_string = format(weekly_change, ",d") + " in the last week\n" weekly_change_string = format(weekly_change, ",d") + in_the_last_week_str + "\n"
############################################################################### ###############################################################################
# TOOT IT! # TOOT IT!
@ -361,11 +439,15 @@ if __name__ == '__main__':
if len(welcome_users) > 0: if len(welcome_users) > 0:
welcomes_str = get_locale("welcomes_str", welcome_lang)
we_have_str = get_locale("we_have_str", welcome_lang)
users_str = get_locale("users_str", welcome_lang)
toot_text = "\n" toot_text = "\n"
toot_text += mastodon_hostname + " welcomes:" + "\n\n" toot_text += mastodon_hostname + ' ' + welcomes_str + ':\n\n'
toot_text += new_users_string + "\n" toot_text += new_users_string + "\n"
toot_text += "\n" toot_text += "\n"
toot_text += "\n\nWe have " + str(current_id) + " users\n\n" toot_text += "\n\n" + we_have_str + ' ' + str(current_id) + users_str + "\n\n"
toot_text += hourly_change_string toot_text += hourly_change_string
toot_text += daily_change_string toot_text += daily_change_string
toot_text += weekly_change_string toot_text += weekly_change_string