Last active
February 27, 2020 05:54
-
-
Save LeCoupa/23d11de87fe80d7724cd to your computer and use it in GitHub Desktop.
Meteor: Internationalization (i18n) --> https://github.com/LeCoupa/awesome-cheatsheets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This little gist shows how to easily internationalize your application using the meteor-just-i18n package. | |
# Package repository: https://github.com/subhog/meteor-just-i18n/ | |
# 1. Adding the package. | |
$ meteor add anti:i18n |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The following code should be available to the client and the server. (i.e.: inside your lib directory) | |
# 2. Set a default language. | |
i18n.setDefaultLanguage 'en_US' | |
# 3. A little helper to retrieve the locale according to the language. (see step 5. for the use) | |
@getLanguage = (language) -> | |
if language.match /fr/ | |
language = 'fr_FR' | |
else | |
language = 'en_US' | |
return language | |
# 4. Our translated strings. | |
i18n.map 'en_US', homepage: | |
title: 'Meteor is awesome.' | |
subtitle: 'A better way to build apps.' | |
i18n.map 'fr_FR', homepage: | |
title: 'Meteor est génial.' | |
subtitle: 'Une meilleure manière de construire des applications.' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The following code should only be available to the client. (inside your client directory) | |
# 5. Set the relevant language client-side. | |
Meteor.startup -> | |
if Meteor.isClient | |
if Meteor.user() | |
language = Meteor.user().profile.language # see step 6. | |
else | |
# detect the language used by the browser | |
language = window.navigator.userLanguage || window.navigator.language | |
language = getLanguage language | |
i18n.setLanguage language | |
# 6. Let's save the user language during registration by passing the language to Accounts.createUser() | |
Template.AccountSignUp.events | |
'submit #signUpForm': (evt) -> | |
# do your stuff as usual for register your users | |
language = i18n.getLanguage language | |
options = { email: email, password: password, language: language } | |
Accounts.createUser options, (err) -> | |
# handle the errors as usual. | |
# 7. Let's load the relevant user language during sign in. | |
Template.AccountSignIn.events | |
'submit #signInForm': (evt) -> | |
# do your stuff as usual for login your users | |
Meteor.loginWithPassword email, password, (err) -> | |
if not err | |
language = Meteor.user().profile.language | |
i18n.setLanguage language |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The following code should only be available to the server. (inside your server directory) | |
# 8. We save the user language during registration server-side. (see step 6.) | |
Accounts.onCreateUser (options, user) -> | |
# do you stuff as usual | |
language = options.language | |
user.profile = { language: language } | |
return user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
9. Inside your Template, we just have to use the i18n helper. | |
Note 1: if your translated strings include html tags, use the triple brackets {{{ }}}. | |
--> | |
<template name="Homepage"> | |
<h1>{{i18n 'homepage.title'}}</h1> | |
<h2>{{i18n 'homepage.subtitle'}}</h2> | |
</template> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment