Skip to content

Instantly share code, notes, and snippets.

@carloshm
Created April 16, 2021 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carloshm/e06bbaf051668ad826cc1cd6293e230d to your computer and use it in GitHub Desktop.
Save carloshm/e06bbaf051668ad826cc1cd6293e230d to your computer and use it in GitHub Desktop.
Python Simple Internationalization
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Carlos de Huerta <carlos.hm@live.com>
# Distributed under the MIT License (https://opensource.org/licenses/MIT)
from intl import intl
# Pi Weather Rock labels have been translated and included in the json file resources.lang.json
mytext = intl()
# get text for the label feels_like in english
print(mytext.get_text('en','feels_like'))
# Prints -> Feels like:
# get text for the label sunset_at in spanish
print(mytext.get_text('es','sunset_at').format(hour=12, minute=60))
# Prints -> Ocaso en 12 hrs 60 min
# get text for the label umbrella in basque
print(mytext.get_text('eu','umbrella'))
# Prints -> Hartu aterkia!
# get text for the label umbrella in greek (not available in the resources file)
# english is the fallback language
print(mytext.get_text('el','umbrella'))
# Prints -> Grab your umbrella!
# get text for the label tonight in catalan
print(mytext.get_text('ca','tonight'))
# Prints -> aquesta nit
# get text for the label tonight in catalan and Capitalize it
print(mytext.get_text('ca','tonight', capital=True))
# Prints -> Aquesta nit
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Carlos de Huerta <carlos.hm@live.com>
# Distributed under the MIT License (https://opensource.org/licenses/MIT)
import json
from os import path
RESOURCES_FILE = 'resources.lang.json'
class intl:
"""
This class assists in the internationalization and localization of apps
through the text stored in the RESOURCES_FILE for different languages supported by the config file.
"""
def __init__(self):
with open(path.join(path.dirname(__file__),RESOURCES_FILE), "r") as t:
self.resources = json.load(t)
def get_text(self, ui_lang, text, capital = False, fallback = 'en'):
if self.resources.get(ui_lang) is None:
ui_lang = fallback
if capital is True:
return self.resources[ui_lang][text].capitalize()
else:
return self.resources[ui_lang][text]
{
"ca":{
"feels_like": "Sensació tèrmica:",
"wind": "Vent:",
"humidity": "Humitat:",
"umbrella": "¡Agafa el paraigües!",
"no_umbrella": "Avui no agafis el paraigües",
"today": "avui",
"powered_by": "Weather rock gràcies a Dark Sky",
"tonight": "aquesta nit",
"tomorrow": "demà",
"check_at": "Part meteorològic de les",
"sunrise": "Alba: {sunrise}",
"sunset": "Posta de sol: {sunset}",
"sunrise_at": "fa de dia a {hour} hrs {minute:02d} min",
"sunset_at": "Ocàs a {hour} hrs {minute:02d} min",
"daylight": "Llum de dia: {hour} hrs {minute:02d} min"
},
"de":{
"feels_like": "Fühlt sich an wie:",
"wind": "Wind:",
"humidity": "Luftfeuchtigkeit:",
"umbrella": "Schnapp dir den Regenschirm!",
"no_umbrella": "Nimm heute nicht den Regenschirm",
"today": "heute",
"powered_by": "Weather rock dank Dark Sky",
"tonight": "heute Abend",
"tomorrow":"morgen",
"check_at": "Wetterbericht der",
"sunrise": "Sonnenaufgang: {sunrise}",
"sunset": "Sonnenuntergang: {sunset}",
"sunrise_at": "Sonnenaufgang in {hour} std. {minute:02d} min.",
"sunset_at": "Sonnenuntergang in {hour} std. {minute:02d} min.",
"daylight": "Tageslicht: {hour} Std. {minute:02d} min."
},
"en": {
"feels_like": "Feels Like:",
"wind":"Wind:",
"humidity":"Humidity:",
"umbrella":"Grab your umbrella!",
"no_umbrella":"No umbrella needed today.",
"today":"today",
"powered_by":"A weather rock powered by Dark Sky",
"tonight":"tonight",
"tomorrow":"tomorrow",
"check_at":"Weather checked at",
"sunrise":"Sunrise: {sunrise}",
"sunset":"Sunset: {sunset}",
"sunrise_at":"Sunrise in {hour} hrs {minute:02d} min",
"sunset_at":"Sunset in {hour} hrs {minute:02d} min",
"daylight":"Daylight: {hour} hrs {minute:02d} min"
},
"es": {
"feels_like": "Sensación térmica:",
"wind":"Viento:",
"humidity":"Humedad:",
"umbrella":"¡Coge el paragüas!",
"no_umbrella":"Hoy no cojas el paragüas",
"today":"hoy",
"powered_by":"Weather rock gracias a Dark Sky",
"tonight":"esta noche",
"tomorrow":"mañana",
"check_at":"Parte meteorológico de las",
"sunrise":"Amanecer: {sunrise}",
"sunset":"Puesta de sol: {sunset}",
"sunrise_at":"Amanece en {hour} hrs {minute:02d} min",
"sunset_at":"Ocaso en {hour} hrs {minute:02d} min",
"daylight":"Luz de día: {hour} hrs {minute:02d} min"
},
"eu":{
"feels_like": "Sentitzen da:",
"wind": "Haizea:",
"humidity": "Hezetasuna:",
"umbrella": "Hartu aterkia!",
"no_umbrella": "Gaur ez hartu aterkia",
"today": "gaur",
"powered_by": "Weather rock Dark Sky-ri esker",
"tonight": "gaur gauean",
"tomorrow": "bihar",
"check_at": "Eguraldiaren iragarpena",
"sunrise": "Egunsentia: {sunrise}",
"sunset": "Ilunabarra: {sunset}",
"sunrise_at": "Egunsentia {hour} hrs {minute:02d} min",
"sunset_at": "Ilunabarra {hour} hrs {minute:02d} min",
"daylight": "Eguneko argia: {hour} hrs {minute:02d} min"
},
"fr":{
"feels_like": "Refroidissement éolien:",
"wind": "Vent:",
"humidity": "Humidité:",
"umbrella": "Attrape le parapluie!",
"no_umbrella": "Ne prenez pas le parapluie aujourd'hui",
"today": "aujourd'hui",
"powered_by": "Weather rock grâce à Dark Sky",
"tonight":"ce soir",
"tomorrow": "demain",
"check_at": "Bulletin météo du",
"sunrise": "Lever de soleil: {sunrise}",
"sunset": "Coucher de soleil: {sunset}",
"sunrise_at": "Lever de soleil dans {hour} hrs {minute:02d} min",
"sunset_at": "Coucher de soleil dans {hour} hrs {minute:02d} min",
"daylight": "Lumière du joir: {hour} hrs {minute:02d} min"
},
"gl":{
"feels_like": "Refrixeración do vento:",
"wind": "Vento:",
"moist": "Humidade:",
"umbrella": "Agarra o paraugas!",
"no_umbrella": "Non collas o paraugas hoxe",
"today": "hoxe",
"powered_by": "O tempo é rockeiro grazas a Dark Sky",
"tonight": "esta noite",
"mañá": "mañá",
"check_at": "Informe meteorolóxico do",
"sunrise": "Amanecer: {sunrise}",
"sunset": "Atardecer: {sunset}",
"sunrise_at": "Amencer en {hour} hrs {minute:02d} min",
"sunset_at": "Atardecer en {hour} hrs {minute:02d} min",
"daylight": "Luz do día: {hour} hrs {minute:02d} min"
},
"it":{
"feels_like": "Si sente come:",
"wind": "Vento:",
"humidity": "Umidità:",
"umbrella": "Prendi l'ombrello!",
"no_umbrella": "Non prendere l'ombrello oggi",
"today": "today",
"powered_by": "Weather rock grazie a Dark Sky",
"tonight": "stasera",
"tomorrow": "domani",
"check_at": "Bollettino meteorologico del",
"sunrise": "Alba: {sunrise}",
"sunset": "Tramonto: {sunset}",
"sunrise_at": "Alba tra {hour} ore {minute:02d} min",
"sunset_at": "Tramonto tra {hour} ore {minute:02d} min",
"daylight": "Luce del giorno: {hour} ore {minute:02d} min"
},
"pt":{
"feels_like": "Parece:",
"wind": "Vento:",
"humidity": "Umidade:",
"umbrella": "¡Pegue o guarda-chuva!",
"no_umbrella": "Não leve o guarda-chuva hoje",
"today": "hoje",
"powered_by": "Weather rock graças ao Dark Sky",
"tonight": "esta noite",
"tomorrow": "amanhã",
"check_at": "Boletim meteorológico de",
"sunrise": "Nascer do sol: {sunrise}",
"sunset": "Pôr do sol: {sunset}",
"sunrise_at": "Nascer do sol em {hour} horas {minute:02d} min",
"sunset_at": "Pôr do sol em {hour} horas {minute:02d} min",
"daylight": "Luz do dia: {hour} horas {minute:02d} min"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment