gpt4 book ai didi

Django makemessages 来获取自定义的可翻译字符串

转载 作者:行者123 更新时间:2023-12-04 16:10:03 27 4
gpt4 key购买 nike

在我的 Django 项目中,我使用 Vue + vue-gettext对于 i18n。

为了制作可翻译的字符串,我的 .vue 文件中有以下代码:

<translate>Hello %{name}</translate>

<a href="..." v-translate>Click here</a>


(使用 翻译 标签和 v-translate 属性)

有没有办法配置 manage.py makemessages还要解析这段代码?默认情况下,它不会查看 .vue 文件或解析此 i18n 代码格式。

最佳答案

基本上我最终得到了一个 Makefile - 它从 vue 文件中提取消息,然后将其与 django

# On OSX the PATH variable isn't exported unless "SHELL" is also set, see: http://stackoverflow.com/a/25506676
SHELL = /bin/bash
NODE_BINDIR = ./node_modules/.bin
export PATH := $(NODE_BINDIR):$(PATH)

INPUT_DIR = static/js
# Where to write the files generated by this makefile.
OUTPUT_DIR = .

# Available locales for the app.
LOCALES = en nl fr

# Name of the generated .po files for each available locale.
LOCALE_FILES ?= $(patsubst %,$(OUTPUT_DIR)/locale/%/LC_MESSAGES/app.po,$(LOCALES))

GETTEXT_HTML_SOURCES = $(shell find $(INPUT_DIR) -name '*.vue' -o -name '*.html' 2> /dev/null)
GETTEXT_JS_SOURCES = $(shell find $(INPUT_DIR) -name '*.vue' -o -name '*.js')

# Makefile Targets
.PHONY: clean makemessages

clean:
rm -f /tmp/template.pot

makemessages: clean django_makemessages /tmp/template.pot

django_makemessages:
./manage.py makemessages $(patsubst %,-l %,$(LOCALES))

# Create a main .pot template, then generate .po files for each available language.
# Thanx to Systematic: https://github.com/Polyconseil/systematic/blob/866d5a/mk/main.mk#L167-L183
/tmp/template.pot: $(GETTEXT_HTML_SOURCES)
# `dir` is a Makefile built-in expansion function which extracts the directory-part of `$@`.
# `$@` is a Makefile automatic variable: the file name of the target of the rule.
# => `mkdir -p /tmp/`
mkdir -p $(dir $@)
which gettext-extract
# Extract gettext strings from templates files and create a POT dictionary template.
gettext-extract --attribute v-translate --quiet --output $@ $(GETTEXT_HTML_SOURCES)
# Extract gettext strings from JavaScript files.
xgettext --language=JavaScript --keyword=npgettext:1c,2,3 \
--from-code=utf-8 --join-existing --no-wrap \
--package-name=$(shell node -e "console.log(require('./package.json').name);") \
--package-version=$(shell node -e "console.log(require('./package.json').version);") \
--output $@ $(GETTEXT_JS_SOURCES)
# Generate .po files for each available language.
@for lang in $(LOCALES); do \
export PO_FILE=$(OUTPUT_DIR)/locale/$$lang/LC_MESSAGES/djangojs.po; \
echo "msgmerge --update $$PO_FILE $@"; \
mkdir -p $$(dirname $$PO_FILE); \
[ -f $$PO_FILE ] && msgmerge --lang=$$lang --update --backup=off $$PO_FILE $@ || msginit --no-translator --locale=$$lang --input=$@ --output-file=$$PO_FILE; \
msgattrib --no-wrap --no-obsolete -o $$PO_FILE $$PO_FILE; \
done;
合并。

关于Django makemessages 来获取自定义的可翻译字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44219651/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com