代码
package xxx. yyy. zzz ; import com. aliyun. alimt20181012. models. TranslateGeneralResponse ;
import org. apache. commons. lang3. StringUtils ; import java. util. * ;
import java. util. stream. Collectors ; public class AlibabaCloudTranslation { public static final String DEFAULT_ACCESS_KEY_ID = "你的id" ; public static final String DEFAULT_ACCESS_KEY_SECRET = "你的密码" ; public static final String ZH = "zh" ; public static final String EN = "en" ; public static final String TEXT = "text" ; public static final String MT_ALIYUNCS_COM = "mt.aliyuncs.com" ; public static final String GENERAL = "general" ; public static final String EXCEPTION = "翻译失败:[阿里云的id或密码错误][网络异常]" ; public static final char CHARACTER_UNDERLINE = '_' ; public static final char NULL_CHARACTER = ' ' ; public static final String DELIMITER_COMMA = "," ; public static com. aliyun. alimt20181012. Client createClient ( String accessKeyId, String accessKeySecret) throws Exception { com. aliyun. teaopenapi. models. Config config = new com. aliyun. teaopenapi. models. Config( ) . setAccessKeyId ( accessKeyId) . setAccessKeySecret ( accessKeySecret) ; config. endpoint = MT_ALIYUNCS_COM ; return new com. aliyun. alimt20181012. Client( config) ; } public static String getResults ( String text, String accessKeyId, String accessKeySecret) { if ( StringUtils . isEmpty ( text) ) { return "" ; } com. aliyun. alimt20181012. Client client; try { if ( StringUtils . isAllEmpty ( accessKeyId, accessKeySecret) ) { client = AlibabaCloudTranslation . createClient ( DEFAULT_ACCESS_KEY_ID , DEFAULT_ACCESS_KEY_SECRET ) ; } else client = AlibabaCloudTranslation . createClient ( accessKeyId, accessKeySecret) ; } catch ( Exception e) { throw new RuntimeException ( e. getMessage ( ) ) ; } com. aliyun. alimt20181012. models. TranslateGeneralRequest translateGeneralRequest = new com. aliyun. alimt20181012. models. TranslateGeneralRequest( ) . setFormatType ( TEXT ) . setSourceLanguage ( ZH ) . setTargetLanguage ( EN ) . setSourceText ( text) . setScene ( GENERAL ) ; com. aliyun. teautil. models. RuntimeOptions runtime = new com. aliyun. teautil. models. RuntimeOptions( ) ; TranslateGeneralResponse translateGeneralResponse; try { translateGeneralResponse = client. translateGeneralWithOptions ( translateGeneralRequest, runtime) ; } catch ( Exception error) { throw new RuntimeException ( EXCEPTION ) ; } return translateGeneralResponse. getBody ( ) . getData ( ) . getTranslated ( ) ; } public static Map < String , String > getResultsMap ( List < String > textList, String accessKeyId, String accessKeySecret) { String collect = textList. stream ( ) . distinct ( ) . collect ( Collectors . joining ( DELIMITER_COMMA ) ) ; String results = removeSpecialCharacters ( getResults ( collect, accessKeyId, accessKeySecret) ) ; List < String > collected = Arrays . stream ( results. split ( DELIMITER_COMMA ) ) . collect ( Collectors . toList ( ) ) ; Map < String , String > resultsMap = new LinkedHashMap < > ( 16 , 0.75f ) ; textList = Arrays . stream ( collect. split ( DELIMITER_COMMA ) ) . collect ( Collectors . toList ( ) ) ; int size = textList. size ( ) ; for ( int i = 0 ; i < size; i++ ) { resultsMap. put ( textList. get ( i) . trim ( ) , collected. get ( i) . trim ( ) ) ; } return resultsMap; } public static String removeSpecialCharacters ( String text) { return text. replaceAll ( "[\n`~!@#$%^&*()+=|{}':;\\[\\].<>/?!¥…()—【】‘;:”“’。,、?]" , "" ) ;
} public static String toUpperCaseWithUnderscore ( String text) { if ( text == null || text. isEmpty ( ) ) { return text; } StringBuilder result = new StringBuilder ( ) ; for ( int i = 0 ; i < text. length ( ) ; i++ ) { char c = text. charAt ( i) ; if ( c == NULL_CHARACTER ) { result. append ( CHARACTER_UNDERLINE ) ; } else { result. append ( Character . toUpperCase ( c) ) ; } } return result. toString ( ) ; } public static String toCamelCase ( String text) { if ( text == null || text. isEmpty ( ) ) { return text; } StringBuilder result = new StringBuilder ( ) ; boolean capitalizeNext = true ; for ( int i = 0 ; i < text. length ( ) ; i++ ) { char c = text. charAt ( i) ; if ( c == NULL_CHARACTER || c == CHARACTER_UNDERLINE ) { capitalizeNext = true ; } else if ( capitalizeNext) { result. append ( Character . toUpperCase ( c) ) ; capitalizeNext = false ; } else { result. append ( c) ; } } return result. toString ( ) ; } public static String toUnderCase ( String text) { if ( text == null ) { return null ; } int len = text. length ( ) ; StringBuilder res = new StringBuilder ( len + 2 ) ; char pre = 0 ; for ( int i = 0 ; i < len; i++ ) { char ch = text. charAt ( i) ; if ( Character . isUpperCase ( ch) ) { if ( pre != CHARACTER_UNDERLINE && pre != 0 ) { res. append ( CHARACTER_UNDERLINE ) ; } res. append ( Character . toLowerCase ( ch) ) ; } else { res. append ( ch) ; } pre = ch; } return res. toString ( ) ; }
}
阿里云翻译包
<dependency><groupId>com.aliyun</groupId><artifactId>alimt20181012</artifactId><version>1.1.0</version>
</dependency>