hyeonga_code

파이선 웹구축_장고_30_번역 기능 googletrans 본문

Python_Django

파이선 웹구축_장고_30_번역 기능 googletrans

hyeonga 2023. 6. 22. 05:59
반응형

- 번역 기능

- 라이브러리

- pip

- 'pypi.org'에서 다른 개발자들이 개발해 놓은 코드를 다운 받을 수 있도록 만든 프로그램

- import

- 다른 파일의 클래스, 함수, 변수를 가져다 쓰도록 만든 구문

 

1. 기능 설치

터미널

=====

'''

(mysite) C:\mysite\앱이름> pip install googletrans==4.0.0-rc1

Collecting googletrans==4.0.0-rc1

Using cached googletrans-4.0.0rc1.tar.gz (20 kB)

Preparing metadata (setup.py) ... done

Collecting httpx==0.13.3

Using cached httpx-0.13.3-py3-none-any.whl (55 kB)

Collecting certifi

Using cached certifi-2022.12.7-py3-none-any.whl (155 kB)

Collecting hstspreload

Using cached hstspreload-2023.1.1-py3-none-any.whl (1.5 MB)

Collecting sniffio

Using cached sniffio-1.3.0-py3-none-any.whl (10 kB)

Collecting chardet==3.*

Using cached chardet-3.0.4-py2.py3-none-any.whl (133 kB)

Collecting idna==2.*

Using cached idna-2.10-py2.py3-none-any.whl (58 kB)

Collecting rfc3986<2,>=1.3

Using cached rfc3986-1.5.0-py2.py3-none-any.whl (31 kB)

Collecting httpcore==0.9.*

Using cached httpcore-0.9.1-py3-none-any.whl (42 kB)

Collecting h11<0.10,>=0.8

Using cached h11-0.9.0-py2.py3-none-any.whl (53 kB)

Collecting h2==3.*

Using cached h2-3.2.0-py2.py3-none-any.whl (65 kB)

Collecting hyperframe<6,>=5.2.0

Using cached hyperframe-5.2.0-py2.py3-none-any.whl (12 kB)

Collecting hpack<4,>=3.0

Using cached hpack-3.0.0-py2.py3-none-any.whl (38 kB)

Installing collected packages: rfc3986, hyperframe, hpack, h11, chardet, sniffio, idna, hstspreload, h2, certifi, httpcore, httpx, googletrans

DEPRECATION: googletrans is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559

Running setup.py install for googletrans ... done

Successfully installed certifi-2022.12.7 chardet-3.0.4 googletrans-4.0.0rc1 h11-0.9.0 h2-3.2.0 hpack-3.0.0 hstspreload-2023.1.1 httpcore-0.9.1 httpx-0.13.3 hyperframe-5.2.0 idna-2.10 rfc3986-1.5.0 sniffio-1.3.0

 

[notice] A new release of pip available: 22.3.1 -> 23.0.1

[notice] To update, run: python.exe -m pip install --upgrade pip

'''

 

2. trans 앱 생성

 

3. 경로 설정 <config> > 'urls.py'

'urls.py'

=====

1
2
3
4
5
6
7
8
9
10
11
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from . import settings
 
urlpatterns = [
    path('admin/', admin.site.urls),
    path('acc/', include('acc.urls')),
    path('board/', include('board.urls')),
    path('trans/', include('trans.urls')),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
cs

4. 경로 설정 <trans> > 'urls.py'

'urls.py'

=====

1
2
3
4
5
6
7
from django.urls import path
from . import views
 
app_name = "trans"
urlpatterns = [
    path('index/', views.index, name="index"),
]
cs

5. 함수 설정 <trans> > 'views.py'

'views.py'

=====

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
from django.shortcuts import render
import googletrans
#_ 구글 번역
 
def index(request):
    bf = request.GET.get('bf','')
    fr = request.GET.get('fr','')
    to = request.GET.get('to','')
 
    context={
        "ndict" : googletrans.LANGUAGES,
        #_ 번역 포함 언어 불러오기
        'fr' : fr,
        'to' : to,
    }   
    if bf :
        trans = googletrans.Translator()
        #_ 번역가 생성
        after = trans.translate(bf, src=fr, dest=to)
        af = after.text
        sbf = gTTS(bf, lang=fr)
        saf = gTTS(af, lang=to)
        print(af)
        context.update({
            'bf' : bf,
            'af' : af,
            'sbf' : sbf,
            'saf' : saf
        })    
    return render(request, 'trans/index.html',context)
cs

6. 번역 페이지 작성 <templates> > <trans> > 'index.html'

'index.html'

=====

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
{% extends 'base.html' %}
{% block con %}
    <h1><b>SH TRANS</b></h1>
    <div class="row mt-5">
    #_ 제목과의 간격 mt-5
        <form>
            <div class="col-sm-5">
            #_ 가로 간격 sm-5
                <textarea class="form-control" name='bf' style="height: 250px;" value="en" placeholder="번역할 문장을 입력해주세요">{{bf}}</textarea><br>
                #_ 번역할 문장 입력칸(bf) 안에는 검색 후에나, 번역 체인지를 할 때에도 내용을 유지하기위해 {{bf}}
            </div>
            <div class="col-sm-2" style="margin:auto">
            #_ 버튼 위치 위아래 가운데 정렬 margin:aduto 
                <select class="form-select" name="fr">
                    {% for code, name in ndict.items %}
                        <option value="{{code}}" {% if fr == code %} selected {% endif %}>{{name|upper}}</option>
                        #_ fr이 코드와 같다면 선택됨을 유지시킴
                        #_ name|upper _ 대문자로 표시됨
                    {% endfor %}
                </select>
                <button class="btn btn-dark mt-4 mb-4" style="width:100%;">번역</button>
                <a href="{% url 'trans:index'%}?bf={{af}}&fr={{to}}&to={{fr}}"  class="btn btn-dark mt-2 mb-4" style="width:100%;">CHANGE</a>
                #_ 변경 시, url 검색 단어들을 지정하여 서로 변경해주는 작업
                <select class="form-select" name="to">
                    {% for code, name in ndict.items %}
                        <option value="{{code}}" {% if to == code %} selected {% endif %}>{{name|upper}}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="col-sm-5">
                <textarea name='to' class="form-control" style="height: 250px;" value="kr"disabled>{{af}}</textarea>
            </div>
        </form>
    </div>
{% endblock %}
cs

 

반응형