Initial commit
This commit is contained in:
121
default.ipynb
Normal file
121
default.ipynb
Normal file
@@ -0,0 +1,121 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"id": "initial_id",
|
||||
"metadata": {
|
||||
"collapsed": true,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2024-07-05T15:11:38.213293Z",
|
||||
"start_time": "2024-07-05T15:11:38.113796Z"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"import os\n",
|
||||
"import re\n",
|
||||
"import json\n",
|
||||
"\n",
|
||||
"def load_translations(json_file):\n",
|
||||
" with open(json_file, 'r', encoding='utf-8') as f:\n",
|
||||
" return json.load(f)\n",
|
||||
"\n",
|
||||
"def to_camel_case(s):\n",
|
||||
" parts = s.split('_')\n",
|
||||
" return parts[0].lower() + ''.join(word.capitalize() for word in parts[1:])\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def replace_content(content, pattern, translations):\n",
|
||||
" matches = pattern.findall(content)\n",
|
||||
" for match in matches:\n",
|
||||
" translation_key = match.strip()\n",
|
||||
" translated_value = translations.get(translation_key, \"\")\n",
|
||||
" replacement = f'<ng-container i18n=\"@@{to_camel_case(translation_key)}\">{translated_value}</ng-container>'\n",
|
||||
" content = re.sub(r\"(?<!\\\"){{\\s*'\"+re.escape(translation_key)+r\"'\\s*\\|\\s*oldTranslate\\s*}}\", replacement, content)\n",
|
||||
" \n",
|
||||
"def replace_attributes(content, attr_pattern, translations):\n",
|
||||
" matches = attr_pattern.findall(content)\n",
|
||||
" for match in matches:\n",
|
||||
" full_match, translation_key = match\n",
|
||||
" translation_key = translation_key.strip()\n",
|
||||
" camel_case_key = to_camel_case(translation_key)\n",
|
||||
" new_key = f\"@@{camel_case_key}\"\n",
|
||||
" translated_value = translations.get(translation_key, \"\")\n",
|
||||
" replacement = f'i18n-{full_match.split(\"=\")[0].strip().strip(\"[]\")}=\"{new_key}\" {full_match.split(\"=\")[0].strip().strip(\"[]\")}=\"{translated_value}\"'\n",
|
||||
" content = content.replace(full_match, replacement)\n",
|
||||
" return content\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def search_in_html_files(folder_path, regex):\n",
|
||||
" # Compile the regex pattern\n",
|
||||
" pattern = re.compile(regex)\n",
|
||||
" attr_pattern = re.compile(attr_regex)\n",
|
||||
" attr_block_pattern = re.compile(attr_block_regex)\n",
|
||||
" \n",
|
||||
" # Iterate over all files in the folder\n",
|
||||
" for root, _, files in os.walk(folder_path):\n",
|
||||
" for file in files:\n",
|
||||
" if file.endswith(\".html\"):\n",
|
||||
" file_path = os.path.join(root, file)\n",
|
||||
" with open(file_path, 'r', encoding='utf-8') as f:\n",
|
||||
" content = f.read()\n",
|
||||
" content = replace_attributes(content, attr_block_pattern, translations)\n",
|
||||
" with open(file_path, 'w', encoding='utf-8') as f:\n",
|
||||
" f.write(content)\n",
|
||||
"\n",
|
||||
"# Usage\n",
|
||||
"folder_path = 'C:/Users/rescl/IdeaProjects/unity-client-ts/src/app'\n",
|
||||
"json_file = 'C:/Users/rescl/IdeaProjects/unity-client-ts/src/translations/unity-nl.json'\n",
|
||||
"regex = r\"(?<!\\\"){{\\s*'([^']*)'\\s*\\|\\s*oldTranslate\\s*}}\"\n",
|
||||
"attr_regex = r\"(\\w+\\s*=\\\"{{\\s*\\'([^']*)\\'\\s*\\|\\s*oldTranslate\\s*}}\\\")\"\n",
|
||||
"attr_block_regex = r\"(\\[\\w+\\s*\\]=\\\"\\s*\\'([^']*)\\'\\s*\\|\\s*oldTranslate\\s*\\\")\"\n",
|
||||
"\n",
|
||||
"translations = load_translations(json_file)\n",
|
||||
"search_in_html_files(folder_path, regex)"
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": 20
|
||||
},
|
||||
{
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2024-07-05T14:41:06.786679Z",
|
||||
"start_time": "2024-07-05T14:41:06.784169Z"
|
||||
}
|
||||
},
|
||||
"cell_type": "code",
|
||||
"source": "",
|
||||
"id": "7ac78eb61eab0d7b",
|
||||
"outputs": [],
|
||||
"execution_count": 6
|
||||
},
|
||||
{
|
||||
"metadata": {},
|
||||
"cell_type": "code",
|
||||
"outputs": [],
|
||||
"execution_count": null,
|
||||
"source": "",
|
||||
"id": "1d4376a576d59d77"
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
Reference in New Issue
Block a user