Skip to content
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

Allow whitelisting of references #86

Merged
merged 14 commits into from
Apr 5, 2016
Merged

Allow whitelisting of references #86

merged 14 commits into from
Apr 5, 2016

Conversation

maufl
Copy link
Contributor

@maufl maufl commented Apr 4, 2016

Adds an option to specify a pattern of references that should be preserved by Gettext.
As discussed in #85.

## Example

config :gettext,
exlude_refs_from_pruging: "^web\/static\/.*"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in _pruging and in exlude.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, not sure why you're escaping / given you're in a string; it would make sense in ~r//, but in strings you can just use ^web/static/.*. :)

@whatyouhide
Copy link
Contributor

Hey @maufl, great job, this is definitely headed in the right direction. I left a first batch of comments, let me know if everything is clear :)

@maufl
Copy link
Contributor Author

maufl commented Apr 5, 2016

Argh, this damn pruging typo followed me everywhere, I thought I found all of them ...

I made some improvements, I think they cover all your suggestions.

that matches this pattern. You can use this pattern to prevent Gettext from
removing translations that you have extracted using another tool, e.g. from
static resources (web/static).
* `:excluded_refs_from_purging` - a regex that is matched agains translation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agains -> agains

@maufl
Copy link
Contributor Author

maufl commented Apr 5, 2016

I love how all you suggestions make the code smaller and simpler.

@@ -42,7 +42,7 @@ defmodule Gettext.PO.Translations do
## Example

config :gettext,
exclude_refs_from_purging: ~r/^web\/static\/.*/
exclude_refs_from_purging: ~r{^web/static/.*}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe better to make this consistent with the rest of the regexes, ~r{^web/static/}.

@whatyouhide
Copy link
Contributor

@maufl awesome. So, I've been thinking about this and I think we should move the impure logic of this up from where it is now. By impure logic, I mean the retrieving of the :exclude_refs_from_purging option from :gettext's environment. We should move that up at least to Gettext.Extractor.merge_template/2, e.g.:

def merge_template(existing, new) do
  protected_pattern = Application.get_env(:gettext, :exclude_refs_from_purging)
  ...
    PO.Translations.protected?(t, protected_pattern)

This way, protected?/2 becomes simpler to reason about and you only retrieve the pattern from the app config once (instead of once for each translation).

Also, I don't love the idea of having a "fallback" regex that never matches when the :exclude_refs_from_purging option is not provided; personally, I'd like more something along these lines:

protected_pattern = Application.get_env(:gettext, :exclude_refs_from_purging)

# in protected?/2
def protected?(_t, nil), do: false
def protected?(_t, %Regex{} = pattern), do: ...

Wdyt?

@maufl
Copy link
Contributor Author

maufl commented Apr 5, 2016

I agree that it's ugly to fetch the pattern every time the protected?/1 method is called, but I'm not really sure if I like it better in merge_template/2.

Originally I used the fallback regex to avoid conditional expressions in protected?/1. Your proposal avoids conditional logic in in the function anyway. I think it's a better solution than what I did so far.

@whatyouhide
Copy link
Contributor

@maufl why you don't like the idea of fetching the option in merge_template/2? Or better, why do you like it better in protected?/1?

(btw, ideally we would have this as up as possible, e.g., in the gettext.extract Mix task, that later passes this option around so that also merge_template/2 can take this option and become pure, but let's do small steps here)

@maufl
Copy link
Contributor Author

maufl commented Apr 5, 2016

I don't like it better in protected?/1, but I'm also not sure where I would put it :/ I'm lacking Elixir experience to have a good feeling about where it should go.

@whatyouhide
Copy link
Contributor

@maufl let's put it in merge_template/2 and pass it along to protected?/2 for now, we can always revise later.

We're almost good to go :D We're only missing docs for this new option. You can describe this option in this section of the docs.

@maufl
Copy link
Contributor Author

maufl commented Apr 5, 2016

I already added docs for this option. Should be the first changeset when you compare the files. :)

@whatyouhide
Copy link
Contributor

Ooops, very sorry :) I even left a few comments there 😄 So, very very last thing: can you squash all the commits in a single one? Then I think we're ready to go 👍

@maufl
Copy link
Contributor Author

maufl commented Apr 5, 2016

You mean, squash them locally and force push? I just discovered in another repo that GitHub allows you to squash commits of a pull request before merging them. If you hit "Merge pull request" you will get a "Confirm merge" button with a dropdown. The squash options is hidden in the dropdown.

iex> protected_pattern = ~r{^web/static/}
iex> t = %Gettext.PO.Translation{msgid: "Hello world!", references: [{"web/static/js/app.js", 42}]}
iex> Gettext.PO.Translations.protected?(t, protected_pattern)
true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we leave an empty line after the true? It's very common to do that in Elixir.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course we can :)

def protected?(_t, nil), do: false
def protected?(%{__struct__: s, references: []}, pattern) when is_translation(s), do: false
def protected?(%{__struct__: s, references: refs}, pattern) when is_translation(s),
do: Enum.any?(refs, fn({path, _}) -> Regex.match?(pattern, path) end)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great with do: instead of do ... end :) Last nitpick: can we always have the do: on a new line?

  def protected?(_t, nil),
    do: false
  def protected?(%{__struct__: s, references: []}, pattern) when is_translation(s),
    do: false
  def protected?(%{__struct__: s, references: refs}, pattern) when is_translation(s),
    do: Enum.any?(refs, fn({path, _}) -> Regex.match?(pattern, path) end)

@whatyouhide
Copy link
Contributor

oh, you're right, force of habit :) Left the superlast couple of comments, then we're ready to merge :)

@maufl
Copy link
Contributor Author

maufl commented Apr 5, 2016

Fixed :) Somehow Emacs Elixir mode has issues with this kind of indentation :/

@whatyouhide
Copy link
Contributor

Wonderful 💟 Thanks so much!

(btw yep, this is hard code to indent I think, that's why elixir-mode has trouble with it)

@whatyouhide whatyouhide merged commit 784d97e into elixir-gettext:master Apr 5, 2016
@maufl
Copy link
Contributor Author

maufl commented Apr 5, 2016

Your welcome :) Feels great already to contribute to an Elixir library after only about 2 months.

@maufl maufl deleted the allow-whitelisting branch April 5, 2016 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants