JaganathC commited on
Commit
ab79a67
Β·
verified Β·
1 Parent(s): 1ed98cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -22
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import streamlit as st
2
  from deep_translator import GoogleTranslator
3
  import json
 
4
 
5
  # Fetch supported languages dynamically
6
  SUPPORTED_LANGUAGES = GoogleTranslator().get_supported_languages()
@@ -17,45 +18,92 @@ target_languages = st.multiselect("🎯 Select target languages", SUPPORTED_LANG
17
  # Input type selection
18
  input_type = st.radio("πŸ“Œ Choose input method", ["Text", "TXT File", "JSON File"])
19
 
20
- def translate_text(text, languages):
21
- """Translate text into multiple languages"""
22
- translations = {}
23
- for lang in languages:
24
- translations[lang] = GoogleTranslator(source='auto', target=lang).translate(text)
25
- return translations
26
 
27
- def translate_json(data, languages):
28
- """Translate JSON while keeping keys intact"""
 
 
 
 
 
 
 
 
29
  translated_json = {}
30
- for key, value in data.items():
 
31
  if isinstance(value, dict):
32
- translated_json[key] = translate_json(value, languages)
 
 
 
 
 
 
 
33
  else:
34
- translated_json[key] = translate_text(value, languages)
 
 
35
  return translated_json
36
 
37
  if input_type == "Text":
38
- user_text = st.text_area("✏️ Enter text to translate:")
39
  if st.button("Translate"):
40
  if user_text.strip():
41
- result = translate_text(user_text, target_languages)
42
- st.json(result)
 
 
 
 
 
 
 
 
 
 
43
  else:
44
  st.warning("Please enter text to translate.")
45
 
46
  elif input_type == "TXT File":
47
  uploaded_file = st.file_uploader("πŸ“„ Upload a TXT file", type=["txt"])
48
- if uploaded_file and st.button("Translate"):
49
  content = uploaded_file.read().decode("utf-8")
50
- result = translate_text(content, target_languages)
51
- st.json(result)
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  elif input_type == "JSON File":
54
  uploaded_json = st.file_uploader("πŸ“‚ Upload a JSON file", type=["json"])
55
- if uploaded_json and st.button("Translate"):
56
  content = json.load(uploaded_json)
57
- result = translate_json(content, target_languages)
58
- st.json(result)
59
- st.download_button("⬇️ Download Translated JSON", json.dumps(result, indent=4), "translated.json", "application/json")
 
 
 
 
 
 
 
 
 
 
60
 
61
- st.markdown("πŸš€ **Supports over 100 languages!**")
 
1
  import streamlit as st
2
  from deep_translator import GoogleTranslator
3
  import json
4
+ import re
5
 
6
  # Fetch supported languages dynamically
7
  SUPPORTED_LANGUAGES = GoogleTranslator().get_supported_languages()
 
18
  # Input type selection
19
  input_type = st.radio("πŸ“Œ Choose input method", ["Text", "TXT File", "JSON File"])
20
 
21
+ def translate_text(text, lang):
22
+ """Translate text while preserving placeholders inside {{ }}"""
23
+ placeholders = re.findall(r"(\{\{.*?\}\})", text) # Extract placeholders
24
+ translated_text = GoogleTranslator(source="auto", target=lang).translate(text)
 
 
25
 
26
+ # Restore placeholders to keep them unchanged
27
+ for placeholder in placeholders:
28
+ translated_text = translated_text.replace(GoogleTranslator(source="auto", target=lang).translate(placeholder), placeholder)
29
+
30
+ return translated_text
31
+
32
+ def translate_json_values(json_data, lang):
33
+ """
34
+ Translate JSON values while preserving the original keys in quotes.
35
+ """
36
  translated_json = {}
37
+
38
+ for key, value in json_data.items():
39
  if isinstance(value, dict):
40
+ # Recursive translation for nested dictionaries
41
+ translated_json[key] = translate_json_values(value, lang)
42
+ elif isinstance(value, list):
43
+ # Translate list elements
44
+ translated_json[key] = [translate_text(item, lang) if isinstance(item, str) else item for item in value]
45
+ elif isinstance(value, str):
46
+ # Translate string values only
47
+ translated_json[key] = translate_text(value, lang)
48
  else:
49
+ # Keep numbers/booleans unchanged
50
+ translated_json[key] = value
51
+
52
  return translated_json
53
 
54
  if input_type == "Text":
55
+ user_text = st.text_area("✏ Enter text to translate:")
56
  if st.button("Translate"):
57
  if user_text.strip():
58
+ results = {lang: translate_text(user_text, lang) for lang in target_languages}
59
+ st.json(results)
60
+
61
+ # Provide download button for TXT file
62
+ for lang, translated_text in results.items():
63
+ file_name = f"translated_{lang}.txt"
64
+ st.download_button(
65
+ label=f"⬇ Download Translated TXT ({lang})",
66
+ data=translated_text,
67
+ file_name=file_name,
68
+ mime="text/plain"
69
+ )
70
  else:
71
  st.warning("Please enter text to translate.")
72
 
73
  elif input_type == "TXT File":
74
  uploaded_file = st.file_uploader("πŸ“„ Upload a TXT file", type=["txt"])
75
+ if uploaded_file:
76
  content = uploaded_file.read().decode("utf-8")
77
+ if st.button("Translate"):
78
+ results = {lang: translate_text(content, lang) for lang in target_languages}
79
+ st.json(results)
80
+
81
+ # Provide download button for translated TXT files
82
+ for lang, translated_text in results.items():
83
+ file_name = f"translated_{lang}.txt"
84
+ st.download_button(
85
+ label=f"⬇ Download Translated TXT ({lang})",
86
+ data=translated_text,
87
+ file_name=file_name,
88
+ mime="text/plain"
89
+ )
90
 
91
  elif input_type == "JSON File":
92
  uploaded_json = st.file_uploader("πŸ“‚ Upload a JSON file", type=["json"])
93
+ if uploaded_json:
94
  content = json.load(uploaded_json)
95
+ translated_results = {lang: translate_json_values(content, lang) for lang in target_languages}
96
+
97
+ st.json(translated_results)
98
+
99
+ # Provide download button for translated JSON files
100
+ for lang, data in translated_results.items():
101
+ file_name = f"translated_{lang}.json"
102
+ st.download_button(
103
+ label=f"⬇ Download Translated JSON ({lang})",
104
+ data=json.dumps(data, indent=4, ensure_ascii=False),
105
+ file_name=file_name,
106
+ mime="application/json"
107
+ )
108
 
109
+ st.markdown("πŸš€ *Supports over 100 languages!*")