mojotech / torch

A rapid admin generator for Elixir & Phoenix
Other
1.12k stars 80 forks source link

Enhance internationalization #143

Closed brunvez closed 4 years ago

brunvez commented 4 years ago

Hey, I came by a use case where I needed to translate my torch interface to Spanish. Doing that in the view I have in my projects is straightforward since I can call the gettext macros on the strings. The issue is that neither the filters nor the pagination allowed me to override the translations.

I came by these two questions in the Elixir forum that give some insight on how to internationalize those strings and cooked my own solution here. It's a bit of a WIP but it worked for me and with some polish maybe we can merge it in if you think it'd be useful.

Forum questions:

cpjolicoeur commented 4 years ago

Hey @brunvez, I think we are all for adding better/proper I18N support into Torch. Either just by adding the es Spanish gettext files directly, or with a "larger" implementation like you have executed.

Is the issue, that Torch currently just doesn't provide a Spanish translation file (we currently only have English and Russian) or that even if we did have a Spanish file, you couldn't get what you needed?

brunvez commented 4 years ago

Thanks for answering, adding the es translation directly would solve things form me. I just wanted to push for a more comprehensive solution since this may arise again in the future.

Though I have to admit I don't really like the current way of solving it with gettext, or at least the one I could find 😅

cpjolicoeur commented 4 years ago

I'd love to add as many translation files as possible, but I'm not a native speaker of all these languages, so we've been adding them to the system as users request them and can provide correct translations.

Using a sort of hybrid approach might not be a bad solution though as it would allow us to add the translation files as the community provides them, while also allowing users to dynamically use their own local translation while not "official" translation file exists.

brunvez commented 4 years ago

Yeah, I think this provides kind of the best of both worlds. It maintains current functionality and allows to extend it to any language. For some context, this is the needed code for developers to config their custom texts:

config :torch,
  otp_app: :my_app,
  messages_backend: MyApp.Torch.MessagesBackend,
  template_format: "eex"
defmodule MyApp.Torch.MessagesBackend do
  import MyAppWeb.Gettext, only: [dgettext: 2]

  def message("Contains"), do: dgettext("torch", "Contains")
  def message("Equals"), do: dgettext("torch", "Equals")
  def message("Choose one"), do: dgettext("torch", "Choose one")
  # ...
end

I'd also be glad to add translations for Spanish of course.

cpjolicoeur commented 4 years ago

Would you be open to submit a Pull Request here with the MessagesBackend features. It could include the Spanish translations as well, or you could submit those as a second pull request if you want.

brunvez commented 4 years ago

Sure thing, I'll make some tweaks and open a PR

cpjolicoeur commented 4 years ago

@brunvez Version 3.1.0 should now be on hex.pm. Thanks again.

brunvez commented 4 years ago

Thank you @cpjolicoeur!