Skip to content

Commit

Permalink
Rewrite lang module
Browse files Browse the repository at this point in the history
  • Loading branch information
ttgc committed Mar 7, 2024
1 parent 2c35551 commit 2ca8054
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/lang/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!usr/bin/env python3
#-*-coding:utf-8-*-

## TtgcBot - a bot for discord
## Copyright (C) 2017-2024 Thomas PIOT
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>
51 changes: 51 additions & 0 deletions src/lang/language.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!usr/bin/env python3
#-*-coding:utf-8-*-

## TtgcBot - a bot for discord
## Copyright (C) 2017-2024 Thomas PIOT
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>


from typing import Self
from enum import Enum
import os


class Language(Enum):
EN = 'EN.lang'
FR = 'FR.lang'

def __new__(cls, value: str) -> Self:
self = object.__new__(cls)
self._value_ = os.path.join('..', 'Lang', value)

def __init__(self, value: str) -> None:
self._translations = {}

with open(value, 'r', encoding='utf-8') as f:
for line in f.readlines():
if line.startswith('#'):
continue

raw_split = line.split('=')
content = raw_split[1].replace('\n', '').replace('\\n', '\n')
self._translations[raw_split[0]] = content

def __getitem__(self, name: str) -> str:
return self._translations[name]

@classmethod
def get_default(cls) -> Self:
return cls.EN

0 comments on commit 2ca8054

Please sign in to comment.