Comments (8)
Now that's a strange coincidence ...
I'm developing the Multilingual URL Field (see URL Field) and I just found out yesterday (Thursday) that I actually need the values for all languages from Multilingual Field to be output in the XML structure :)
There is one problem tough ...
This is the current output of the field as seen in a DS (for a Title
field, English language as current):
<title mode="formatted" handle="yet-another-entry" word-count="1" handle-en="yet-another-country" handle-ro="inca-o-inregistrare" handle-es="spanish-entry">
Yet another entry
</title>
There are 2 ways to do this:
1_ Output the values for all languages as attributes
, except current language value which is the value of the node:
<title mode="formatted" handle="yet-another-entry" word-count="1" handle-en="yet-another-country" handle-ro="inca-o-inregistrare" handle-es="spanish-entry" value-ro="Inca o inregistrare" value-es="Spanish entry">
Yet another entry
</title>
Thsi approach will be backwards compatible (no XSLT changes required) but the XML output will be REALLY clumsy and won't scale well at all.
2_a_ Rework the output of the code with dedicated XML elements for each language:
<title mode="formatted" handle="yet-another-entry" word-count="3" value="Yet another country">
<item lang="en" handle="yet-another-country" word-count="3">Yet another country</item>
<item lang="ro" handle="inca-o-inregistrare" word-count="3">Inca o inregistrare</item>
<item lang="es" handle="spanish-entry" word-count="2">Spanish entry</item>
</title>
This approach will scale very well, obviously, clean output with detailed information for each language.
I added the values for current-language (English here) as attributes in title for easy access. This is debatable:
Pros: Small code snippet (yeeey, for lazy ones):
<xsl:value-of select="title/@value"/>
Cons: duplicate information. Not a good thing.
2_b_ Remove this info and keep it clean:
<title mode="formatted">
<item lang="en" handle="yet-another-country" word-count="3">Yet another country</item>
<item lang="ro" handle="inca-o-inregistrare" word-count="3">Inca o inregistrare</item>
<item lang="es" handle="spanish-entry" word-count="2">Spanish entry</item>
</title>
Pros: clean structure
Cons: The value of current language must be used to identify the element, but if you're building multilingual sites I bet you have the current language stored in a variable so this won't be a con after all:
<xsl:variable name="lc" select="/data/fl-languages/current-language/@handle"/>
<xsl:value-of select="title/item[ @lang = $lc ]"/>
I suggest approach 2_b_ because it's future friendly. If we do this, we do this right from the first time.
from multilingual_field.
Maybe @vlad-ghita knows a way to add it to all multlingual fields?
This should be done in each field separately.
from multilingual_field.
I thought this to be a new, separate output mode, like fieldname: all-languages
. So in the case of the ML Text Box we'd end up the output modes :formatted
, :unformatted
and :all-languages
. This way we don't have to worry about compatibility with existing templates. So I'm suggesting (and implemented it like that) 2_b_ as well. :-)
I my use case (building a members backend) the :all-languages
mode automatically builds an :unformatted
item for each supported language. The code in appendFormattedElement()
for this (the new) version of the multilingual text box could look like
if ($mode == 'all-languages') {
$all = new XMLElement($this->get('element_name'), NULL, array('mode' => $mode));
foreach (FLang::getLangs() as $lang_code) {
$data['handle'] = $data['handle-'.$lang_code];
$data['value'] = $data['value-'.$lang_code];
$data['value_formatted'] = $data['value_formatted-'.$lang_code];
$data['word_count'] = $data['word_count-'.$lang_code];
$attributes = array(
'language' => $lang_code
);
$item = new XMLElement(
'item', null, $attributes
);
parent::appendFormattedElement($item, $data, true, 'unformatted');
// Reformat generated XML
$elem = $item->getChild(0);
if (!is_null($elem)) {
$attributes = $elem->getAttributes();
$value = $elem->getValue();
$item->setAttributeArray($attributes);
$item->setValue($value);
$item->removeChildAt(0);
}
$all->appendChild($item);
}
$wrapper->appendChild($all);
}
The mode could be added to the DS Editor like
public function fetchIncludableElements() {
$includable_elements = parent::fetchIncludableElements();
$includable_elements[] = $this->get('element_name') . ': all';
return $includable_elements;
}
But maybe we sould support every output mode of the parent field in a :all-languages
variant. Then the above snippet wouldn't work when the output is not text only (e.g. formatted) and has child nodes because of
$value = $elem->getValue();
$item->setValue($value);
from multilingual_field.
all-languages formatted
all-languages unformatted
?
from multilingual_field.
Thumbs up for 2_b implementation and all-languages formatted / all-languages unformatted
from multilingual_field.
I'll take care of it.
Thanks @klaftertief for the implementation example.
from multilingual_field.
Yep. It sounds a bit rough but I think prepending 'all-languages' to the parents modes works. Though the mode selection in the DS editor gets filled even more. Imagine ML fields in a subsection.
from multilingual_field.
I'll take care of it.
Thanks. Appreciated.
from multilingual_field.
Related Issues (20)
- S2.5: Multilingual values of associated entries don't get translated properly anymore HOT 16
- Cross language display in entry lists HOT 2
- Optionnal message when the there is only one required language HOT 1
- Can't see the check "Display in entries table" HOT 2
- Add Note when "use main language" is checked HOT 3
- Problem with required languages field HOT 1
- Backend filtering is broken in 2.6
- Backend sorting is broken in 2.6
- html is encoded <p> -> >p< HOT 1
- XML Importer Fails HOT 4
- Not PHP 7 compatible HOT 1
- Preferences not saved HOT 11
- Handles are unique even if not opted HOT 2
- Fatal error when updating from Symphony 2.6.9 HOT 7
- PHP 7: Symphony Warning: Declaration of fieldMultilingual_TextBox::getCurrentHandle($entry_id, $lc) should be compatible with FieldTextBox::getCurrentHandle($entry_id, $lc = NULL) HOT 5
- Datasource filtering multilingual_field regexp:{$url-keyword} with empty $url-keyword doesn't work anymore
- Required languages are lost when saving a section with error
- Symphony Fatal Database Error: Specified key was too long; max key length is 1000 bytes HOT 6
- Symphony Fatal Database Error: Data too long for column 'handle-en' at row 1 (Output handles checkbox = false) HOT 3
- Still having the 1000bytes error in SYM 2.7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from multilingual_field.