from pathlib import Path

archivo = Path("translations/es/LC_MESSAGES/messages.po")

nuevo_contenido = []
with archivo.open(encoding='utf-8') as f:
    lineas = f.readlines()

i = 0
while i < len(lineas):
    linea = lineas[i]
    nuevo_contenido.append(linea)
    if linea.startswith('msgid ') and not lineas[i+1].startswith('msgstr') and i+2 < len(lineas):
        i += 1
        continue
    if linea.startswith('msgid ') and lineas[i+1].strip() == 'msgstr ""':
        msgid_texto = linea.strip().replace('msgid ', '').strip('"')
        nuevo_contenido.append(f'msgstr "{msgid_texto}"\n')
        i += 1  # saltamos el msgstr vacío
        i += 1
        continue
    i += 1

# Guardar backup y luego sobreescribir
archivo.rename(archivo.with_suffix('.po.bak'))
archivo.write_text(''.join(nuevo_contenido), encoding='utf-8')

print("Listo: se completaron los msgstr vacíos con su msgid.")
