《Django 5 By Example》阅读笔记:p493-p520

《Django 5 By Example》学习第 17 天,p493-p520 总结,总计 28 页。

一、技术总结

1.internationalization(国际化) vs localization(本地化)

(1)18n,L10n,g11n

以前总觉得这两个缩写好难记,今天仔细看了下维基百科,"i18n" 中的 i 代表 "internationalization" 的第一个字母 i,n 代表最后一个字母 n, 18 代表 i 和 n 中间有 18 个字母。"L10n" 中的 i 代表 "localization" 的第一个字母 l,n 代表最后一个字母 n, 10 代表 l 和 n 中间有 10个字母。使用大写L 是因为小写的 l(L的小写) 和 大写的 I(i 的 大写)差不多一样,所以使用大写,其实大小写都可以。

也有人把上面两者合称为全球化(globalization),简称为 g11n。

(2)internationalization 和 localization 的区别?

p495, Internationalization (frequently abbreviated to i18n) is the process of adapting software for the potential use of different languages and locales so that it isn't hardwired to a specific language or locale.

Localization (abbreviated to l10n) is the process of actually translating the software and adapt-ing it to a particular locale. Django itself is translated into more than 50 languages using its internationalization framework.

说实话,看完这两句还是没法理解它们之间的区别是什么。

2.Django 国际化和本地化实现

(1)设置

国际化和本地化依赖于 gettext, 如果没有先安装。本人是在WSL 里面使用 Ubuntu 系统,安装示例:

python 复制代码
# sudo apt install gettext

(2)翻译 Python 代码

1)Standard translation

使用 gettext() 实现:

python 复制代码
from django.utils.translation import gettext as _

output = _('Text to be translated.')

2)Lazy translations

When using the lazy functions, strings are translated when the value is accessed, rather than when the function is called (this is why they are translated lazily).

使用 suffix _lazy() 实现。

3)Translations including variables

使用 gettext() 及 占位(placeholder)符实现。

python 复制代码
from django.utils.translation import gettext as _

month = _('April')

day = '14'

output = _('Today is %(month)s %(day)s') % {'month': month, 'day': day}

4)Plural forms in translations

使用 ngettext() 和 ngettext_lazy() 实现。

(3)生成 po 文件

在 project 目录下执行命令:

python 复制代码
django-admin makemessages --all

(4)生成mo文件

在 project 目录下执行命令:

python 复制代码
django-admin compilemessages

(5)翻译 template

使用 {% translate %} 和 {% blocktranslate %} 。

(6)poedit

用于编辑翻译文件的工具。

https://poedit.net/

二、英语总结(生词:2)

1.hardwired

p495, Internationalization (frequently abbreviated to i18n) is the process of adapting software for the potential use of different languages and locales so that it isn't hardwired to a specific language or locale.

(1)haredwired: hard("with effor or energy, with difficulty") + wire("adorn(装饰) with wire")

adj. 也写作 hard-wired。本意是:In computing, with permanently connected circuit performing unchangeable functions(在计算机技术中,具有永久连接的电路,执行不可改变的功能),后面引申为"Describe sth that is fixed, or designed in a way that is cannot be easily changed or modified",和 hardcoded 的用法类似。

2.interpolate

p499, Translations including variables: Used to interpolate variables within strings that are to be translated.

(1)interpolate:inter-("among, between") + polare("to smooth, polish(擦)")

vt. to alter by inserting sth between other elements.

3.place an order

p503, You have added names for the fields that are displayed when a user places a new order.

"place an order" 的意思就是"下订单",对于 order,看的时候一下脑子里面的第一反应是"顺序",导致理解不了,其实 place an order 是一个很常见的用法,这里记一下。

三、其它

看下去,即使这本书写得再不好。

四、参考资料

1. 编程

(1) Antonio Melé,《Django 5 By Example》:https://book.douban.com/subject/37007362/

2. 英语

(1) Etymology Dictionary:https://www.etymonline.com

(2) Cambridge Dictionary:https://dictionary.cambridge.org

欢迎搜索及关注:编程人(a_codists)

相关推荐
trayvontang几秒前
Python虚拟环境与包管理工具(uv、Conda)
python·conda·uv·虚拟环境·miniconda·miniforge
伊织code4 分钟前
pdfminer.six
python·pdf·图片·提取·文本·pdfminer·pdfminer.six
hqxstudying29 分钟前
JAVA项目中邮件发送功能
java·开发语言·python·邮件
Q_Q5110082851 小时前
python的软件工程与项目管理课程组学习系统
spring boot·python·django·flask·node.js·php·软件工程
合作小小程序员小小店1 小时前
SDN安全开发环境中常见的框架,工具,第三方库,mininet常见指令介绍
python·安全·生成对抗网络·网络安全·网络攻击模型
后台开发者Ethan1 小时前
Python需要了解的一些知识
开发语言·人工智能·python
北京_宏哥2 小时前
Python零基础从入门到精通详细教程11 - python数据类型之数字(Number)-浮点型(float)详解
前端·python·面试
盼小辉丶2 小时前
PyTorch生成式人工智能——使用MusicGen生成音乐
pytorch·python·深度学习·生成模型
HAPPY酷4 小时前
给纯小白的Python操作 PDF 笔记
开发语言·python·pdf
传奇开心果编程5 小时前
【传奇开心果系列】Flet框架实现的家庭记账本示例自定义模板
python·学习·ui·前端框架·自动化