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

Add support for multiple languages #73

Merged
merged 3 commits into from Aug 29, 2023

Conversation

m-horky
Copy link
Contributor

@m-horky m-horky commented Apr 17, 2023

Is this a fix, improvement or something else?

This patch adds support for fallback translations. pt_BR:pt:es makes it possible to use Brazilian Portuguese, fallback to default Portuguese and to Spanish when the translation is not available.

What does this change implement/fix?

  • This patch changes the config struct, and internal implementation of functions that work with it.
  • It changes Get*() functions to iterate over loaded languages, instead of using the only one.
  • It changes IsTranslated*() functions to include new required argument of the language to be checked.
  • Adds Locale.GetActualLanguage(), required by the IsTranslated*() functions. This one may need a bit of polishing.
    • Is there a better name?
    • Is there a better place?
    • Should it be able to handle empty argument as a wildcard, making it possible to return true if some translation exists?

I have ...

  • answered the 2 questions above,
  • discussed this change in an issue,
  • included tests to cover this changes.

Copy link
Owner

@leonelquinteros leonelquinteros left a comment

Choose a reason for hiding this comment

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

Quick comments now, will review in deep later.

gotext.go Outdated Show resolved Hide resolved
gotext_test.go Show resolved Hide resolved
gettext supports supplying multiple languages using the colon character
$ LC_ALL=pt_BR:pt_PT:en_US foo bar
which are used as fallback: when a translation for the first one is not
available, the second language is used. If no language contains
a translation for the string `msgid` is returned.

This patch adds this support into gotext.

1/ config struct
 - 'language' was renamed to 'languages' and is a slice of strings
 - 'storage' was renamed to 'locales' and is a slice of Locale pointers

2/ loadStorage()
 - all loaded languages are iterated over

3/ GetLanguages()
 - new function returns the languages from the config
 - GetLanguage() uses the first element of it, keeping the compatibility

4/ SetLanguage(), Configure()
 - the language string is split at colon and iterated over

5/ Get*()
 - languages are iterated and the first translation for given string is
   returned

6/ IsTranslated*()
 - new optional parameter (langs) has been added

7/ Locale.GetActualLanguage()
 - it checks the filesystem and determines what the actual language code
   is: for 'cs_CZ', just 'cs' may be returned, depending on the actual
   name of the .mo/.po file.
Specifying C disables all further localization.
@m-horky m-horky marked this pull request as ready for review July 10, 2023 13:20
@m-horky
Copy link
Contributor Author

m-horky commented Jul 10, 2023

Hi. I'm still alive and I can continue supporting this PR :). It would be great to get more notes once you have time for that.

@leonelquinteros leonelquinteros merged commit dfd3da1 into leonelquinteros:master Aug 29, 2023
1 check passed
@leonelquinteros
Copy link
Owner

Sorry for the delay on this one @m-horky , it's merged now!

@leonelquinteros
Copy link
Owner

Hey @m-horky , sorry for the turnaround.
It looks your PR doesn't build with current code state, so I had to revert it.

Could you create a new one addressing the issues?

@m-horky m-horky mentioned this pull request Aug 30, 2023
3 tasks
nono added a commit to cozy/cozy-stack that referenced this pull request Apr 15, 2024
…4376)

[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
|
[github.com/leonelquinteros/gotext](https://togithub.com/leonelquinteros/gotext)
| `v1.5.2` -> `v1.6.0` |
[![age](https://developer.mend.io/api/mc/badges/age/go/github.com%2fleonelquinteros%2fgotext/v1.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/go/github.com%2fleonelquinteros%2fgotext/v1.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/go/github.com%2fleonelquinteros%2fgotext/v1.5.2/v1.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/go/github.com%2fleonelquinteros%2fgotext/v1.5.2/v1.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>leonelquinteros/gotext
(github.com/leonelquinteros/gotext)</summary>

###
[`v1.6.0`](https://togithub.com/leonelquinteros/gotext/releases/tag/v1.6.0)

[Compare
Source](https://togithub.com/leonelquinteros/gotext/compare/v1.5.2...v1.6.0)

#### What's Changed

- Allow locale to use a `fs.FS` by
[@&#8203;kobergj](https://togithub.com/kobergj) in
[leonelquinteros/gotext#68
- Runtime translation detection by
[@&#8203;m-horky](https://togithub.com/m-horky) in
[leonelquinteros/gotext#69
- feat: Add language getter for Locale by
[@&#8203;taozhou-glean](https://togithub.com/taozhou-glean) in
[leonelquinteros/gotext#77
- Allow setting global storage by
[@&#8203;didrocks](https://togithub.com/didrocks) in
[leonelquinteros/gotext#80
- Fix multiline export by
[@&#8203;didrocks](https://togithub.com/didrocks) in
[leonelquinteros/gotext#82
- Order locations in ascii order by
[@&#8203;didrocks](https://togithub.com/didrocks) in
[leonelquinteros/gotext#83
- Fix io/fs File leak by
[@&#8203;diamondburned](https://togithub.com/diamondburned) in
[leonelquinteros/gotext#79
- Add support for multiple languages by
[@&#8203;m-horky](https://togithub.com/m-horky) in
[leonelquinteros/gotext#73
- Revert "Add support for multiple languages" by
[@&#8203;leonelquinteros](https://togithub.com/leonelquinteros) in
[leonelquinteros/gotext#84
- Remove unused dependency by
[@&#8203;der-lyse](https://togithub.com/der-lyse) in
[leonelquinteros/gotext#86
- Fix typo in documentation by
[@&#8203;der-lyse](https://togithub.com/der-lyse) in
[leonelquinteros/gotext#87
- Add support for multiple languages by
[@&#8203;m-horky](https://togithub.com/m-horky) in
[leonelquinteros/gotext#85

#### New Contributors

- [@&#8203;kobergj](https://togithub.com/kobergj) made their first
contribution in
[leonelquinteros/gotext#68
- [@&#8203;m-horky](https://togithub.com/m-horky) made their first
contribution in
[leonelquinteros/gotext#69
- [@&#8203;taozhou-glean](https://togithub.com/taozhou-glean) made their
first contribution in
[leonelquinteros/gotext#77
- [@&#8203;didrocks](https://togithub.com/didrocks) made their first
contribution in
[leonelquinteros/gotext#80
- [@&#8203;diamondburned](https://togithub.com/diamondburned) made their
first contribution in
[leonelquinteros/gotext#79
- [@&#8203;der-lyse](https://togithub.com/der-lyse) made their first
contribution in
[leonelquinteros/gotext#86

**Full Changelog**:
leonelquinteros/gotext@v1.5.2...v1.6.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "before 6am on Monday" in timezone
Europe/Paris, Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/cozy/cozy-stack).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4yOTMuMCIsInVwZGF0ZWRJblZlciI6IjM3LjI5My4wIiwidGFyZ2V0QnJhbmNoIjoibWFzdGVyIiwibGFiZWxzIjpbXX0=-->
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