Kakushigoto
Member
- Oct 2, 2024
- 286
- 360
- 132
import requests
import re
import json
# Path to the text file on your computer
raw_path = "cntd.txt"
text_path = "cntd(translated).txt"
# Function to read content from a text file on the computer
def read_file_content(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.readlines() # Read file by line
return content
except FileNotFoundError:
print(f"File {file_path} does not exist.")
return None
# Function to write content to a file
def write_file_content(file_path, content):
try:
with open(file_path, 'a', encoding='utf-8') as file:
file.write(content + "\n") # Add newline after each piece of content
print(f"Result has been written to file {file_path}")
except Exception as e:
print(f"An error occurred while writing to the file: {e}")
# Function to send an API request
def send_api_request(text_content):
url = "https://api.dichnhanh.com/"
# Form data you provided
# type: "Modern", "Ancient"
data = {
"type": "Ancient",
"enable_analyze": 1,
"enable_fanfic": 0,
"mode": "qt",
"text": text_content, # Text content from file
"remove": ""
}
# Send POST request to API
try:
response = requests.post(url, data=data)
if response.status_code == 200:
return response.text
else:
print(f"Error {response.status_code}: {response.text}")
return None
except Exception as e:
print("An error occurred:", e)
return None
# Capitalize the first letter immediately after a newline
def capitalize_after_newline(text):
lines = text.split('\n') # Split text by newline
capitalized_lines = []
special_characters = ['(', '( ', "'", '"', '|', '[', '“', '【', '【 ', '《', '《 '] # Special characters
for line in lines:
line = line.strip() # Remove leading and trailing spaces
if line: # If the line is not empty
if line[0] in special_characters and len(line) > 1:
# If the first character is special, capitalize the character right after it
capitalized_line = line[0] + line[1].upper() + line[2:]
else:
# If there's no special character, capitalize the first character
capitalized_line = line[0].upper() + line[1:]
capitalized_lines.append(capitalized_line)
else:
capitalized_lines.append(line) # If the line is empty, keep it as is
return '\n'.join(capitalized_lines)
# Function to process and write results from the API
def write_result(response_text, text_path):
# Parse JSON
response_json = json.loads(response_text)
# Get the text content from the 'content' key
raw_content = response_json['data']['content']
# Replace <br> or <p> tags with newline \n
content_with_newline = re.sub(r'<br\s*/?>|</p>', '\n', raw_content)
# Remove HTML tags (span, br, ...) using regex
cleaned_content = re.sub(r'<[^>]+>', '', content_with_newline)
# Apply capitalization processing function
final_content = capitalize_after_newline(cleaned_content)
# Print the processed result
write_file_content(text_path, final_content)
# Function to split the file into smaller chunks
def split_content_into_chunks(content, chunk_size=100):
for i in range(0, len(content), chunk_size):
yield ''.join(content[i:i+chunk_size]) # Join the lines into a string
# Read file content
text_content = read_file_content(raw_path)
# If file is not empty, split the content and send each chunk
if text_content:
for chunk in split_content_into_chunks(text_content):
response = send_api_request(chunk)
if response:
write_result(response, text_path)
you'll have to create your own addonHello guys
I just want to ask that can I customize translate engine to use my own API ?
Here is a translate file python code using my custom API
Python:import requests import re import json # Path to the text file on your computer raw_path = "cntd.txt" text_path = "cntd(translated).txt" # Function to read content from a text file on the computer def read_file_content(file_path): try: with open(file_path, 'r', encoding='utf-8') as file: content = file.readlines() # Read file by line return content except FileNotFoundError: print(f"File {file_path} does not exist.") return None # Function to write content to a file def write_file_content(file_path, content): try: with open(file_path, 'a', encoding='utf-8') as file: file.write(content + "\n") # Add newline after each piece of content print(f"Result has been written to file {file_path}") except Exception as e: print(f"An error occurred while writing to the file: {e}") # Function to send an API request def send_api_request(text_content): url = "https://api.dichnhanh.com/" # Form data you provided # type: "Modern", "Ancient" data = { "type": "Ancient", "enable_analyze": 1, "enable_fanfic": 0, "mode": "qt", "text": text_content, # Text content from file "remove": "" } # Send POST request to API try: response = requests.post(url, data=data) if response.status_code == 200: return response.text else: print(f"Error {response.status_code}: {response.text}") return None except Exception as e: print("An error occurred:", e) return None # Capitalize the first letter immediately after a newline def capitalize_after_newline(text): lines = text.split('\n') # Split text by newline capitalized_lines = [] special_characters = ['(', '( ', "'", '"', '|', '[', '“', '【', '【 ', '《', '《 '] # Special characters for line in lines: line = line.strip() # Remove leading and trailing spaces if line: # If the line is not empty if line[0] in special_characters and len(line) > 1: # If the first character is special, capitalize the character right after it capitalized_line = line[0] + line[1].upper() + line[2:] else: # If there's no special character, capitalize the first character capitalized_line = line[0].upper() + line[1:] capitalized_lines.append(capitalized_line) else: capitalized_lines.append(line) # If the line is empty, keep it as is return '\n'.join(capitalized_lines) # Function to process and write results from the API def write_result(response_text, text_path): # Parse JSON response_json = json.loads(response_text) # Get the text content from the 'content' key raw_content = response_json['data']['content'] # Replace <br> or <p> tags with newline \n content_with_newline = re.sub(r'<br\s*/?>|</p>', '\n', raw_content) # Remove HTML tags (span, br, ...) using regex cleaned_content = re.sub(r'<[^>]+>', '', content_with_newline) # Apply capitalization processing function final_content = capitalize_after_newline(cleaned_content) # Print the processed result write_file_content(text_path, final_content) # Function to split the file into smaller chunks def split_content_into_chunks(content, chunk_size=100): for i in range(0, len(content), chunk_size): yield ''.join(content[i:i+chunk_size]) # Join the lines into a string # Read file content text_content = read_file_content(raw_path) # If file is not empty, split the content and send each chunk if text_content: for chunk in split_content_into_chunks(text_content): response = send_api_request(chunk) if response: write_result(response, text_path)
it actually isn't, but you can export the project to xlsx, csv, etc. just click on the button on the top rightIt's seem to be hard
Otherway,Is there a way to extract all original text to a file
Then I translate this file by my custom API
Then put the translated data to the intial or machine translation column View attachment 4231648
After I translate the text, the small pattern on the left suddenly disappears, how can I restore it?
View attachment 4166512
↑ after
View attachment 4166516
↑ before
[/引用]
你翻译了系统引导字符,假如<New>代表引导使用这个图标,你把New翻译后,引导就无法触发,你在翻译的时候也需要把这个图案文件的名称改为被引导触发的名称才会生效。 "New"只是个举例!
第一个办法,你需要找到并改写图案名字与引导词搭配来触发图案
第二个办法,你再进行一次翻译,跳过关键引导词来维持图案的触发。
Information was shared by a Buddy with the nickname (Alex(GoD)):Sisulizer 4
Thanks, man.You must be registered to see the links
Translator++ v_6.11.27
With the (www\php\cache) folder cleaned out, post it later if you can.Thanks, man.
With the (www\php\cache) folder cleaned out, post it later if you can.
You must be registered to see the links
Translator++ v_6.11.27
Thank you very much.You must be registered to see the links
Could you continue this tutorial? I found it excellent, but I don't know what to do with this tip you gave us. What would be the next step to translate the initial hill to Machine Translator?Information was shared by a Buddy with the nickname (Alex(GoD)):
1) Open your project in Translator++:
Once you have opened your project, press Ctrl+H in the main window of the programme to bring up a box, select ‘Search’ .
2) Select ‘Context’:
In the upper right corner of the box where it says ‘Target’, select ‘Context’.
View attachment 4255971
3) Find /message/ and tag it:
In the search box, type /message/ and press Enter. (This may take some time).
Click ‘Select all.’ Then right click on any of the entries found.
Select ‘With selection’ and then ‘Set Green Tag’.
View attachment 4255975
4) Repeat the process (Set Green Tag) for /Show Choices/ :
Similarly, find /Show Choices/ and set ‘Set Green Tag’ for all of them.
Note: This usually covers about 95% of the game text, except for things like character names, items, skills, menu options, and lines in plugins.
At this stage, anything that needs translation is marked with a green tag. You can move all these green lines to a separate column.
5) Select the relevant files:
Select all MapXXX files, as well as the CommonEvents and Troops files. (CommonEvents and Troops files may contain translatable lines).
6) Create an automation script:
Right-click any selected file on the left (For example: on Map001). Select ‘With (number) selected’ -> ‘Create automation’ -> ‘For each row’.
View attachment 4255980
7) Paste and run the following script: (this will move all green tagged rows to the Initial column)
if (this.tags.includes("green")) {
this.cells[1] = this.cells[0];
}
View attachment 4255988
It's done.
View attachment 4255991
Save the project.
Good luck with your translations!!!
Translated withYou must be registered to see the links(free version)