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

Experiment implementing po_extract() #243

Merged
merged 31 commits into from
Nov 9, 2021
Merged

Experiment implementing po_extract() #243

merged 31 commits into from
Nov 9, 2021

Conversation

hadley
Copy link
Collaborator

@hadley hadley commented Nov 6, 2021

Mostly to trigger discussion. More comments below.

@MichaelChirico
Copy link
Owner

po_scan() doesn't feel like a great name to me... maybe po_snapshot()?

# treat gettextf separately since it takes a named argument, and we ignore ...
get_named_arg_strings(expr_data, 'gettextf', c(fmt = 1L), recursive = TRUE),
get_named_arg_strings(expr_data, fmt_funs, c(fmt = 1L), recursive = TRUE),
if (use_tr) get_named_arg_strings(expr_data, 'tr_', c(string = 1L), recursive = TRUE),
Copy link
Owner

Choose a reason for hiding this comment

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

an aside: tr_ might be a good opportunity to build something I'd been kind of stuck on:

Since R doesn't have parser-level string concatenation (i.e. C's 'abc' 'def') making translation-ready strings in R will tend to produce lintr-violating wide lines (esp. at 80 but also at 120 characters)... the tooling for base has no shot here because it considers all the inputs as individual strings.

But with custom functions we could allow:

tr_(
  'abcd',
  'efg'
)

To wind up as abcdefg in the .pot file & also run gettext() on the combined string:

tr_ <- function(...)
  gettext(paste0(...))

I think it would require some effort to get it working but maybe worthwhile to prevent infinite scroll strings...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I like that idea. I'll change it to a dots function.

In total we might want something like:

tr_ <- function(...) {
  structure(enc2utf8(gettext(paste0(...), domain = "R-pkgdown")), class = "translated")
}

That ensures it's correctly flagged as UTF-8 (which gettext()) doesn't do by default, and flags it as a translated string which might be useful for other functions.

Copy link
Collaborator Author

@hadley hadley Nov 7, 2021

Choose a reason for hiding this comment

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

This function could also error if given anything other than a literal string (i.e. tr_(foo) and tr_(foo()) should error because they're not translatable).

Copy link
Owner

Choose a reason for hiding this comment

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

that sounds basically right to me (requiring literal inputs), I do wonder if it will wind up being limiting. I have in mind the N_() construct in C which is needed to keep strings untranslated at compile time, then translated as a variable at run time.

https://github.com/wch/r-source/blob/430360aa394b02046dd7175c0fc55e347e947f80/src/main/errors.c#L1398-L1419

https://github.com/wch/r-source/blob/430360aa394b02046dd7175c0fc55e347e947f80/src/main/errors.c#L1461

Not sure if it's relevant, so probably it's OK to start being strict and allow the use cases to show themselves as a feature request.

@hadley
Copy link
Collaborator Author

hadley commented Nov 7, 2021

Hmmm, it seems like we're cueing off different parts of this function — it both looks for (scans) and records (snapshots) messages for translation. I wonder if we can somehow get both ideas into one word ... How about po_extract()?

@hadley
Copy link
Collaborator Author

hadley commented Nov 7, 2021

Fixes #223. Fixes #226. Fixes #229.

@MichaelChirico
Copy link
Owner

po_extract() sounds better to me -- both make clearer the output of the function... the scanning part feels more "incidental"

@hadley
Copy link
Collaborator Author

hadley commented Nov 8, 2021

Ok, I think this is pretty close to being done apart from the documentation — the problem is that po_extract() and get_message_data()share all of their arguments but there's no way to@inheritParams` from an .Rd file in the same package. I think there are two options:

  1. Convert get_message_data() to use roxygen2
  2. Duplicate the docs

Which would you prefer?

@hadley hadley changed the title Experiment implementing po_scan() Experiment implementing po_extract() Nov 8, 2021
@MichaelChirico
Copy link
Owner

  1. I'm happy to gradually converge on roxygen2 everywhere

@hadley
Copy link
Collaborator Author

hadley commented Nov 8, 2021

@MichaelChirico if you want, I can do a separate PR that converts all the .Rds to roxygen (it's 95% automated). Do you want to use @export too or stick with the explicit NAMESPACE?

@MichaelChirico
Copy link
Owner

That works, esp. if it'll make it easier for you to be productive going forward (and assuming it's not a huge time drain for you).

I would keep an explicit NAMESPACE, I do prefer being more intentional there.

I was reading `style = 'base'` as the call which was throwing me off. an explicit argument here should help readability.
po_params = list(
package = desc[['Package']],
version = desc[['Version']],
copyright = NULL,
Copy link
Owner

Choose a reason for hiding this comment

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

is your sense that these fields (copyright and bugs) aren't really needed?

my sense as well has been these things are basically covered by the DESCRIPTION file already, and kept it to try & be consistent with base (and it's something of a maintenance headache to implement)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I think we should remove them; it's better to leave to the DESCRIPTION and avoid creating duplicates that might get out of date.

@MichaelChirico MichaelChirico merged commit bd9c91a into MichaelChirico:master Nov 9, 2021
@hadley hadley deleted the po_scan branch November 9, 2021 11:49
@hadley
Copy link
Collaborator Author

hadley commented Nov 9, 2021

To be clear, I meant using @export to generate the NAMESPACE, so it's still intentional, but you while you're near the source of the function you can tell if it's exported or not.

@MichaelChirico
Copy link
Owner

right -- I like that but the @import / @importFrom less so.

how about using the NAMESPACE roclet but keeping all the @import/@importFrom tags in a central place (e.g. the @doctype package or .onLoad)?

@hadley
Copy link
Collaborator Author

hadley commented Nov 9, 2021

Sure. Our current convention is to put them with the package docs anyway.

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.

2 participants