Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

PR#1402: Thousand separator change with language selection; Caveat: need to clear the cache when changing the language

Brandon-Lu7370f3190aa727e
diff --git a/wger/software/views.py b/wger/software/views.py
index 5127630d3..49092d381 100644
--- a/wger/software/views.py
+++ b/wger/software/views.py
@@ -35,7 +35,7 @@ from wger.nutrition.models import Ingredient
logger = logging.getLogger(__name__)
-CACHE_KEY = 'landing-page-context'
+CACHE_KEY = "landing-page-context"
def features(request):
@@ -45,22 +45,87 @@ def features(request):
context = cache.get(CACHE_KEY)
if not context:
- result_github_api = requests.get('https://api.github.com/repos/wger-project/wger').json()
+ result_github_api = requests.get(
+ "https://api.github.com/repos/wger-project/wger"
+ ).json()
context = {
- 'nr_users': User.objects.count(),
- 'nr_exercises': ExerciseBase.objects.count(),
- 'nr_ingredients': Ingredient.objects.count(),
- 'nr_stars': result_github_api.get('stargazers_count', '2000'),
+ "nr_users": User.objects.count(),
+ "nr_exercises": ExerciseBase.objects.count(),
+ "nr_ingredients": Ingredient.objects.count(),
+ "nr_stars": result_github_api.get("stargazers_count", "2000"),
}
+
+ comma_countries = [
+ "ca",
+ "en",
+ "en-au",
+ "en-gb",
+ "en",
+ "es-ar",
+ "es-co",
+ "zh-hans",
+ ]
+
+ period_countries = ["de", "es-mx", "es-ni", "el", "es-ve", "it", "nl", "tr"]
+
+ space_countries = [
+ "bg",
+ "es",
+ "fr",
+ "hr",
+ "nb",
+ "pl",
+ "pt",
+ "pt-br",
+ "ru",
+ "sv",
+ "uk",
+ ]
+
+ def get_format(lang_code):
+ if lang_code in comma_countries:
+ context["nr_users"] = f"{context['nr_users']:,}"
+ context["nr_exercises"] = f"{context['nr_exercises']:,}"
+ context["nr_ingredients"] = f"{context['nr_ingredients']:,}"
+ context["nr_stars"] = f"{context['nr_stars']:,}"
+ elif lang_code in period_countries:
+ context["nr_users"] = f"{context['nr_users']:,}".replace(",", ".")
+ context["nr_exercises"] = f"{context['nr_exercises']:,}".replace(
+ ",", "."
+ )
+ context["nr_ingredients"] = f"{context['nr_ingredients']:,}".replace(
+ ",", "."
+ )
+ context["nr_stars"] = f"{context['nr_stars']:,}".replace(",", ".")
+ elif lang_code in space_countries:
+ context["nr_users"] = f"{context['nr_users']:,}".replace(",", " ")
+ context["nr_exercises"] = f"{context['nr_exercises']:,}".replace(
+ ",", " "
+ )
+ context["nr_ingredients"] = f"{context['nr_ingredients']:,}".replace(
+ ",", " "
+ )
+ context["nr_stars"] = f"{context['nr_stars']:,}".replace(",", " ")
+ else:
+ context["nr_users"] = f"{context['nr_users']:,}"
+ context["nr_exercises"] = f"{context['nr_exercises']:,}"
+ context["nr_ingredients"] = f"{context['nr_ingredients']:,}"
+ context["nr_stars"] = f"{context['nr_stars']:,}"
+
+ get_format(request.LANGUAGE_CODE)
+
cache.set(CACHE_KEY, context, 60 * 60 * 24 * 7) # one week
- FormClass = RegistrationForm if settings.WGER_SETTINGS['USE_RECAPTCHA'] \
+ FormClass = (
+ RegistrationForm
+ if settings.WGER_SETTINGS["USE_RECAPTCHA"]
else RegistrationFormNoCaptcha
+ )
form = FormClass()
- form.fields['username'].widget.attrs.pop("autofocus", None)
+ form.fields["username"].widget.attrs.pop("autofocus", None)
- context['form'] = form
- context['allow_registration'] = settings.WGER_SETTINGS['ALLOW_REGISTRATION']
- context['allow_guest_users'] = settings.WGER_SETTINGS['ALLOW_GUEST_USERS']
+ context["form"] = form
+ context["allow_registration"] = settings.WGER_SETTINGS["ALLOW_REGISTRATION"]
+ context["allow_guest_users"] = settings.WGER_SETTINGS["ALLOW_GUEST_USERS"]
- return render(request, 'features.html', context)
+ return render(request, "features.html", context)