Uploaded by ynoneqjnjwokqrwdvs

Translate app Documentation (1)

advertisement
1. Introduction:
Mobile Translator is a mobile application that allow users to translate text from English to
any other language. The problem of language differences has hampered effective
information communication for years. There have been difficulties in information
communication between the countries for years. In modern times, language interpreters
should understand and speak both translation and verse-visa languages. This traditional
approach used to solve the problem of language differences has not been fruitful and
favorable. Also, learning different languages can be difficult because of language differences.
So, Develop an Android application that translate text in different language. DeepL
translation API [Refer this. https://www.deepl.com/docs-api] uses natural language
processing with Java programming language to develop the language translation application.
This application can be useful for travelers for communication purposes, thus they can
integrate with the locals and get the right information.This application can also saved the
translation.The saved translated text can be read without internet.This application keep
track of user translation.
2. Application Structure:
The android project contains different types of app modules, source code files, and resource
files. We will explore all the folders and files in the android app.
1. Manifests folder:
Manifests
folder
contains
AndroidManifest.xml for creating the
android application. This file contains
information about application such as
android version, metadata, states package
for Kotlin file and other application
components.
2. Java folder:
Java folder contains all the java and Kotlin
source code (.java) files that we create
during the app development, including
other Test files.
3. Res folder:
The resource folder is the most important
folder because it contains all the non-code
sources as mentioned below:a. Drawable folder:
It contains the different types of images
used for the development of the
application.
b. Layout folder:
The layout folder contains all XML layout
files which we used to define the user
interface of an application.
c. Mipmap folder:
This folder contains launcher.xml files to
define icons that are used to show on the 4. Gradle Scripts
home screen. It contains different density
Gradle means automated build system and it
types of icons depending upon the size of
contains number of files which are used to
the device such as hdpi, mdpi, xhdpi.
define a build configuration which can be apply
d. Values folder:
to all modules in an application. In build.gradle
Values folder contains a number of XML
(Project) there are buildscripts and in
files like strings, dimensions, colors and
build.gradle
(Module)
plugins
and
styles definitions
implementations are used to build
configurations that can be applied to all our
application modules.
2.1 AndroidManifest.xml
The
AndroidManifest.xml
file
contains
information of your package, including
components of the application such as
activities, services, broadcast receivers, content
providers etc.
<application>
application is the subelement of the manifest. It
includes the namespace declaration. This element
contains several subelements that declares the
application component such as activity etc.
The elements used in the above xml file are The commonly used attributes are of this element are
described below.
icon, label, theme etc.
<manifest>
android:icon represents the icon for all the android
manifest is the root element of the application components.
AndroidManifest.xml file. It has package android:label works as the default label for all the
attribute that describes the package name of application components.
the activity class.
android:theme represents a common theme for all
<activity>
the android activities.
activity is the subelement of application and <intent-filter>
represents an activity that must be defined in
the AndroidManifest.xml file. It has many intent-filter is the sub-element of activity that
attributes such as label, name, theme, describes the type of intent to which activity, service
or broadcast receiver can respond to.
launchMode etc.
android:label represents a label i.e. displayed
on the screen.
android:name represents a name for the
activity class. It is required attribute.
2.2 Gradle Scripts:
Gradle means automated build system and it contains
number of files which are used to define a build
configuration which can be apply to all modules in an
application.
There are two build.gradle files for every android
studio project of which, one is for application and other
is for project level(module level) build files.
1. build.gradle (project level) :
2. build.gradle (application level)
The Application level build.gradle file is located
in each module of the android project. This file
includes your package name as applicationID,
version name(apk version), version code,
minimum and target sdk for a specific
application module. When requirement to add
an external libraries then need to mention it in
the app level gradle file to include them in a
project as dependencies of the application.
Below dependencies added in project:
The Top level (module) build.gradle file is project level
build file, which defines build configurations at project
level. This file applies configurations to all the modules
● implementation
in android application project.
●
●
●
'com.squareup.retrofit2:retrofit:2.9.0'
implementation
'com.google.code.gson:gson:2.8.9'
implementation
'com.squareup.retrofit2:converter-gson:2.9.0'
implementation
'com.kaopiz:kprogresshud:1.2.0'
Automatically added in a project:●
●
●
implementation
'androidx.appcompat:appcompat:1.4.1'
implementation
'com.google.android.material:material:1.5.0'
implementation
'androidx.constraintlayout:constraintlayout:2.1.
3'
2.3 Java Folder:
Java folder contains all the java source code (.java) files that we create during the app
development, including other Test files.
1. Adapter:
Adapter is an interface whose implementations
provide data and control the display of that data.
ListViews own adapters that completely control
the ListView’s display. So adapters control the
content displayed in the list as well as how to
display it.
2. API:
An API defines a set of functions which perform
requests and receive responses via HTTP protocol
such as GET and POST. In this application, Retrofit
use as a REST Client library (Helper Library) to
create an HTTP request and also to process the
HTTP response from a REST API. To use Retrofit in
an application,
3. Constant:
Constant folder contains constant files and
function used as multiple time in source code and
its value/result won’t changed .
4. Database:
This folder contains classes that used to store data
locally.
5. Model:
This folder contains multiple model class. Model
class is a class which represents data object which
can be used for transferring data in java
application.
6. Screen:
This folder contain multiple activities.An activity
represents a single screen with a user interface just
like window or frame of Java.Android activity is the
subclass of ContextThemeWrapper class.
3. Classes:
There are main five classes used in an application.They are:
1. SplashScreen.java:
This splash class is an activity class.This class display splash screen.Splash Screen is the
first screen visible to the user when the application’s launched.This activity set content of
activity_splash_screen.xml which is in layout folder.
2. SignInScreen.java:
This signin class is an activity class that display sign in screen which opened after splash
screen. It contain various method and function to authenticate the user and redirect to
main page of the app.This activity set content of activity_signin_screen.xml which is in
layout folder.
3. SignUpScreen.java:
This signup class is an activity class that display sign up screen. It contain various method
and function that register user in an application .This activity set content of
activity_sign_up_screen.xml which is in layout folder.
4. TranslationScreen.java:
This translation class is an activity class that display translation screen . It manage
translation
between
languages.
This
activity
set
content
of
activity_translation_screen.xml which is in layout folder.
5. TranslationListScreen.java:
This translationlist class is an activity class that display saved translation list in a screen
.This activity set content of activity_translation_list.xml which is in layout folder.
Following classes are used in retrofit:
1.
Model class:
This class contains the objects to be obtained from the JSON file.
2.
ApiClient.java:
This class is a Retrofit.Builder class. Its Instance uses the interface and the Builder API to
allow defining the URL end point for the HTTP operations.
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
3.
ApiInterface.java:
It is an Interfaces that define all the API endpoints.
4. Relationship between classes:
1. SignInScreen.java:
This SignIn class is used to authenticate user. Following class instance used in SignIn class:
● Utils.class:
This class is used to check internet connectivity.
● ApiClient.class:
ApiClient uses to create instance of ApiInterface.java.
● ApiInterface.class:
The instances of ApiInterface is used to make logIn API call using end points.
@FormUrlEncoded
@POST("logIn")
Call<JsonObject> logIn(@Field("email") String email,
@Field("password") String password);
A call<results> object that will return the information from the API.
● User.class:
A user class is a POJO class that automatically parse the JSON data which return
as result from API using Gson in background.User class contain getter/setter
methods for parsing user data like email.password and userId.
● MyPref.class:
This class is used to store user data in a local storage of an application.
2. SignUpScreen.java:
This SignIn class is used to register user. Following class instance used in SignUp class:
● Utils.class:
This class is used to check internet connectivity.
● ApiClient.class:
ApiClient uses to create instance of ApiInterface.java.
● ApiInterface.class:
The instances of ApiInterface is used to make SignUp API call using end points
(signUp).
@FormUrlEncoded
@POST("signUp")
Call<JsonObject> signUp(@Field("email") String email,
@Field("password") String password);
A call<results> object that will return the information from the API.
● User.class:
A user class is a POJO class that automatically parse the JSON data which return
as result from API using Gson in background.
● MyPref.class:
This class is used to store user data in a local storage of an application.
3. TranslationScreen.java:
This Translation class is used to translate text from one language to another. And also
provide button to saved translation.It contain Logout button to sign out from the
application. Following class instance used in TranslationScreen class:
● ArrayAdapter.class:
An adapter is a bridge between ListView component and data source. It holds the
language data and send it to adapter view then view can takes the data from the
adapter view and shows the data on Language List View.
● Utils.class:
This class is used to check internet connectivity.
● ApiClient.class:
ApiClient uses to create instance of ApiInterface.java.
● ApiInterface.class:
The instances of ApiInterface is used to make translation API and saved
translation API call using end points (translate and savedTranslation).
@FormUrlEncoded
@POST("translate")
Call<JsonObject> translation(@Field("auth_key") String auth_key,
@Field("source_lang") String source_lang,
@Field("text") String text,
@Field("target_lang") String to_type);
Auth-key : This key generated from DeepL account.
@FormUrlEncoded
@POST("savedTranslation")
Call<JsonObject> saveTranslation(@Field("user_id") String user_id,
@Field("from_type") String from_type,
@Field("from_text") String from_text,
@Field("to_type") String to_type,
@Field("to_text") String to_text);
A call<results> object that will return the information from the API.This result will
bind into text view.
4. TranslationList.java:
This class is used to show all the saved translation text in a list. Particular text can be saved
locally using SQLite and delete from list.
RecyclerView is used to show saved translation list in an application. RecyclerView is the
ViewGroup that contains the views corresponding to data as per requirement.
Following class instance used in TranslationList class:
● Utils.class:
This class is used to check internet connectivity.
● ApiClient.class:
ApiClient uses to create instance of ApiInterface.java.
● ApiInterface.class:
The instances of ApiInterface is used to get saved translation list from api call using
end points (translation_list);
@GET("getAllTranslation")
Call<JsonObject> getAllTranslation(@Query("user_id") String user_id);
A call<results> object that will return json array from the API.The json array
converted into list of translation word model using gson converter. Gson
convertor
used
in
an
application
by
adding
dependencies
'com.squareup.retrofit2:converter-gson:2.9.0' in build.gradle application module.
Following api call used in translation list class:
DeleteTranslation:
@FormUrlEncoded
@POST("deleteTranslation")
Call<JsonObject>
deleteTranslation(@Field("translation_id")
translationID);
This api is used to delete translation from saved translation list.
● TranslationWord Model:
String
This model contain translated word getter/setter method which helps in
translation adapter to display data.
● TranslationAdapter.class:
The
TranslationAdapter
extending
RecyclerView.Adapter
class.The
RecyclerView.Adapter, works same as ArrayAdapter, will populate the data into
the RecyclerView. It also converts a Java object into an individual list item View to
be inserted and displayed.
The RecyclerView.Adapter requires a ViewHolder.
A ViewHolder is an object that stores multiple Views inside the tag field of the
Layout so they can be immediately loaded. This also improves application
performance.
The RecyclerView.Adapter has three primary methods: onCreateViewHolder(),
onBindViewHolder(), and getItemCount().
● onCreateViewHolder() inflates an translation_list_row XML layout and
returns a ViewHolder.
● onBindViewHolder() sets the various information in the list item View
through the ViewHolder. This is the moment when the data from
Translation Word model gets associated, aka "bound" to recycler view.
● getItemCount() returns the number of items the RecyclerView.
● DatabaseHandler class:
SQLite is a opensource SQL database that stores data to a text file on a device.
SQLite supports all the relational database features.
As per requirement,an Application stored translated text in to local storage using
SQLite class.DatabaseHandler class is used to implement SQLiteOpenHelper in an
application.
SQLiteOpenHelper provide methods for creating/upgrading database and
inserting/delete data into/from table.
Declare following method in DatabaseHandler:
● addTranslatedWord: This method is used to add translation word into
translation table.
● getAllSavedWords: This method is used to get all translation word from
translation table.
● deleteSavedWord:This method is used to delete translation word from
translation table.
● deleteTables:This method is used to delete translation table.
● isSavedWord:This method is used to check translation word is saved or
not in translation table.
Download