File size: 1,937 Bytes
0cee3b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# ===================================================================== #
#                      Copyright (c) 2022 Itz-fork                      #
#                                                                       #
# 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/>   #
# ===================================================================== #

import logging
from json import loads
from asyncio import get_event_loop


# Cache dicts
USER_LANG = {}
STRINGS = {}


def update_languages_cache():
    from unzipper.database.language import get_user_languages

    async def _iter_and_update():
        async for doc in await get_user_languages():
            USER_LANG[doc["_id"]] = doc["lang"]

    loop = get_event_loop()
    loop.run_until_complete(_iter_and_update())


def update_text_strings():
    def _read_json(file, as_items=False):
        with open(file) as f:
            return loads(f.read()).items() if as_items else loads(f.read())

    subfolders = _read_json("unzipper/localization/languages.json", True)
    for lcode, fnm in subfolders:
        str_list = _read_json(f"unzipper/localization/{lcode}/messages.json")
        btn_strs = _read_json(f"unzipper/localization/defaults/buttons.json")
        STRINGS[lcode] = str_list
        STRINGS["buttons"] = btn_strs


def update_cache():
    logging.info(" >> Updating text strings cache...")
    update_text_strings()

    logging.info(" >> Updating language cache...")
    update_languages_cache()