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

Use user-preferred language in labels #578

Merged
merged 5 commits into from
Nov 27, 2022
Merged

Use user-preferred language in labels #578

merged 5 commits into from
Nov 27, 2022

Conversation

1ec5
Copy link
Collaborator

@1ec5 1ec5 commented Nov 27, 2022

All map labels now use the name:* field corresponding to the user’s preferred language if available, falling back to the user’s second preferred language and so on. If none of the preferred languages is available, labels fall back to the local language. English is no longer hard-coded.

Spanish and Traditional Chinese, falling back to the local language:

Spanish Chinese

Working towards #20.

@1ec5 1ec5 added enhancement New feature or request internationalization labels Nov 27, 2022
@1ec5 1ec5 self-assigned this Nov 27, 2022
["get", "name:latin"],
["get", "name"],
];
export const localizedName = (function () {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The text-field expressions continue to be evaluated statically as the page loads. To dynamically switch languages after the page loads, we could listen for the languagechange and tear down and rebuild all the layers. This would require refactoring americana.js.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Comment on lines 5 to 6
let userLocales =
"languages" in navigator ? [...navigator.languages] : [navigator.language];
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The user-preferred language could be Arabic or Hebrew, which are both written right to left. As tail work, we should install the mapbox-gl-rtl-text plugin to keep the map readable for users of these languages.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@ZeLonewolf
Copy link
Owner

How does this work? Is it based on a user's web browser settings?

@ZeLonewolf
Copy link
Owner

Awesome...

image

image

Copy link
Owner

@ZeLonewolf ZeLonewolf left a comment

Choose a reason for hiding this comment

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

I'm not going to pretend that I fully understand how this works, but the result is awesome!

src/constants/label.js Outdated Show resolved Hide resolved
@jleedev
Copy link
Collaborator

jleedev commented Nov 27, 2022

I seem to recall that it was once possible for navigator.languages to be empty, but both chrome and firefox appear to default to en-us when I set their respective preference to empty string, so defensive coding there isn't needed.

@1ec5
Copy link
Collaborator Author

1ec5 commented Nov 27, 2022

I seem to recall that it was once possible for navigator.languages to be empty, but both chrome and firefox appear to default to en-us when I set their respective preference to empty string, so defensive coding there isn't needed.

This is actually for Node, which doesn’t support navigator.languages yet.

Co-authored-by: Josh Lee <jleedev@gmail.com>
@jleedev
Copy link
Collaborator

jleedev commented Nov 27, 2022

I seem to recall that it was once possible for navigator.languages to be empty, but both chrome and firefox appear to default to en-us when I set their respective preference to empty string, so defensive coding there isn't needed.

This is actually for Node, which doesn’t support navigator.languages yet.

Node.js doesn't have navigator at all, not sure what you mean here.

@1ec5
Copy link
Collaborator Author

1ec5 commented Nov 27, 2022

Oh, right…

@1ec5
Copy link
Collaborator Author

1ec5 commented Nov 27, 2022

I brought the transportation_label layers under the new localized label system as well. However, due to openmaptiles/openmaptiles#769, roads can only be in English, German, or the local language.

Hoover Dam Access Road Hoover-Talsperre

Oldenburg Oldenburg

@answerquest
Copy link

How do we make this change happen? I'm not seeing any place in the main map https://zelonewolf.github.io/openstreetmap-americana to change language

@1ec5
Copy link
Collaborator Author

1ec5 commented Dec 6, 2022

@1ec5
Copy link
Collaborator Author

1ec5 commented Dec 20, 2022

#622 implemented a control in the lower-right corner to set the map’s language independently of your browser settings, so you no longer need to know the ISO 639 code of the language you want to see.

Find out more about Americana’s label localization and its use cases in this OSMUS blog post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants