New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
text collector task: automatically generated items for db fields #3584
Comments
Just for clarification - at the moment, it looks like |
Hi Ingo, the prefixing saves us work so we don't need to define field label translations by hand. DataObject's method In the end it 's all about defining a standard way for translating field labels, either way it's work and communication about the best practice. I'd go for the prefixed version, as it's already implemented (though undocumented) and easy to use. And prefix saves us headache if we use e.g. the Title key for anything else. ATM the prefixed translations are overwritten by Of course we'd also have to modify However, a "magic" way or at least less redundant work for translating field labels would be a great feature and would clean up a lot of code. And defining the field labes manually is just a boring and useless work ;) So my proposal:
|
All translations are overwritten by this, not only prefixed ones.
So you mean continue to allow defining these labels without prefix?
I think it's going to be quite confusing if the keys are different between class MyObject extends DataObject {
private static $field_labels = array('ZIP' => 'ZIP Code');
function someMethod() {
echo $this->fieldLabel('ZIP'); // doesn't work with your proposal, has to be 'db_ZIP'?
}
} Does your proposal address the fact I've outlined around duplication in the YAML files for backwards compat? I don't think any solution which requires us to duplicate hundreds of keys across dozens of modules will be feasible.
API breaking change which in my opinion unnecessarily complicates things. I've written a section for ### Field Labels
`DataObject` subclasses can have autogenerated interfaces such as search and edit forms in [ModelAdmin](/reference/modeladmin).
In order to control the field labels displayed there, you can create a mapping in a `$field_labels` static.
Fields from `$db` are mapped directly, for relation fields (`$has_one`, `$has_many`, `$many_many`) you'll need a prefix.
:::php
<?php
class MyObject extends DataObject {
private static $db = array(
'ProductCode' => 'Varchar'
);
private static $many_many = array(
'Tags' => 'Tag'
);
private static $field_labels = array(
'ProductCode' => 'Unique SKU',
'many_many_Tags' => 'Keywords'
);
}
For more advanced cases, you can also overwrite the `fieldLabels()` method and determine the mapping programmatically.
The `$field_labels` mapping can also help to keep your database schema labels in English (good development practice),
while showing labels in the language your users speak. SilverStripe will export all labels into its language files
for you to translate into multiple languages (see [i18n](/topics/i18n)). This means instead of writing
out a label string, you can use the `fieldLabel()` shortcut.
:::php
class MyObject extends DataObject {
// ...
public function getCMSFields() {
$fields = parent::getCMSFields();
// same result as _t('MyDataObject.ProductCode')
$fields->push(TextField::create('ProductCode', $this->fieldLabel('ProductCode')));
return $fields;
}
} |
really? I just tried (in 3.1.6) but the content of $field_labels array isn't grabbed by text collector task. Only $singular_name and $plural_name. Though i don't get from current code where the translation for unprefixed db fields is grabbed... if not manually in each fieldLables() by defining a ton of _t() statements i have to code manually.
IMHO it's more a bugfix than an api breaking change... ATM we grab the translation and overwrite it with the english original string. This way
and the key already used in fieldLabels().
In your documentation example some fields (relations) are prefixed, db fields not... What are you referencing to? FormScaffolder? If we use prefix for everything exept $db then we need to grab translation without prefix. However, i don't know what to do with the double strings. Many of them are surely caused because translation strings are created in different ways manually (Wildwuchs)... not by a defined schema everyone is supposed to use. All i've seen is the code in |
You're right, I've checked
Its breaking existing implementations where both OK, I think we're getting to the root of the problem, thanks for your patience. On adding those prefixed fields to |
This was implemented somewhat recently |
😍😍 Cant wait to test it in real life! Thanks a lot! |
As discussed with @sminnee at #SilverstripeEU:
There is already an automagic method for translating db fields in https://github.com/silverstripe/silverstripe-framework/blob/3.1/model/DataObject.php#L3292
E.g. if i define a translation for MyDataObject.db_Title it gets grabbed and applied there.
But noone knows about it. Many modules use the fieldLabels() method to define translation keys which is just a double work.
Why not modify DataObject::provideI18nEntities() to provide all db_, has_one key also?
I'd be happy to provide a PR.
Take also #3581 in account if it gets merged
The text was updated successfully, but these errors were encountered: