Class TranslatedText

java.lang.Object
de.intranda.metadata.multilanguage.MultiLanguageMetadataValue
io.goobi.viewer.model.translations.TranslatedText
All Implemented Interfaces:
de.intranda.metadata.multilanguage.IMetadataValue, IPolyglott, Serializable

public class TranslatedText extends de.intranda.metadata.multilanguage.MultiLanguageMetadataValue implements IPolyglott, Serializable
Translations for some text for a set of locales. Text can be set and retrieved for each locale individually. There may be a "default language" text which represent text that has not actual locale, whether it is text that has no translations or is an internal representation of the text. This default language only exists if the text is initiated giving only a single text without locale or if it is explicitly added
Author:
Florian Alpers
See Also:
  • Constructor Details

    • TranslatedText

      public TranslatedText()
      Create a text with the locales from IPolyglott.getLocalesStatic().
    • TranslatedText

      public TranslatedText(Collection<Locale> locales)
      Creates a text with the given locales.
      Parameters:
      locales - collection of locales to initialize the text with
    • TranslatedText

      public TranslatedText(Collection<Locale> locales, Locale initalLocale)
      Creates a text with the given locales, setting the selected locale to the given initialLocale.
      Parameters:
      locales - collection of locales to initialize the text with
      initalLocale - locale to select as the active locale
    • TranslatedText

      public TranslatedText(TranslatedText orig, Collection<Locale> locales, Locale initialLocale)
      Create a text with the given locales, setting the selected locale to the given initialLocale. Initialize the values with the onces from the given TranslatedText "orig". If a Locale contained in locales is not included in orig, its value remains blank. If a locale is contained in orig, but not in locales, the value is set anyway
      Parameters:
      orig - source translated text to copy values from
      locales - collection of locales to initialize the text with
      initialLocale - locale to select as the active locale
    • TranslatedText

      public TranslatedText(TranslatedText orig)
      Creates a copy of the given Translated text "orig".
      Parameters:
      orig - source translated text to copy
    • TranslatedText

      public TranslatedText(String text)
      Creates a text with the default language set to the given value.
      Parameters:
      text - initial text value for the default language
    • TranslatedText

      public TranslatedText(de.intranda.metadata.multilanguage.IMetadataValue orig)
      Create a text using the values of the given IMetadataValue orig. If orig is a SimpleMetadataValue, its value is written to the default locale. The selected locale is set to IPolyglott.getCurrentLocale()
      Parameters:
      orig - source metadata value to copy translations from
    • TranslatedText

      public TranslatedText(de.intranda.metadata.multilanguage.IMetadataValue orig, Locale initialLocale)
      Create a text using the values of the given IMetadataValue orig. If orig is a SimpleMetadataValue, its value is written to the default locale. The selected locale is set to the given initialLocale
      Parameters:
      orig - source metadata value to copy translations from
      initialLocale - locale to select as the active locale
  • Method Details

    • getSelectedLocale

      public Locale getSelectedLocale()
      Specified by:
      getSelectedLocale in interface IPolyglott
      Returns:
      the selectedLocale
    • setSelectedLocale

      public void setSelectedLocale(Locale locale)
      Set the selectedLocale.
      Specified by:
      setSelectedLocale in interface IPolyglott
      Parameters:
      locale - locale to set as the selected locale
    • getText

      public String getText(Locale locale)
      Get the text for the given locale. If that does not exist, get the value for the default language and if that doesn't exist, an empty string
      Parameters:
      locale - locale for which to retrieve the text
      Returns:
      Text for the given locale
    • getTextOrDefault

      public String getTextOrDefault()
      Get the text for IPolyglott.getCurrentLocale(), or, failing that, for IPolyglott.getDefaultLocale(), the internal default language or finally an empty string.
      Returns:
      String
    • getTextOrDefault

      public String getTextOrDefault(Locale locale, Locale defaultLocale)
      Gets the text for the given language, or, failing that, for the given defaultLocale, the internal default language or finally an empty string.
      Parameters:
      locale - The locale to return the text for
      defaultLocale - The fallback locale to use if no text exists for the given locale
      Returns:
      String
    • setText

      public void setText(String text, Locale locale)
      Sets the text for the given locale.
      Parameters:
      text - text value to set
      locale - locale for which to set the text
    • getText

      public String getText()
      get the text for the current selectedLocale.
      Returns:
      Text for selected locale
    • setText

      public void setText(String text)
      set the text for the current selectedLocale.
      Parameters:
      text - text value to set for the selected locale
    • toMap

      public Map<Locale,String> toMap()
      Get the values as a map of locales and associated texts. The default language text is never included since it does not have a locale
      Returns:
      Map<Locale, String>
    • isComplete

      public boolean isComplete(Locale locale)
      Alias for isValid(Locale).
      Specified by:
      isComplete in interface IPolyglott
      Parameters:
      locale - the locale to check for completeness
      Returns:
      true if a non-empty text is set for the given locale; false otherwise
    • isValid

      public boolean isValid(Locale locale)
      Description copied from interface: IPolyglott
      Only meaningful for the default language for which all required fields must be filled.
      Specified by:
      isValid in interface IPolyglott
      Parameters:
      locale - the locale to check for a non-empty value
      Returns:
      true if at least one locale has a non empty text set
    • getValue

      public Optional<String> getValue(Locale locale)
      Specified by:
      getValue in interface de.intranda.metadata.multilanguage.IMetadataValue
      Overrides:
      getValue in class de.intranda.metadata.multilanguage.MultiLanguageMetadataValue
      Parameters:
      locale - the locale for which to retrieve the text value
      Returns:
      an optional containing the text for the given locale if one exists, or an empty optional otherwise
    • getLocales

      public Collection<Locale> getLocales()
      Specified by:
      getLocales in interface IPolyglott
      Returns:
      the collection of locales for which a text value may be set
    • hasLocale

      public boolean hasLocale(Locale locale)
      Parameters:
      locale - locale to check for presence in the supported locale list
      Returns:
      true if the given locale is in the list of locales for which a text may be set, whether or not a text exists for that locale
    • toString

      public String toString()
      Alias for getText().
      Overrides:
      toString in class de.intranda.metadata.multilanguage.MultiLanguageMetadataValue
      Returns:
      a string representation of this object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class de.intranda.metadata.multilanguage.MultiLanguageMetadataValue
    • equals

      public boolean equals(Object obj)
      Two TranslatedTexts are considered equal if the have the same locales and the same texts for each locale.
      Overrides:
      equals in class de.intranda.metadata.multilanguage.MultiLanguageMetadataValue
      Parameters:
      obj - the object to compare to this translated text
      Returns:
      true if the given object is equal to this instance, false otherwise
    • isComplete

      public boolean isComplete(Locale locale, Locale defaultLocale, boolean required)
      If required is true, true is returned if a non-empty value exists for the given locale. Otherwise return true unless defaultLocale has a non-empty value while locale has not.
      Parameters:
      locale - locale to check for completeness
      defaultLocale - fallback locale used to determine if a value exists
      required - whether a non-empty value for the given locale is strictly required
      Returns:
      true if completeness not required or text not empty; false otherwise
    • getAsJson

      public String getAsJson() throws com.fasterxml.jackson.core.JsonProcessingException
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException