/usr/share/pyshared/zope/browserresource/i18nfile.py is in python-zope.browserresource 3.12.0-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | ##############################################################################
#
# Copyright (c) 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Internationalized file resource.
"""
from zope.i18n.interfaces import II18nAware
from zope.i18n.negotiator import negotiator
from zope.interface import implements, classProvides
from zope.browserresource.file import FileResource
from zope.browserresource.interfaces import IResourceFactory
from zope.browserresource.interfaces import IResourceFactoryFactory
class I18nFileResource(FileResource):
implements(II18nAware)
def __init__(self, data, request, defaultLanguage='en'):
"""Creates an internationalized file resource. data should be
a mapping from languages to File objects.
"""
self._data = data
self.request = request
self.defaultLanguage = defaultLanguage
def chooseContext(self):
"""Choose the appropriate context according to language"""
langs = self.getAvailableLanguages()
language = negotiator.getLanguage(langs, self.request)
try:
return self._data[language]
except KeyError:
return self._data[self.defaultLanguage]
def getDefaultLanguage(self):
'See II18nAware'
return self.defaultLanguage
def setDefaultLanguage(self, language):
'See II18nAware'
if language not in self._data:
raise ValueError(
'cannot set nonexistent language (%s) as default' % language)
self.defaultLanguage = language
def getAvailableLanguages(self):
'See II18nAware'
return self._data.keys()
# for unit tests
def _testData(self, language):
file = self._data[language]
f=open(file.path,'rb')
data=f.read()
f.close()
return data
class I18nFileResourceFactory(object):
implements(IResourceFactory)
classProvides(IResourceFactoryFactory)
def __init__(self, data, defaultLanguage):
self.__data = data
self.__defaultLanguage = defaultLanguage
def __call__(self, request):
return I18nFileResource(self.__data, request, self.__defaultLanguage)
|