Skip to content

Commit

Permalink
Implement a script to replace language into code in mongoDB
Browse files Browse the repository at this point in the history
  • Loading branch information
SeoulSKY committed Feb 27, 2024
1 parent f57619d commit 51983d8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion commands/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async def init(self):
Initialize the channel select UI
"""
super().__init__(placeholder=await self.loc.format_value_or_translate("select-channels"),
channel_types=list(ChannelType),
channel_types=[x for x in ChannelType if x != ChannelType.category],
max_values=int(Limit.SELECT_MAX))

return self
Expand Down
45 changes: 45 additions & 0 deletions scripts/language_name_to_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
Replace the translate_to field with language code in MongoDB
"""

import asyncio
import os
import sys

from deep_translator import GoogleTranslator
from motor.motor_asyncio import AsyncIOMotorCollection
from tqdm import tqdm

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

# pylint: disable=wrong-import-position
from mongo.user import collection as user_collection
from mongo.channel import collection as channel_collection

NAME_TO_CODE = GoogleTranslator().get_supported_languages(as_dict=True)


async def replace(collection: AsyncIOMotorCollection):
"""
Replace the translate_to field with language code
"""
pbar = tqdm(desc=f"Updating {collection.name} collection", total=await collection.count_documents({}), unit="doc")

async for doc in collection.find():
pbar.update(1)
doc["translate_to"] = [NAME_TO_CODE.get(lang, lang) for lang in doc["translate_to"] if lang in NAME_TO_CODE]
await collection.replace_one({"_id": doc["_id"]}, doc)

pbar.close()


async def main():
"""
Main function
"""
await replace(user_collection)
await replace(channel_collection)


if __name__ == "__main__":
asyncio.run(main())
6 changes: 6 additions & 0 deletions utils/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,8 @@ async def _translate_about_docs(self, languages: Iterable[Language]) -> None:
Cache.set(translation)
pbar.update()

pbar.close()

await Cache.save()

def _get_commands(self) -> Generator[Command, Any, None]:
Expand Down Expand Up @@ -770,6 +772,8 @@ async def translate_texts(texts: list[str], language: Language) -> list[Translat

pbar.update()

pbar.close()

await Cache.save()

async def _localize_commands(self, languages: Iterable[Language]) -> None:
Expand Down Expand Up @@ -927,4 +931,6 @@ async def translate_texts(language: Language, texts: list[str], is_name: list[bo

pbar.update()

pbar.close()

await Cache.save()

0 comments on commit 51983d8

Please sign in to comment.