Skip to content

Commit

Permalink
Merge pull request #599 from scireum/ili/SIRI-866
Browse files Browse the repository at this point in the history
Adds a Property.getLabelKey method
  • Loading branch information
idlira authored Oct 26, 2023
2 parents 3ba523d + 50e8e6d commit c118af4
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions src/main/java/sirius/db/mixing/Property.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,12 @@ public Object getTarget(Object entity) {
}

/**
* Returns the name of the property which is shown to the user.
* Returns the name of the key used to determine the label of the property.
* <p>
* The label can be set in three ways:
* <ol>
* <li>
* Define an local i18n value for <tt>propertyKey</tt>, which is normally <tt>[entityClass].[compositeName]_[field]</tt>.
* Define a local i18n value for <tt>localPropertyKey</tt>, which is normally <tt>[entityClass].[compositeName]_[field]</tt>.
* So for <tt>com.acme.model.Address.street</tt> in <tt>com.acme.model.Customer</tt> this would be
* <tt>Customer.address_street</tt>. This can be used to give the same composite field different
* names of a per-entity basis.
Expand All @@ -323,23 +323,40 @@ public Object getTarget(Object entity) {
* <li>
* Define an i18n value for <tt>alternativePropertyKey</tt>, which is normally <tt>Model.[field]</tt>.
* So for <tt>com.acme.model.Address.street</tt> this would be <tt>Model.street</tt>. That
* way common names across different entities can share the same translation.
* way, common names across different entities can share the same translation.
* </li>
* </ol>
*
* @return the effective label of the property
* @see #getFullLabel()
* @return the key used to determine the label of the property
*/
public String getLabel() {
public String getLabelKey() {
String currentLanguage = NLS.getCurrentLanguage();
String localLabel = NLS.getIfExists(localPropertyKey, currentLanguage).orElse(null);
if (Strings.isFilled(localLabel)) {
return localLabel;
if (NLS.exists(localPropertyKey, currentLanguage)) {
return localPropertyKey;
}

return NLS.getIfExists(propertyKey, currentLanguage)
.orElseGet(() -> NLS.getIfExists(alternativePropertyKey, currentLanguage)
.orElseGet(() -> NLS.get(propertyKey)));
if (NLS.exists(propertyKey, currentLanguage)) {
return propertyKey;
}

if (NLS.exists(alternativePropertyKey, currentLanguage)) {
return alternativePropertyKey;
}

return propertyKey;
}

/**
* Returns the name of the property which is shown to the user.
* <p>
* This method relies on {@link #getLabelKey()}. See the documentation there for how the label is determined.
*
* @return the effective label of the property
* @see #getLabelKey()
* @see #getFullLabel()
*/
public String getLabel() {
return NLS.get(getLabelKey());
}

/**
Expand Down

0 comments on commit c118af4

Please sign in to comment.