HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/buyercall_forms/buyercall/buyercall/lib/i18n.py
from os import path
from flask_webpack import Webpack
from flask_babel import get_locale
import time


class I18nWebpack(Webpack):
    def __init__(self, *args, **kwargs):
        super(I18nWebpack, self).__init__(*args, **kwargs)

    def _add_prefix(self, f, ext):
        head, tail = path.split(f)
        languages = [get_locale().language, 'en']
        for lang in languages:
            candidate = path.join(head, '{}.{}'.format(lang, tail))
            if '{}.{}'.format(candidate, ext) in self.assets:
                return candidate
        return f

    def javascript_tag(self, *args):
        return super(I18nWebpack, self).javascript_tag(
                *[self._add_prefix(f, 'js') for f in args]
        )

    def stylesheet_tag(self, *args):
        return super(I18nWebpack, self).stylesheet_tag(
                *[self._add_prefix(f, 'css') for f in args]
        )

    def asset_url_for(self, *arg):
        param = '?timestamp={}'.format(time.time())  # will result in something like ?timestamp=1355563265.81
        return "{}{}".format(super(I18nWebpack, self).asset_url_for(*arg), param)