Hi all,
I'm trying to figure out how to solve in L20n a certain pattern in Gaia that
currently requires pretty weird back-and-forth between JS and l10n code.
That pattern is units. Common code looks like this:
--------------------
# File sizes
byteUnit-B = B
byteUnit-KB = KB
byteUnit-MB = MB
byteUnit-GB = GB
byteUnit-TB = TB
byteUnit-PB = PB
byteUnit-EB = EB
byteUnit-ZB = ZB
byteUnit-YB = YB
fileSize = {{size}} {{unit}}
--------------------
And that requiers JS code like this:
document.l10n.formatValue('byteUnit-' + unit).then(unitString => {
document.l10n.setAttributes(element, 'fileSize', {
unit: unitString
});
});
---------------
Can we do this better in L20n? We can, but I'm not sure what pattern should we
use and I think it's related to some things that Stas was wondering about some
time ago.
1) One approach might be:
<byteUnit {
B: "B",
KB: "KB",
MB: "MB"
}>
<fileSize "{{$size}} {{byteUnit[$unit]}}">
-------------------
The problem with this approach is that it abuses hash values. Those are not
*variants* of the same string, those are different strings. It would be pretty
weird if a localizer decided to translate it as <bytUnit "B">, right?
Additionally, it's the only case scenario I can come up with that would
validate the concept of a non-resolvable value. A Hash value without an index
or default value.
Should we allow for that?
2) Another approach might be something like this:
<byteUnit
B: "B",
KB: "KB",
MB: "MB">
<fileSize "{{$size}} {{byteUnit::[$unit]}}">
But then again. Are those attributes of an entity?
3) Another option would be to keep each unit as a separate entity:
<byteUnit_b "B">
<byteUnit_kb "KB">
<byteUnit_mb "MB">
but then... how do I construct a placeable to reference unit that is a
concatenated string ("byteUnit_" + $unit)?
I guess there may be more than those three approaches. Other ideas?
In the future, it would be awesome to have a macro/global that gives you the
right unit size and integer for a given byte integer. Something where you pass
"1025" and it returns "1" and "kb", but I don't think we'll get to macros
anytime soon.
What do you think?
zb.
_______________________________________________
tools-l10n mailing list
[email protected]
https://lists.mozilla.org/listinfo/tools-l10n