-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguide
109 lines (72 loc) · 4.26 KB
/
guide
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# https://docs.djangoproject.com/en/5.1/topics/i18n/ or test driven website multi language in django
1- install gettext shared model(that handle ddl files)
2- config LANGUAGES and LOCALE_PATHS in settings ==>
LANGUAGES = [
('fa', 'persian'),
('en', 'english'),
]
LOCALE_PATHS = [
BASE_DIR / 'locale',
]
3- in template that need use it and load i18n and every field that need to translate just must put it in trans item==>
<h2>{% trans 'this is home page' %}</h2>
4- python manage.py has a command to collect all messages for translating and if you want ignore something or just collect
one target language in that command==>python manage.py makemessages -l <fa,en or it,...> --ignore venv
5- now after run that command you can edit file.po of other languages that want to use and done.but for django to
understand that when must use translated words and sentences must set i18n_patterns like static and media urls in
settings ===>
urlpatterns = []
urlpatterns += i18n_patterns(
path('admin/', admin.site.urls),
path('', include('home_module.urls')),
prefix_default_language=False
# if it is False it doesn't display default language in url==> # website.com/en/==>website.com
)
6- MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware', # new
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
7- now still doesnt use translated mssges and just urls change,last step is to run this command(to mage django.mo file in locale path) and boom!!==>
python manage.py compilemessages --ignore venv
django-admin compilemessages --ignore venv
------ translation in python files
# it is for all static files and words in templates.now if you want to translate in python files must try this way
1- use gettext or gettext_lazy module
2- again run makemessages command and write translated words in .po file
3- run compliemessages and done again you can see translated words!!
from django.utils.translation import gettext_lazy as _
def index(request):
context = {
'data': _('hello world!!')
}
return render(request, 'home_module/index.html', context)
_______________________________________________________________________________________________________________
*** we can use poedit or translate all the world one by one but it is not easy.now is another way to translate .po files,
and it is rosseta ==>
1- first must install it in project==>ppip install django-rosetta
2- add it in settings apps ==>'rosetta'
3- add urls in main urls with include and done =>path('rosetta/',include('rosetta.urls'))
4- just for understandig our .po files must set ROSETTA_LANGUAGES=[] too in settings
_________________________________________________________________________________________________________________
**next step is using translation packages for handleling model fields in django.there are 2 ways to handle it and have
some diffrences with each other.for way one there is parler package and for it in database need to add 2 data(row) for
every object in tables and it is not good for seo and in big projects that work with many data decrease performance of site
*but second way is using django-modeltranslation package.this way add column for every language in every field of models that we want
to translate in database tables and uses in every language mode of our site==>
https://django-modeltranslation.readthedocs.io/en/latest/installation.html
1- pip install django-modeltranslation
2- add 'modeltranslation' in installed apps
3- create translation.py in every app that want use and translate models of that
4- add fields of every models that want in a class(for every model must separate classes and set fields)==>
from modeltranslation.translator import TranslationOptions,register
from .models import Product
@register(Product)
class ProductTranslationOption(TranslationOptions):
fields = ['title']
5- python manage.py migrate and done.now in admin panel we can see some fields for translated meaning of fields we set