elm-fluent / elm-fluent Goto Github PK
View Code? Open in Web Editor NEWFluent for Elm (translation and l10n/i18n solution)
License: MIT License
Fluent for Elm (translation and l10n/i18n solution)
License: MIT License
If you use the --when-missing=fallback
option, the generated code currently works as follows:
The result of this is that the output for that message could be a mixture of multiple locales.
For example, our default language is English, and in the French .ftl
a message is missing. The end user has selected a French locale, and this Locale
value gets passed to the dispatching translation function. The dispatching function calls the English message function (because there is no French function). But it still passes the original French locale value to that function.
So, for that message we will get English text. But if the message uses NUMBER()
(explicitly or implicitly), the Intl.NumberFormat
function gets passed the French locale, and will do French number formatting.
So, the message becomes a mixture of English and French, which is pretty confusing.
The alternative is that each generated module for a locale hard codes a Locale
value into the module, and uses this for number/date formatting, instead of the passed in value (which means that we wouldn't actually need to pass in a Locale
value to these functions.
This, however, is also confusing. We now have a mixture of number/date formatting within the page. Both alternatives have confusion - the current method means you get consistent number/date formatting across the page, although a message might have number/date formatting that is not consistent with the language it uses. The alternative means you get consistency within a message, but not consistent number/date formatting across the page.
There is another use case. If you have British English and American English locales, it could be useful to, say, have American English as the default, and only create British English messages where they are different - reducing the load on translators. In this case, the current behaviour is good - when British English locale is active, but American English messages are used, you still British number/date formatting. Similar things may be true for other locale 'dialects'.
Ideas welcome!
Not really an issue, more of an suggestion, so feel free to close this if you do not like it.
I've recently implemented fluent compatability in my
own Elm i18n package on 0.19 using
the approach of decoding a proxy object at runtime. You can find the repo for the proxy here. The users overhead is pretty minimal in this approach:
What do you think?
The docs need finishing:
ftl2elm
It would be nice to integrate support for Pontoon, which provides a GUI for translation that allows in-place editing.
For this to work best, I think the generated HTML would need to include the message ID - see the L20n docs .
<p data-l10n-id="about"></p>
In our case, we could generate the content of this tag as well, adding the data-l10n-id
attribute just for the sake of Pontoon. I'm guessing this works only for content which is in the body of HTML nodes, rather than in attributes, for example.
(Note, a lot of Mozilla docs about Pontoon assuming you are using their infrastructure, pontoon.mozilla.org, but you can self-host).
Currently only a single fallback language is supported. It would be nice to support things like "en-GB
automatically falls back to en
" etc. Ideas for how this should work are welcome (e.g. command line flags, automatic smart behaviour etc.). See also issue #9.
When writing .ftl
files, it is useful to have ftl2elm
run automatically for various use cases.
This could be done by adding a --watch
parameter to ftl2elm
. Most likely watchdog will be the easiest way to do this.
As a workaround for now, you can us the watchmedo
utility that watchdog
provides:
$ pip install watchdog
$ watchmedo shell-command --recursive --patterns='*.ftl' --command 'ftl2elm --verbose --when-missing=fallback' locales
All generated Elm modules should have a comment saying that they shouldn't be edited, preferable with something saying how the file was generated and can be re-generated.
There is currently poor test coverage for the compile_master
function in elm_fluent/compiler.py
and elm_fluent/run.py
- especially for the error condition cases. We should fix this, it just requires a bit more setup - run.py
is hard especially because its input is filesystem - one of the techniques here might help.
BDI isolation characters are not currently inserted for HTML output. We should insert them, or see if some other HTML construct is more appropriate.
The fluent
dependency is listed as fluent>=0.8.0
. pip install elm-fluent
installs fluent
0.10.0, which breaks elm-fluent
.
Both fluent
0.9.0 and 0.10.0 introduced changes to the AST. Please see the summary of the changes at https://github.com/projectfluent/python-fluent/releases. Notably, the VariantName
node was removed. Variant keys can now be Numbers
or Identifiers
.
(@elm) $ Mozilla/fluent/elm-example -> ftl2elm
Traceback (most recent call last):
File "/c/Users/stas/Mozilla/fluent/@elm/bin/ftl2elm", line 7, in <module>
from elm_fluent.cli import main
File "/c/Users/stas/Mozilla/fluent/@elm/local/lib/python2.7/site-packages/elm_fluent/cli.py", line 13, in <module>
from .run import ErrorWhenMissing, FallbackToDefaultLocaleWhenMissing, run_compile
File "/c/Users/stas/Mozilla/fluent/@elm/local/lib/python2.7/site-packages/elm_fluent/run.py", line 10, in <module>
from .compiler import (
File "/c/Users/stas/Mozilla/fluent/@elm/local/lib/python2.7/site-packages/elm_fluent/compiler.py", line 987, in <module>
@compile_expr.register(ast.VariantName)
AttributeError: 'module' object has no attribute 'VariantName'
(@elm) $ Mozilla/fluent/elm-example ->
pip install fluent==0.8.0
fixed the issue.
e.g. from the docs, these are disallowed:
bad-message-1-html = Some <{ $arg }>text</{ $arg }>
bad-message-2-html = Some <b { $arg }="value">text</b>
But the compiler doesn't disallow them, it produces nonsense HTML instead.
For each generated message function, there should be a comment that identifies:
.ftl
file and lineCurrently elm-fluent compiles each .ftl
file separately. If you use a message reference or term references that refers to something in a different file, it will be a compilation error.
Fixing this requires a few things:
a stronger convention requiring message IDs to be unique across all FTL files. Otherwise we won't be able to know which message is being referred to in the case of duplicates. Alternatively, we only enforce the uniqueness across files if we have a message reference that would be ambiguous otherwise.
a change to ftl2elm so that it can find references across files. We will likely need to do a couple of passes, first to find all the message/terms names then to compile.
a change to the compiler so that for messages it can import the compiled messages from other modules. If FTL files have cyclic dependencies, this won't work however, so perhaps it will be better to inline all messages references that cross modules.
for terms, it is less complex, because these are always inlined so importing is definitely not necessary.
Currently the function signature is:
Locale.Locale -> a -> String
or:
Locale.Locale -> a -> List (String, List (Html.Attribute msg)) -> List (Html.Html msg)
If we put Locale
as the last argument, instead of the first, would it be more useful? By supplying the message arguments (and attributes for the Html
case), but omitting the Locale
, you would then have something that represented an untranslated message, which might crop up quite a bit in actual usage.
Currently the last defined message wins, ftl2elm
should instead treat this as an error
The generated Elm code embeds all unicode characters directly into strings. It would probably be better if some were represented using escape sequences - particularly non-printable ones, such as the BDI control characters. The current compiler output can have things like this:
String.concat [ ""
, args_.user_name
, " napisał:"
]
Here there are BDI chars in the first and last string literal, but that isn't visible at all, and the first string looks like a useless empty string. This could be an issue when debugging.
See projectfluent/fluent#140 (comment)
It would be cool if elm-fluent was able to influence the future of Fluent to make it more friendly to statically typed languages.
Otherwise we get warning from elm-make etc, and it is just cleaner.
This will involve Module
objects searching over their contained expressions to see which imported modules are actually used.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.