Skip to content

Commit

Permalink
remove unused translations
Browse files Browse the repository at this point in the history
  • Loading branch information
Zheoni committed Jan 13, 2024
1 parent 49d5886 commit 0f57173
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 59 deletions.
12 changes: 10 additions & 2 deletions .hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
#!/bin/sh

# This hook prevents commiting changes when the css is not built for production

# Check that CSS is built for production
if [ $(wc -l < ui/assets/styles.css) -gt 1 ]; then
echo "styles.css not minified: run 'npm run build' in ui/"
exit 1
fi

# Check i18n translations
python ui/i18n/check.py
res=$?
if [ $res -ne 0 ]; then
echo "There is a problem with the translations"
exit 1
fi

13 changes: 1 addition & 12 deletions ui/i18n/_template.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"_lang": null,
"index": {
"divideInFolders": null,
"search": null,
"noRecipes": null
},
Expand Down Expand Up @@ -44,17 +43,11 @@
"metric": null,
"imperial": null
},
"scaleSelector": {
"label": null,
"presets": null,
"reset": null
},
"ingredients": null,
"cookware": null,
"optMarker": null,
"method": null,
"stepIngredientsView": {
"label": null,
"compact": null,
"list": null,
"hidden": null
Expand All @@ -70,16 +63,12 @@
"fixed": null
},
"error": {
"notFound": null,
"parse": null,
"unknown": null
"parse": null
},
"timer": {
"start": null
},
"quantity": {
"roundError": null,
"cantConvert": null,
"reset": null
},
"about": {
Expand Down
68 changes: 59 additions & 9 deletions ui/i18n/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,38 @@
import json
import os
import pathlib
import re

template = "_template.json"

translations = os.path.dirname(__file__)


def main():
with pathlib.Path(translations, template).open() as fp:
content = json.load(fp)
templateKeys = extract_keys(content)
templateKeys = set(templateKeys)

def check_template(template_keys):
files = [
file
for file in os.listdir(translations)
if file.endswith(".json") and not file.startswith("_")
]

error = False
for file in files:
path = pathlib.Path(translations, file)
with path.open() as fp:
content = json.load(fp)
keys = extract_keys(content)
keys = set(keys)
if templateKeys != keys:
if template_keys != keys:
error = True
print("Error in", file)
missing = templateKeys.difference(keys)
extra = keys.difference(templateKeys)
missing = template_keys.difference(keys)
extra = keys.difference(template_keys)
if len(missing) > 0:
print("Missing keys:", missing)
if len(extra) > 0:
print("Extra keys:", extra)
print()
return error


def extract_keys(obj: dict, prefix: str = "", array: list = None):
Expand All @@ -50,5 +49,56 @@ def extract_keys(obj: dict, prefix: str = "", array: list = None):
return array


html_templates = os.path.join(translations, "..", "templates")

# Used but, not directly
safelist = set(
[
"_lang",
"r.convertSelector.default",
"r.convertSelector.metric",
"r.convertSelector.imperial",
"openInEditor.error",
"openInEditor.success",
]
)


def check_uses(template_keys):
error = False
re_usages = re.compile(r"\bt\([\"'](\w+(?:.\w+)*)[\"']")
all_uses = set(safelist)

for root, _, tmpls in os.walk(html_templates):
for tmpl in tmpls:
path = pathlib.Path(root, tmpl)
with path.open(encoding="utf8") as fp:
content = fp.read()
uses = re_usages.findall(content)
for use in uses:
all_uses.add(use)
unused = template_keys.difference(all_uses)
if len(unused) > 0:
error = True
print("Unused keys:", unused)
not_found = all_uses.difference(template_keys)
if len(not_found) > 0:
error = True
print("Not found keys:", not_found)
return error


def main():
with pathlib.Path(translations, template).open() as fp:
content = json.load(fp)
template_keys = extract_keys(content)
template_keys = set(template_keys)
error = False
error |= check_template(template_keys)
error |= check_uses(template_keys)
if error:
exit(1)


if __name__ == "__main__":
main()
13 changes: 1 addition & 12 deletions ui/i18n/de.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"_lang": "Deutsche",
"index": {
"divideInFolders": "Ordneransicht",
"search": "Suche",
"noRecipes": "Keine Rezepte gefunden"
},
Expand Down Expand Up @@ -44,17 +43,11 @@
"metric": "metrisch",
"imperial": "imperial"
},
"scaleSelector": {
"label": "Größenordnung",
"presets": "Vorgaben",
"reset": "Zurücksetzen"
},
"ingredients": "Zutaten",
"cookware": "Kochutensilien",
"optMarker": "wählen",
"method": "Zubereitung",
"stepIngredientsView": {
"label": "Zutatenansicht",
"compact": "kompakt",
"list": "Liste",
"hidden": "versteckt"
Expand All @@ -70,16 +63,12 @@
"fixed": "Dieser Wert lässt sich nicht umrechnen"
},
"error": {
"notFound": "Rezept nicht gefunden",
"parse": "Fehler beim Verarbeiten des Rezepts",
"unknown": "unbekannter Fehler"
"parse": "Fehler beim Verarbeiten des Rezepts"
},
"timer": {
"start": "Timer starten"
},
"quantity": {
"roundError": "Fehler",
"cantConvert": "Bei der Umrechnung der Menge ist ein Fehler aufgetreten.",
"reset": "Reset"
},
"about": {
Expand Down
13 changes: 1 addition & 12 deletions ui/i18n/en.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"_lang": "English",
"index": {
"divideInFolders": "Divide in folders",
"search": "Search",
"noRecipes": "No recipes found"
},
Expand Down Expand Up @@ -44,17 +43,11 @@
"metric": "Metric",
"imperial": "Imperial"
},
"scaleSelector": {
"label": "Scale",
"presets": "Presets",
"reset": "Reset"
},
"ingredients": "Ingredients",
"cookware": "Cookware",
"optMarker": "opt",
"method": "Method",
"stepIngredientsView": {
"label": "Step ingredients view",
"compact": "Compact",
"list": "List",
"hidden": "Hidden"
Expand All @@ -70,16 +63,12 @@
"fixed": "This values does not scale"
},
"error": {
"notFound": "Recipe not found",
"parse": "Error parsing recipe",
"unknown": "Unknown error"
"parse": "Error parsing recipe"
},
"timer": {
"start": "Start timer"
},
"quantity": {
"roundError": "Error",
"cantConvert": "There was an error trying to convert the quantity",
"reset": "Reset"
},
"about": {
Expand Down
13 changes: 1 addition & 12 deletions ui/i18n/es.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"_lang": "Español",
"index": {
"divideInFolders": "Por carpetas",
"search": "Buscar",
"noRecipes": "No se han encontrado recetas"
},
Expand Down Expand Up @@ -44,17 +43,11 @@
"metric": "Métrico",
"imperial": "Imperial"
},
"scaleSelector": {
"label": "Escalar",
"presets": "Preajustes",
"reset": "Reiniciar"
},
"ingredients": "Ingredientes",
"cookware": "Menaje",
"optMarker": "opcional",
"method": "Método",
"stepIngredientsView": {
"label": "Vista de ingredients del paso",
"compact": "Compacto",
"list": "Lista",
"hidden": "Oculto"
Expand All @@ -70,16 +63,12 @@
"fixed": "Este valor no escala"
},
"error": {
"notFound": "Receta no econtrada",
"parse": "Error interpretando la receta",
"unknown": "Error desconocido"
"parse": "Error interpretando la receta"
},
"timer": {
"start": "Iniciar temporizador"
},
"quantity": {
"roundError": "Error",
"cantConvert": "Ha habido un error convirtiendo la cantidad",
"reset": "Reset"
},
"about": {
Expand Down
3 changes: 3 additions & 0 deletions ui/templates/components/recipe_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ <h1 class="font-heading text-6xl">
{% endif %}
<span>
<i class="i-lucide-ruler me-1 text-primary-11"></i>
<label for="units" class="sr-only">
{{ t('r.convertSelector.label') }}
</label>
<select
id="units"
name="units"
Expand Down

0 comments on commit 0f57173

Please sign in to comment.