-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
280 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// 填入自己的OpenL apikey (https://openl.club/), 并修改文件名为 openl.json | ||
{ | ||
"apikey": "abcabcacb" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"status?": true, | ||
"text?": "HTML elements", | ||
"result?": "HTML元素", | ||
"source_lang?": "auto", | ||
"target_lang?": "zh" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import 'package:flutter/foundation.dart'; | ||
|
||
import 'dns_cache.dart'; | ||
import 'dns_cache.dart'; | ||
|
||
@immutable | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import 'package:flutter/foundation.dart'; | ||
|
||
import 'gallery_comment_span.dart'; | ||
|
||
@immutable | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,25 @@ | ||
export 'advance_search.dart'; | ||
export 'auto_lock.dart'; | ||
export 'cache_config.dart'; | ||
export 'local_fav.dart'; | ||
export 'eh_config.dart'; | ||
export 'simple_tag.dart'; | ||
export 'commit_vote_res.dart'; | ||
export 'dns_cache.dart'; | ||
export 'dns_config.dart'; | ||
export 'download_config.dart'; | ||
export 'auto_lock.dart'; | ||
export 'gallery_item.dart'; | ||
export 'download_task_info.dart'; | ||
export 'eh_config.dart'; | ||
export 'favcat.dart'; | ||
export 'dns_config.dart'; | ||
export 'gallery_cache.dart'; | ||
export 'gallery_comment.dart'; | ||
export 'gallery_comment_span.dart'; | ||
export 'gallery_item.dart'; | ||
export 'gallery_preview.dart'; | ||
export 'gallery_comment.dart'; | ||
export 'openl_translation.dart'; | ||
export 'gallery_tag.dart'; | ||
export 'gallery_torrent.dart'; | ||
export 'local_fav.dart'; | ||
export 'profile.dart'; | ||
export 'simple_tag.dart'; | ||
export 'tab_config.dart'; | ||
export 'dns_cache.dart'; | ||
export 'tab_item.dart'; | ||
export 'tag_group.dart'; | ||
export 'user.dart'; | ||
export 'download_config.dart'; | ||
export 'gallery_torrent.dart'; | ||
export 'advance_search.dart'; | ||
export 'tag_group.dart'; | ||
export 'tab_config.dart'; | ||
export 'favcat.dart'; | ||
export 'cache_config.dart'; | ||
export 'gallery_preview.dart'; | ||
export 'profile.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import 'package:flutter/foundation.dart'; | ||
|
||
import 'gallery_item.dart'; | ||
|
||
@immutable | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import 'package:flutter/foundation.dart'; | ||
|
||
|
||
@immutable | ||
class OpenlTranslation { | ||
|
||
const OpenlTranslation({ | ||
this.status, | ||
this.text, | ||
this.result, | ||
this.sourceLang, | ||
this.targetLang, | ||
}); | ||
|
||
final bool? status; | ||
final String? text; | ||
final String? result; | ||
final String? sourceLang; | ||
final String? targetLang; | ||
|
||
factory OpenlTranslation.fromJson(Map<String,dynamic> json) => OpenlTranslation( | ||
status: json['status'] != null ? json['status'] as bool : null, | ||
text: json['text'] != null ? json['text'] as String : null, | ||
result: json['result'] != null ? json['result'] as String : null, | ||
sourceLang: json['source_lang'] != null ? json['source_lang'] as String : null, | ||
targetLang: json['target_lang'] != null ? json['target_lang'] as String : null | ||
); | ||
|
||
Map<String, dynamic> toJson() => { | ||
'status': status, | ||
'text': text, | ||
'result': result, | ||
'source_lang': sourceLang, | ||
'target_lang': targetLang | ||
}; | ||
|
||
OpenlTranslation clone() => OpenlTranslation( | ||
status: status, | ||
text: text, | ||
result: result, | ||
sourceLang: sourceLang, | ||
targetLang: targetLang | ||
); | ||
|
||
|
||
OpenlTranslation copyWith({ | ||
bool? status, | ||
String? text, | ||
String? result, | ||
String? sourceLang, | ||
String? targetLang | ||
}) => OpenlTranslation( | ||
status: status ?? this.status, | ||
text: text ?? this.text, | ||
result: result ?? this.result, | ||
sourceLang: sourceLang ?? this.sourceLang, | ||
targetLang: targetLang ?? this.targetLang, | ||
); | ||
|
||
@override | ||
bool operator ==(Object other) => identical(this, other) | ||
|| other is OpenlTranslation && status == other.status && text == other.text && result == other.result && sourceLang == other.sourceLang && targetLang == other.targetLang; | ||
|
||
@override | ||
int get hashCode => status.hashCode ^ text.hashCode ^ result.hashCode ^ sourceLang.hashCode ^ targetLang.hashCode; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import 'package:flutter/foundation.dart'; | ||
|
||
import 'tab_item.dart'; | ||
|
||
@immutable | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import 'package:flutter/foundation.dart'; | ||
|
||
import 'gallery_tag.dart'; | ||
|
||
@immutable | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import 'package:flutter/foundation.dart'; | ||
|
||
import 'favcat.dart'; | ||
|
||
@immutable | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/// Language object with name and code (ISO) | ||
class Language { | ||
Language(this.code, this.name); | ||
final String name; | ||
final String code; | ||
|
||
@override | ||
String toString() => name; | ||
} | ||
|
||
/// Language list containing all languages supported by openl Translate API | ||
class LanguageList { | ||
static final Map<String, String> _langs = { | ||
'auto': 'Automatic', | ||
'zh': 'Chinese', | ||
'en': 'English', | ||
'de': 'German', | ||
'fr': 'French', | ||
'it': 'Italian', | ||
'ja': 'Japanese', | ||
'es': 'Spanish', | ||
'nl': 'Dutch', | ||
'pl': 'Polish', | ||
'pt': 'Portuguese', | ||
'ru': 'Russian', | ||
}; | ||
|
||
Language operator [](String code) { | ||
code = code.toLowerCase(); | ||
if (_langs.containsKey(code)) { | ||
return Language(code, _langs[code]!); | ||
} | ||
throw LanguageNotSupportedException('$code is not a supported language.'); | ||
} | ||
|
||
static bool contains(String codeOrLang) { | ||
if (_langs.containsKey(codeOrLang) || | ||
_langs.containsValue(codeOrLang.toCamelCase())) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
class LanguageNotSupportedException implements Exception { | ||
LanguageNotSupportedException(String lang) | ||
: msg = '$lang is not a supported language.'; | ||
|
||
final String msg; | ||
} | ||
|
||
extension _CamelCase on String { | ||
String toCamelCase() { | ||
return '${this[0].toUpperCase()}${substring(1).toLowerCase()}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import 'dart:convert' show jsonDecode, jsonEncode; | ||
|
||
import 'package:fehviewer/models/base/eh_models.dart'; | ||
import 'package:fehviewer/utils/openl/language.dart'; | ||
|
||
import 'package:http/http.dart' as http; | ||
|
||
const String _baseUrl = 'api.openl.club'; | ||
|
||
class OpenLTranslator { | ||
OpenLTranslator({required this.apikey}); | ||
|
||
final String apikey; | ||
|
||
Future<OpenlTranslation> translate( | ||
String sourceText, { | ||
String from = 'auto', | ||
String to = 'en', | ||
String service = 'deepl', | ||
}) async { | ||
for (final String each in [from, to]) { | ||
if (!LanguageList.contains(each)) { | ||
throw LanguageNotSupportedException(each); | ||
} | ||
} | ||
|
||
final String _path = '/services/$service/translate'; | ||
|
||
final Map<String, String> reqMap = { | ||
'apikey': apikey, | ||
'text': sourceText, | ||
'source_lang': from, | ||
'target_lang': to, | ||
}; | ||
|
||
final Uri url = Uri.https(_baseUrl, _path); | ||
final data = await http.post(url, body: jsonEncode(reqMap)); | ||
|
||
if (data.statusCode != 200) { | ||
throw http.ClientException('Error ${data.statusCode}: ${data.body}', url); | ||
} | ||
|
||
final jsonData = jsonDecode(data.body); | ||
if (jsonData == null) { | ||
throw http.ClientException('Error: Can\'t parse json data'); | ||
} | ||
|
||
return OpenlTranslation.fromJson(jsonData); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import 'dart:async'; | ||
import 'dart:convert'; | ||
|
||
import 'package:fehviewer/models/openl_translation.dart'; | ||
import 'package:fehviewer/utils/logger.dart'; | ||
import 'package:fehviewer/utils/openl/openl_translator.dart'; | ||
import 'package:flutter/services.dart'; | ||
|
||
class TranslatorHelper { | ||
static Future<String> getApikey() async { | ||
final String openl = await rootBundle.loadString('assets/openl.json'); | ||
final openlJson = json.decode(openl); | ||
return openlJson['apikey'] ?? ''; | ||
} | ||
|
||
static Future<OpenlTranslation> translate( | ||
String sourceText, { | ||
String from = 'auto', | ||
String to = 'en', | ||
String service = 'deepl', | ||
}) async { | ||
final String apikey = await getApikey(); | ||
|
||
logger.v('apikey $apikey'); | ||
|
||
final OpenLTranslator openLTranslator = OpenLTranslator(apikey: apikey); | ||
return openLTranslator.translate(sourceText, from: from, to: to); | ||
} | ||
|
||
static Future<String> translateText( | ||
String sourceText, { | ||
String from = 'auto', | ||
String to = 'en', | ||
String service = 'deepl', | ||
}) async { | ||
final OpenlTranslation rult = | ||
await translate(sourceText, from: from, to: to); | ||
|
||
logger.v(rult.toJson()); | ||
|
||
return rult.result ?? ''; | ||
} | ||
} |
Oops, something went wrong.