public static String Translate(String source, String sourceType, String destType, String appId = null, String key = null) { String dest = source; WebClient client = new WebClient(); String usedAppId = appId ?? TranslateApi.AppId; String usedKey = key ?? TranslateApi.Key; String random = TranslateApi.GetRandomStr(); String checkout = usedAppId + source + random + usedKey; checkout = MD5(checkout); String uri = String.Format( TranslateApi.BaseAddress + "? q={0}&from={1}&to={2}&appid={3}&salt={4}&sign={5}", source, sourceType, destType, usedAppId, random, checkout ); var response = client.DownloadString(uri); var packet = ResponsePacket.From(response); if (packet.Successed) { dest = packet.Results[0].Dest; } return(dest); }
public void AugmentQuickInfoSession(IQuickInfoSession session, IList <object> quickInfoContent, out ITrackingSpan applicableToSpan) { // Map the trigger point down to our buffer. SnapshotPoint?subjectTriggerPoint = session.GetTriggerPoint(m_subjectBuffer.CurrentSnapshot); if (!subjectTriggerPoint.HasValue) { applicableToSpan = null; return; } ITextSnapshot currentSnapshot = subjectTriggerPoint.Value.Snapshot; SnapshotSpan querySpan = new SnapshotSpan(subjectTriggerPoint.Value, 0); //look for occurrences of our QuickInfo words in the span ITextStructureNavigator navigator = m_provider.NavigatorService.GetTextStructureNavigator(m_subjectBuffer); TextExtent extent = navigator.GetExtentOfWord(subjectTriggerPoint.Value); string searchText = extent.Span.GetText(); searchText = searchText?.Trim(); String text = null; if (!String.IsNullOrEmpty(searchText)) { text = TranslateApi.TranslateEn_Zh(searchText); applicableToSpan = currentSnapshot.CreateTrackingSpan ( //querySpan.Start.Add(foundIndex).Position, 9, SpanTrackingMode.EdgeInclusive extent.Span.Start, text.Length, SpanTrackingMode.EdgeInclusive ); quickInfoContent.Add(text); return; } //foreach (string key in m_dictionary.Keys) //{ // int foundIndex = searchText.IndexOf(key, StringComparison.CurrentCultureIgnoreCase); // if (foundIndex > -1) // { // applicableToSpan = currentSnapshot.CreateTrackingSpan // ( // //querySpan.Start.Add(foundIndex).Position, 9, SpanTrackingMode.EdgeInclusive // extent.Span.Start + foundIndex, key.Length, SpanTrackingMode.EdgeInclusive // ); // string value; // m_dictionary.TryGetValue(key, out value); // if (value != null) // quickInfoContent.Add(value); // else // quickInfoContent.Add(""); // return; // } //} applicableToSpan = null; }
public static String TranslateZh_En(String source) { return(TranslateApi.Translate(source, Language.Zh, Language.En)); }
public static String TranslateEn_Zh(String source) { return(TranslateApi.Translate(source, Language.En, Language.Zh)); }