> # ♻️ 资源
> **大小:** 1.55MB
> **文档链接:**[**https://www.yuque.com/sxbn/ks/100010578**](https://www.yuque.com/sxbn/ks/100010578)
> **➡️ 资源下载:**[**https://download.csdn.net/download/s1t16/87404249**](https://download.csdn.net/download/s1t16/87404249)
> **注:更多内容可关注微信公众号【神仙别闹】,如当前文章或代码侵犯了您的权益,请私信作者删除!**
> ![qrcode_for_gh_d52056803b9a_344.jpg](https://cdn.nlark.com/yuque/0/2023/jpeg/2469055/1692147256036-49ec7e0c-5434-4963-b805-47e7295c9cbc.jpeg#averageHue=%23a3a3a3&clientId=u8fb96484-770e-4&from=paste&height=140&id=u237e511a&originHeight=344&originWidth=344&originalType=binary&ratio=1.25&rotation=0&showTitle=false&size=8270&status=done&style=none&taskId=ud96bf5f7-fe85-4848-b9c2-82251181297&title=&width=140.1999969482422)
# 在线翻译
## 1 需求分析
前端页面选择语言,输入文字,发送给服务器端,服务器端调用翻译 API 接口,将翻译结果传递至前端页面进行显示。
### 1.1 前端界面案例
![](https://cdn.nlark.com/yuque/0/2024/png/2469055/1713233623071-6a103930-1757-491f-bed3-56146c48baad.png#averageHue=%23f3f3f3&id=lwlYz&originHeight=210&originWidth=830&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)
### 1.2 参考资料
有道翻译 API:[http://fanyi.youdao.com/openapi](http://fanyi.youdao.com/openapi)
百度翻译 API:[https://api.fanyi.baidu.com/](https://api.fanyi.baidu.com/)
### 1.3 各项功能
#### 1.3.1 下拉菜单
![](https://cdn.nlark.com/yuque/0/2024/png/2469055/1713233623135-840d0600-8ffb-44cb-b612-424b3a429865.png#averageHue=%23fbfafa&id=GjceI&originHeight=277&originWidth=831&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)
下拉菜单的选项和个国语言的代码都是在页面加载的时候向后端发起请求,在数据中查找的,都是动态生成的。
#### 1.3.2 翻译
当输入中文时,可以点击不同的选项,翻译为对应的语言。
例如:
中文 == 英文
![](https://cdn.nlark.com/yuque/0/2024/png/2469055/1713233623190-5e1050dd-a830-4a56-979d-14b2749f84f8.png#averageHue=%23fdfcfc&id=TKYSU&originHeight=183&originWidth=830&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)
中文 == 日文
![](https://cdn.nlark.com/yuque/0/2024/png/2469055/1713233623262-0e940ea8-e1e0-4cd2-ad27-3584cafb351e.png#averageHue=%23fdfcfc&id=z7gt5&originHeight=147&originWidth=831&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)
## 2 系统设计
### 2.1 用户用例图
页面加载,初始化下拉菜单。
用户点击翻译按钮,发送请求获取翻译信息
![](https://cdn.nlark.com/yuque/0/2024/png/2469055/1713233623322-33f2aba0-041c-4e7f-b163-e1ddbe748995.png#averageHue=%23f5f5f5&id=shAOJ&originHeight=523&originWidth=830&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)
图 2-1 用户用例图
### 2.2 ER 图
数据库设计 1 张表,语言表,对应的 ER 图如图 3-1 所示。
LangName:语言名字
LangCode: 语言代码(对应百度 AP)![](https://cdn.nlark.com/yuque/0/2024/png/2469055/1713233623378-266a684b-eef9-427d-9c94-6b05ceda11a9.png#averageHue=%23fbfbfb&id=VPY2t&originHeight=429&originWidth=831&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)
图 3-1 数据库 ER 图
### 2.3 UML 类图(Class Diagram)
对于翻译功能模块,共设计如下 6 个类。
Entity 实体类 Lang.java:与数据库结构进行映射的类。主要由属性,setter, getter 方法组成,Entity 类中的属性与表中的字段相对应,每一个 Entity 类的对象都表示表中的每一条记录
pojo 对象类:
·Trans.java 为用户的翻译值对象,属性 query,from,to,分别表示请求翻译,翻译源语言,翻译目标语言。
·TransData.java:为值对象,属性 ser,dst,为原文与译文
·TransResult.java:为值对象,里面包含 TransData.java,从 API 请求的内容要用此对象存
Mapper 接口 LangMapper.java:主要定义操作的接口,定义一系列数据库的原子性操作,例如增删改查(通常称为 CRUD)等。
Serivce 层:主要处理业务逻辑,对于数据层的原子操作进行整合。在 TransService 里面调用百度 API 接口。
Controller 层:前端控制器,里面都是前需要请求调用的接口,主要包括前端下拉菜单的请求接口和前端翻译按钮的点击事件请求。
各类的结构及类之间的关系如图 2-5 所示:
![](https://cdn.nlark.com/yuque/0/2024/png/2469055/1713233623438-27f8a5c5-2399-4d65-af3b-5c3f5401f021.png#averageHue=%23fbfbf9&id=SUGPj&originHeight=569&originWidth=830&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)
图 2-5 翻译功能类图
### 2.4 UML 时序图(Sequence Diagram)
#### 2.4.1 翻译
![](https://cdn.nlark.com/yuque/0/2024/png/2469055/1713233623495-5a4147ff-4628-4ccd-abb5-fe893c7a3ce9.png#averageHue=%23f5f5f5&id=sWdoo&originHeight=392&originWidth=830&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)
#### 2.4.2 模块
每个功能按编号往后递增绘图
#### 2.4.3 模块
每个功能按编号往后递增绘图
## 3 系统实现
### 3.1 项目结构
![](https://cdn.nlark.com/yuque/0/2024/png/2469055/1713233623566-8153d26a-d259-4c36-a4c6-4c6fb28d1f0e.png#averageHue=%23faf9f8&height=757&id=M4OgT&originHeight=879&originWidth=425&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=&width=366)![](https://cdn.nlark.com/yuque/0/2024/png/2469055/1713233623635-988484c2-90ad-4b41-be10-2f24d7114fc0.png#averageHue=%23fbf6e2&id=dVTU3&originHeight=615&originWidth=366&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)
### 3.2 百度 API
在项目下新建 Source Folder,名为 resources
#### 3.2.1 百度 API 相关
TransApi.java
```
@Component
public class TransApi {
private static final String TRANS_API_HOST = "http://api.fanyi.baidu.com/api/trans/vip/translate";
private static String appid = "20201213000646199";
private static String securityKey = "qgXPVgAa6onAAbtZ5NjP";
public TransApi(){
}
public TransApi(String appid, String securityKey) {
this.appid = appid;
this.securityKey = securityKey;
}
/**
* 获得翻译结果
* @param query
* @param from
* @param to
* @return
*/
public String getTransResult(String query, String from, String to) throws IOException {
Map<String, String> params = buildParams(query, from, to);
JSONObject jsonObject = null;
//当请求翻译内容过长 用post
if (query.length() >= 2000){
//post请求方式
jsonObject = HttpUtil.doPostStr(TRANS_API_HOST, params);
}else {
//get请求方式
String url = getUrlWithQueryString(TRANS_API_HOST, params);
jsonObject = HttpUtil.doGetStr(url);
}
if (jsonObject.get("error_code")!=null) {
return "翻译失败,原因:"+jsonObject.get("error_msg");
}else{
TransResult transResult = JSON.parseObject(jsonObject.toString(), TransResult.class);
String s = JSON.toJSONString(transResult);
return s;
// System.out.println("transResult:"+s);
// return " 翻译结果 "+transResult.getTrans_result().get(0).getDst();
}
}
/**
* 构建参数map
*
* @param query
* @param from
* @param to
* @return
* @throws UnsupportedEncodingException
*/
private static Map<String, String> buildParams(String query, String from, String to){
Map<String, String> params = new HashMap<String, String>();
params.put("q", query);
params.put("from", from);
params.put("to"
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
详情介绍:https://www.yuque.com/sxbn/ks/100010578 前端页面选择语言,输入文字,发送给服务器端,服务器端调用翻译 API 接口,将翻译结果传递至前端页面进行显示。
资源推荐
资源详情
资源评论
收起资源包目录
100010578-基于Java实现在线翻译.zip (96个子文件)
translate
web应用开发课程设计报告-物联网11803孙汉明.docx 357KB
LICENSE 1KB
translate
bootstrap3
.DS_Store 6KB
js
html5shiv.min.js 2KB
bootstrap.js 59KB
npm.js 484B
holder.js 21KB
bootstrap.min.js 31KB
css3-mediaqueries.js 16KB
respond.min.js 4KB
font-awesome-4.7.0
css
font-awesome.css 37KB
font-awesome.min.css 30KB
HELP-US-OUT.txt 323B
scss
_animated.scss 715B
_core.scss 459B
_fixed-width.scss 120B
_icons.scss 49KB
_larger.scss 375B
_stacked.scss 482B
_screen-reader.scss 134B
_variables.scss 22KB
_path.scss 783B
font-awesome.scss 430B
_bordered-pulled.scss 592B
_rotated-flipped.scss 672B
_mixins.scss 2KB
_list.scss 378B
less
larger.less 370B
core.less 452B
screen-reader.less 118B
rotated-flipped.less 622B
stacked.less 476B
bordered-pulled.less 585B
fixed-width.less 119B
mixins.less 2KB
variables.less 22KB
font-awesome.less 495B
icons.less 49KB
animated.less 713B
list.less 377B
path.less 771B
fonts
FontAwesome.otf 132KB
fontawesome-webfont.eot 162KB
fontawesome-webfont.woff 96KB
fontawesome-webfont.woff2 75KB
fontawesome-webfont.svg 434KB
fontawesome-webfont.ttf 162KB
css
bootstrap-theme.min.css.map 25KB
bootstrap.min.css 107KB
bootstrap-theme.css.map 23KB
bootstrap-theme.css 21KB
bootstrap.css.map 216KB
bootstrap.css 129KB
bootstrap-theme.min.css 18KB
bootstrap.min.css.map 529KB
jquery.min.js 95KB
fonts
glyphicons-halflings-regular.svg 61KB
glyphicons-halflings-regular.ttf 40KB
glyphicons-halflings-regular.woff 23KB
glyphicons-halflings-regular.eot 20KB
glyphicons-halflings-regular.woff2 18KB
js
vue.js 333KB
axios.min.js 14KB
vue.min.js 91KB
index.html 4KB
translate-api
mvnw.cmd 6KB
pom.xml 4KB
src
test
java
com
shm
TranslateApiApplicationTests.java 1KB
main
resources
mapper
LangMapper.xml 205B
application.properties 1B
application.yml 616B
java
com
shm
TranslateApiApplication.java 324B
mapper
LangMapper.java 168B
controller
LangController.java 947B
TranslateController.java 773B
CodeGenerator.java 4KB
utils
HttpUtil.java 3KB
pojo
Trans.java 316B
TransData.java 185B
TransResult.java 301B
service
LangService.java 272B
impl
TransService.java 794B
LangServiceImpl.java 455B
baidu
MD5.java 4KB
TransApi.java 4KB
common
R.java 2KB
entity
Lang.java 737B
config
swaggerConfig.java 859B
MybatisPlusConfig.java 347B
.mvn
wrapper
maven-wrapper.properties 218B
maven-wrapper.jar 50KB
MavenWrapperDownloader.java 5KB
mvnw 10KB
.gitignore 395B
README.md 27KB
db
t_lang.sql 572B
共 96 条
- 1
资源评论
- SilverThrone2023-04-19果断支持这个资源,资源解决了当前遇到的问题,给了新的灵感,感谢分享~
神仙别闹
- 粉丝: 2671
- 资源: 7640
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 物模块模型代码,前往设计物模块所属
- Java面试手册,助力大家面试过五关斩六将,面试成功
- HITK0303MP-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- mybatis动态sql之xml增删改查批量操作示例EmpMapper.xml
- C/C++内存检测工具Sanitizers
- HITK0302MP-VB一款N-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- 宝塔批量建站工具,很优秀的宝塔管理工具,基于宝塔api
- HITK0204MP-VB一款N-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- azeryhgtfxhj
- 操作系统实验页面置换算法
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功