Skip to content

Commit

Permalink
LG-40116: update example for Terminology public API
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-petrehus committed Oct 30, 2023
1 parent 99c7cdb commit c89c257
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 51 deletions.
4 changes: 2 additions & 2 deletions apiconcepts/terminology/adding_terms.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Open the **MyTerminologyProviderViewerWinFormsUI.cs** class and go to the **AddT
* Display the newly-created entry in the Internet Explorer control of the **Termbase Viewer** window.

# [Adding Terms Functionality](#tab/tabid-1)
[!code-csharp[MyTerminologyProvider](code_samples/MyTerminologyProvider.cs#L67-L96)]
[!code-csharp[MyTerminologyProvider](code_samples/MyTerminologyProviderViewerWinFormsUI.cs#L66-L102)]
***

When a new source/target term pair has been added, the following will, for example, be displayed in the **Termbase Viewer** window:
Expand All @@ -27,7 +27,7 @@ You can also implement your terminology provider to support editing. However, in


# [Adding and Editing Terms](#tab/tabid-2)
[!code-csharp[MyTerminologyProviderViewerWinFormsUI](code_samples/MyTerminologyProviderViewerWinFormsUI.cs#L60-L63)]
[!code-csharp[MyTerminologyProviderViewerWinFormsUI](code_samples/MyTerminologyProviderViewerWinFormsUI.cs#L59-L64)]
***

When you try to add a term that has already been added, <Var:ProductName> throws the following message that prompts you to:
Expand Down
113 changes: 83 additions & 30 deletions apiconcepts/terminology/code_samples/MyTerminologyProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

namespace SDL_Terminology_Provider_Plug_in
{
public class MyTerminologyProvider : AbstractTerminologyProvider
public class MyTerminologyProvider : ITerminologyProvider
{

private List<IEntry> _entry = new List<IEntry>();

private List<Entry> _entry = new List<Entry>();


#region "FileName"
//Stores the glossary text file name and path
Expand All @@ -30,11 +30,11 @@ public MyTerminologyProvider(string providerSettings)
#region "Definition"
//Creates the terminology source definition, i.e. the languages in the glossary and any
//additional descriptive fields, in this case the 'Definition' field
public override IDefinition Definition
public Definition Definition
{
get
{
return new Definition(GetDescriptiveFields(), GetLanguages().Cast<IDefinitionLanguage>().ToList());
return new Definition(GetDescriptiveFields(), GetLanguages().Cast<DefinitionLanguage>().ToList());
}
}
#endregion
Expand All @@ -43,9 +43,9 @@ public override IDefinition Definition
// Creates the termbase definition by declaring the Definition text field.
// This allows our terminology provider to also display the Definition field content
// in the Termbase Search and Terminology Recognition windows.
public IList<IDescriptiveField> GetDescriptiveFields()
public IList<DescriptiveField> GetDescriptiveFields()
{
var result = new List<IDescriptiveField>();
var result = new List<DescriptiveField>();

var definitionField = new DescriptiveField
{
Expand All @@ -61,23 +61,23 @@ public IList<IDescriptiveField> GetDescriptiveFields()

#region "NameDescrptionUri"
//Return the terminology provider name, uri, and description to show in the Studio UI
public override string Description
public string Description
{
get
{
return PluginResources.My_Terminology_Provider_Description;
}
}

public override string Name
public string Name
{
get
{
return PluginResources.My_Terminology_Provider_Name;
}
}

public override Uri Uri
public Uri Uri
{
get
{
Expand All @@ -89,14 +89,14 @@ public override Uri Uri
#region "GetEntry"
// Returns the entry for a search result. The entries are associated with the search results
// through the entry id.
public override IEntry GetEntry(int id)
public Entry GetEntry(int id)
{
return _entry.FirstOrDefault(_entry => _entry.Id == id);
}
#endregion

#region GetEntryExt"
public override IEntry GetEntry(int id, IEnumerable<ILanguage> languages)
public Entry GetEntry(int id, IEnumerable<ILanguage> languages)
{
return _entry.FirstOrDefault(_entry => _entry.Id == id);
}
Expand All @@ -106,27 +106,27 @@ public override IEntry GetEntry(int id, IEnumerable<ILanguage> languages)
//We parse the first line of the glossary text file to retrieve the the source and target language.
//Then we create two language objects that we return as source and target language to populate the
//termbase language dropdown lists in Studio.
public override IList<ILanguage> GetLanguages()
public IList<ILanguage> GetLanguages()
{
StreamReader _inFile = new StreamReader(fileName.Replace("file:///", ""));
string[] languages = _inFile.ReadLine().Split(';');
string srgLanguage = languages[1], trgLanguage = languages[2];
string srgLanguage = languages[0], trgLanguage = languages[1];
string srcLabel = srgLanguage.Split(',')[0], srcLocale = srgLanguage.Split(',')[1];
string trgLabel = trgLanguage.Split(',')[0], trgLocale = trgLanguage.Split(',')[1];
_inFile.Close();

var result = new List<IDefinitionLanguage>();
var result = new List<DefinitionLanguage>();

var tbSrcLanguage = new DefinitionLanguage
{
Name = srcLabel,
Locale = new System.Globalization.CultureInfo(srcLocale)
Locale = new Sdl.Core.Globalization.CultureCode(srcLocale)
};

var tbTrgLanguage = new DefinitionLanguage
{
Name = trgLabel,
Locale = new System.Globalization.CultureInfo(trgLocale)
Locale = new Sdl.Core.Globalization.CultureCode(trgLocale)
};


Expand All @@ -141,8 +141,8 @@ public override IList<ILanguage> GetLanguages()
#region "Search"
//Is executed when the user launches a lookup operation in the Termbase Search window,
//or when the user moves to a segment in the Editor of Studio. Moving to a segment
//automatically launches a fuzzy search to retrieve any known terminology.
public override IList<ISearchResult> Search(string text, ILanguage source, ILanguage destination, int maxResultsCount, SearchMode mode, bool targetRequired)
//automatically launches a fuzzy search to retrieve any known terminology.
public IList<SearchResult> Search(string text, ILanguage source, ILanguage destination, int maxResultsCount, SearchMode mode, bool targetRequired)
{
string[] chunks;
List<string> hits = new List<string>();
Expand Down Expand Up @@ -170,9 +170,9 @@ public override IList<ISearchResult> Search(string text, ILanguage source, ILang
hits.Add(thisLine);
}
}

// Create search results object (hitlist)
var results = new List<ISearchResult>();
var results = new List<SearchResult>();

for (int i = 0; i < hits.Count; i++)
{
Expand All @@ -193,29 +193,29 @@ public override IList<ISearchResult> Search(string text, ILanguage source, ILang

results.Add(result);
}
return results;
return results;
}
#endregion

#region "ConstructEntryContent"
// This helper function is used to construct the entry content with entry id, source and target term, and
// definition (if applicable)
private IEntry CreateEntry(string id, string sourceTerm, string targetTerm, string definitionText, string targetLanguage)
private Entry CreateEntry(string id, string sourceTerm, string targetTerm, string definitionText, string targetLanguage)
{
// Assign the entry id
IEntry thisEntry = new Entry
Entry thisEntry = new Entry
{
Id = Convert.ToInt32(id)
};

// Add the target language
IEntryLanguage trgLanguage = new EntryLanguage
EntryLanguage trgLanguage = new EntryLanguage
{
Name = targetLanguage
};

// Create the target term
IEntryTerm _term = new EntryTerm
EntryTerm _term = new EntryTerm
{
Value = targetTerm
};
Expand All @@ -224,17 +224,70 @@ private IEntry CreateEntry(string id, string sourceTerm, string targetTerm, stri

// Also add the definition (if available)
if (definitionText != "")
{
IEntryField _definition = new EntryField
{
EntryField _definition = new EntryField
{
Name = "Definition",
Value = definitionText
};
thisEntry.Fields.Add(_definition);
}
}

return thisEntry;
}
#endregion

#region "Initialize provider"
public void SetDefault(bool value)
{
return;
}

public bool Initialize()
{
return true;
}

public bool Initialize(TerminologyProviderCredential credential)
{
return true;
}

public bool IsProviderUpToDate()
{
return true;
}

public IList<FilterDefinition> GetFilters()
{
return new List<FilterDefinition>();
}

public bool Uninitialize()
{
throw new NotImplementedException();
}

public string Id => "id";

public TerminologyProviderType Type => TerminologyProviderType.Custom;

public bool IsReadOnly => false;

public bool SearchEnabled => true;

public FilterDefinition ActiveFilter
{
get { return null; }
set { value = new FilterDefinition(); }
}

public bool IsInitialized => true;

public void Dispose()
{

}
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace SDL_Terminology_Provider_Plug_in
[TerminologyProviderViewerWinFormsUI]
public class MyTerminologyProviderViewerWinFormsUI : ITerminologyProviderViewerWinFormsUI
{

private MyTerminologyProvider _terminologyProvider;


#region "ControlObject"
private TermProviderControl termControl;
Expand Down Expand Up @@ -40,7 +40,7 @@ public bool Initialized
}
#endregion

public IEntry SelectedTerm
public Entry SelectedTerm
{
get
{
Expand All @@ -57,12 +57,18 @@ public IEntry SelectedTerm
public event EventHandler TermChanged;

#region "AddEditTerm"
public void AddAndEditTerm(IEntry term, string source, string target)
public void AddAndEditTerm(Entry term, string source, string target)
{
MessageBox.Show("Sorry, editing terms is currently not implemented :-(", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
#endregion

#region "Enable term adding and editing
public bool CanAddTerm => true;

public bool IsEditing => true;
#endregion

#region "AddTerms"
//Triggered when the Studio user selects a source (and target) term, and then clicks the
//corresponding button for adding the term or term pair to the glossary.
Expand All @@ -87,22 +93,27 @@ public void AddTerm(string source, string target)
//Show added entry in web browser control of the Termbase Viewer window
string tmpFile = System.IO.Path.GetTempPath() + "simple_list_entry.htm";
StreamWriter previewFile = new StreamWriter(tmpFile);
previewFile.Write("<html><body><b>Entry id:</b> " + newEntryId.ToString() +
"<br/><b>Source term:</b> " + source +
"<br/><b>Target term:</b> " + target +
previewFile.Write("<html><body><b>Entry id:</b> " + newEntryId.ToString() +
"<br/><b>Source term:</b> " + source +
"<br/><b>Target term:</b> " + target +
"</body></html>");
previewFile.Close();
termControl.webBrowser.Navigate(tmpFile);
termControl.SetNavigation(tmpFile);
}

public void CancelTerm()
{
throw new NotImplementedException();
}
#endregion

public void EditTerm(IEntry term)
public void EditTerm(Entry term)
{
// Not used in this implementation.
}

#region "InitializeProvider"
public void Initialize(ITerminologyProvider terminologyProvider, CultureInfo source, CultureInfo target)
public void Initialize(ITerminologyProvider terminologyProvider, CultureCode source, CultureCode target)
{
_terminologyProvider = (MyTerminologyProvider)terminologyProvider;
}
Expand All @@ -111,7 +122,7 @@ public void Initialize(ITerminologyProvider terminologyProvider, CultureInfo sou
#region "JumpToTerm"
// this function outputs the full entry content in the Internet Explorer control
// of the Termbase Viewer window
public void JumpToTerm(IEntry entry)
public void JumpToTerm(Entry entry)
{
// Load the glossary file
string fileName = _terminologyProvider.fileName.Replace("file:///", "");
Expand All @@ -125,11 +136,11 @@ public void JumpToTerm(IEntry entry)
string thisLine = glossaryFile.ReadLine();
chunks = thisLine.Split(';');
string thisId = chunks[0];
if(thisId==entry.Id.ToString())
if (thisId == entry.Id.ToString())
{
entryContent = thisLine;
break;
}
}
}

// Parse the line alongside the semi-colon
Expand All @@ -144,7 +155,7 @@ public void JumpToTerm(IEntry entry)
"<br/><b>Definition:</b> " + chunks[3] +
"</body></html>");
previewFile.Close();
termControl.webBrowser.Navigate(tmpFile);
termControl.SetNavigation(tmpFile);

glossaryFile.Close();
}
Expand All @@ -155,8 +166,14 @@ public void Release()
{
_terminologyProvider = null;
}

public void SaveTerm()
{
return;
}
#endregion

/// Check if this component supports the specified terminology provider URI
public bool SupportsTerminologyProviderUri(Uri terminologyProviderUri)
{
return true;
Expand Down
Loading

0 comments on commit c89c257

Please sign in to comment.