可重用的django应用程序,根据当前设置的语言为名称提供正确的顺序。
django-localized-names的Python项目详细描述
Django本地化名称
一个可重用的django应用程序,允许类似于 Django将日期和数字本地化。当你处理这些事情的时候 具有中文和英文的项目,因为在中文中,名称是 以以下格式编写:
{last name}{first name}{title}
当然,在英语中,它们是这样写的:
{title}. {first name} {last name}
此应用程序将确保,当用户切换网站的语言时,即使人名也将得到正确的“翻译”。
安装
要使用此应用程序,您需要安装以下先决条件:
pip install Django
如果要安装pypi的最新稳定版本:
$ pip install django-localized-names
如果您有冒险精神并希望安装来自github的最新提交:
$ pip install -e git://github.com/bitmazk/django-localized-names.git#egg=localized_names
将localized_names添加到INSTALLED_APPS:
INSTALLED_APPS = ( ..., 'localized_names', )
使用量
模型接口
因为这个应用程序不知道你的具体型号(我们称之为 Person在我们的示例中),它保存名称和标题的值,它 希望您实现以下方法。 示例:
class Person(models.Model): """Holds the information about a person in your system.""" # your fields go here def get_gender(self): """Returns either 'Mr.' or 'Mrs.' depending on the gender.""" if self.gender == 'male': return 'Mr' else: return 'Mrs' def get_title(self): """Returns the academic title of the person""" return self.title def get_romanized_first_name(self): """Returns the first name in roman letters.""" return self.first_name # e.g. Zhang def get_romanized_last_name(self): """Returns the first name in roman letters.""" return self.last_name # e.g. Mingshun def get_non_romanized_first_name(self): """Returns the non roman version of the first name.""" return self.chinese_first_name # e.g. 张 def get_non_romanized_last_name(self): """Returns the non roman version of the first name.""" return self.chinese_last_name # e.g. 明顺 def get_nickname(self): """Returns the nickname of a person in roman letters.""" return self.nickname # e.g. Jack
模板标记
要获取person
的名称,只需在模板中使用templateTag
就像这样{{ person|get_name "SHORT_NAME_FORMAT" }}
。
或者,您可以提供"LONG_NAME_FORMAT"
,这是默认值。
设置
该应用程序提供德语、英语和汉语的标准设置。
如果你想超越我们的标准,你可以设置
CUSTOM_FORMAT_MODULE_PATHS
设置(默认为
[localized_names.formats
,])。
如果提供其他格式,则只需扩展设置。
CUSTOM_FORMAT_MODULE_PATHS = [ 'my_app.formats', 'localized_names.formats', ]
添加或覆盖格式
此应用程序使用Django现有的区域格式框架。 因此,您需要使用以下文件夹结构添加新格式:
my_app/ formats/ __init__.py en/ __init__.py formats.py
这里将进一步介绍:
https://docs.djangoproject.com/en/dev/topics/i18n/formatting/#creating-custom-format-files
然后formats.py
需要设置SHORT_NAME_FORMAT
和
LONG_NAME_FORMAT
。
可能的选项有
g = gender (i.e. Mr, Mrs) G = gender capitalized t = title (i.e. Dr, Prof) t = title capitalized f = romanized first name F = romanized first name capitalized l = romanized last name L = romanized last name capitalized a = non romanized first name A = non romanized name capitalized x = non romanized last name X = non romanized last name capitalized n = nickname N = nickname capitalized
例如
LONG_NAME_FORMAT = '{a}{x}{g} ({L}, "{n}", {f})'
将产生以下格式化名称:
张明顺先生 (ZHANG, "Jack", Mingshun)
贡献
如果您想参与此项目,请执行以下步骤:
# Fork this repository # Clone your fork $ mkvirtualenv -p python2.7 django-localized-names $ pip install -r requirements.txt $ ./localized_names/tests/runtests.sh # You should get no failing tests $ git co -b feature_branch master # Implement your feature and tests # Describe your change in the CHANGELOG.txt $ git add . && git commit $ git push origin feature_branch # Send us a pull request for your feature branch
无论何时运行测试,都会在 tests/coverage/index.html。添加新功能时,请确保 你把保险范围保持在100%。
路线图
查看github上的问题跟踪程序,以了解未来的里程碑和功能。