Git Product home page Git Product logo

ember-best-language's Introduction

⚠️ This project is deprecated and no longer maintained by Mirego. It’s only available as a read-only repository.


A FastBoot-enabled addon to detect the best language for your user.

Installation

$ ember install ember-best-language

How does it work?

ember-best-language uses a scoring system to determine the best language to use. The scoring system is based on the Accept-Language header on the FastBoot-side. On the client side, we use navigator.languages and give a score to each language based on its order in the array.

ember-best-language also split language code from country code to make sure that if the user reads fr-CA and your system supports fr, you will have a match.

This addon is inspired by the work of Rémi Prévost in plug_best, you should check it out!

Usage

ember-best-language provides a service with two methods:

  • bestLanguage
  • bestLanguageOrFirst

To find out which language is the best one to use among a list of supported languages:

import Route from '@ember/routing/route';
import {inject as service} from '@ember/service';

export default class extends Route {
  @service('best-language') bestLanguage;

  beforeModel() {
    const bestLanguage = this.bestLanguage.bestLanguage(['en-US', 'fr']);
    // => {language: 'en-US', baseLanguage: 'en', score: 1}
  }
});

If none of the user’s languages are supported, ember-best-language will return null. However, you can use the bestLanguageOrFirst method to make it return the first supported language in those cases.

import Route from '@ember/routing/route';
import {inject as service} from '@ember/service';

export default class extends Route {
  @service('best-language') bestLanguage;

  beforeModel() {
    const bestLanguage = this.bestLanguage.bestLanguage(['fr', 'de', 'en-US']);
    // => null

    const bestLanguageOrFirst = this.bestLanguage.bestLanguageOrFirst(['fr', 'de', 'en-US']);
    // => {language: 'fr', baseLanguage: 'fr', score: 0}
  }
});

Contributing

$ git clone https://github.com/mirego/ember-best-language
$ cd ember-best-language
$ yarn install

Running tests

$ yarn test # Runs `ember try:each` to test the addon against multiple Ember versions
$ ember test
$ ember test --server

Building

$ ember build

For more information on using ember-cli, visit https://ember-cli.com.

License

ember-best-language is © 2017-2022 Mirego and may be freely distributed under the New BSD license. See the LICENSE.md file.

The flag logo is based on this lovely icon by Prasanta Kr Dutta, from The Noun Project. Used under a Creative Commons BY 3.0 license.

About Mirego

Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We're a team of talented people who imagine and build beautiful Web and mobile applications. We come together to share ideas and change the world.

We also love open-source software and we try to give back to the community as much as we can.

ember-best-language's People

Contributors

bracke avatar charlesdemers avatar dependabot[bot] avatar ember-tomster avatar pboutin avatar pkvince avatar remi avatar scottkidder avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ember-best-language's Issues

TypeScript 'declare' fields must first be transformed by @babel/plugin-transform-typescript.

Hi! I have error when build application.
Reproducing in clean project Ember 3.28 or 3.24

ERROR Summary:

  • broccoliBuilderErrorStack: SyntaxError: /home/mabab/my-project/ttt/ember-best-language/services/best-language.ts: TypeScript 'declare' fields must first be transformed by @babel/plugin-transform-typescript.
    If you have already enabled that plugin (or '@babel/preset-typescript'), make sure that it runs before any plugin related to additional class features:
  • @babel/plugin-proposal-class-properties
  • @babel/plugin-proposal-private-methods
  • @babel/plugin-proposal-decorators
    2 | import Service from '@ember/service';
    3 |

4 | declare class FastBoot {
| ^
5 | isFastBoot: boolean;
6 | request: Request;
7 | }
at File.buildCodeFrameError (/home/mabab/my-project/ttt/node_modules/@babel/core/lib/transformation/file/file.js:249:12)
at NodePath.buildCodeFrameError (/home/mabab/my-project/ttt/node_modules/@babel/traverse/lib/path/index.js:143:21)
at assertFieldTransformed (/home/mabab/my-project/ttt/node_modules/@babel/helper-create-class-features-plugin/lib/typescript.js:10:16)
at PluginPass.Class (/home/mabab/my-project/ttt/node_modules/@babel/helper-create-class-features-plugin/lib/index.js:93:79)
at newFn (/home/mabab/my-project/ttt/node_modules/@babel/traverse/lib/visitors.js:177:21)
at NodePath._call (/home/mabab/my-project/ttt/node_modules/@babel/traverse/lib/path/context.js:53:20)
at NodePath.call (/home/mabab/my-project/ttt/node_modules/@babel/traverse/lib/path/context.js:40:17)
at NodePath.visit (/home/mabab/my-project/ttt/node_modules/@babel/traverse/lib/path/context.js:100:31)
at TraversalContext.visitQueue (/home/mabab/my-project/ttt/node_modules/@babel/traverse/lib/context.js:103:16)
at TraversalContext.visitMultiple (/home/mabab/my-project/ttt/node_modules/@babel/traverse/lib/context.js:72:17)

  • code: [undefined]
  • codeFrame: /home/mabab/my-project/ttt/ember-best-language/services/best-language.ts: TypeScript 'declare' fields must first be transformed by @babel/plugin-transform-typescript.
    If you have already enabled that plugin (or '@babel/preset-typescript'), make sure that it runs before any plugin related to additional class features:
  • @babel/plugin-proposal-class-properties
  • @babel/plugin-proposal-private-methods
  • @babel/plugin-proposal-decorators
    2 | import Service from '@ember/service';
    3 |

4 | declare class FastBoot {
| ^
5 | isFastBoot: boolean;
6 | request: Request;
7 | }

  • errorMessage: ember-best-language/services/best-language.ts: /home/mabab/my-project/ttt/ember-best-language/services/best-language.ts: TypeScript 'declare' fields must first be transformed by @babel/plugin-transform-typescript.
    If you have already enabled that plugin (or '@babel/preset-typescript'), make sure that it runs before any plugin related to additional class features:
  • @babel/plugin-proposal-class-properties
  • @babel/plugin-proposal-private-methods
  • @babel/plugin-proposal-decorators
    2 | import Service from '@ember/service';
    3 |

4 | declare class FastBoot {
| ^
5 | isFastBoot: boolean;
6 | request: Request;
7 | }
in /tmp/broccoli-63474gJVA9pwuPPt9/out-070-funnel_funnel_ember_best_language_addon
at broccoli-persistent-filter:Babel > [Babel: ember-best-language] (Babel: ember-best-language)

  • errorType: Build Error
  • location:
    • column: [undefined]
    • file: ember-best-language/services/best-language.ts
    • line: [undefined]
    • treeDir: /tmp/broccoli-63474gJVA9pwuPPt9/out-070-funnel_funnel_ember_best_language_addon
  • message: ember-best-language/services/best-language.ts: /home/mabab/my-project/ttt/ember-best-language/services/best-language.ts: TypeScript 'declare' fields must first be transformed by @babel/plugin-transform-typescript.
    If you have already enabled that plugin (or '@babel/preset-typescript'), make sure that it runs before any plugin related to additional class features:
  • @babel/plugin-proposal-class-properties
  • @babel/plugin-proposal-private-methods
  • @babel/plugin-proposal-decorators
    2 | import Service from '@ember/service';
    3 |

4 | declare class FastBoot {
| ^
5 | isFastBoot: boolean;
6 | request: Request;
7 | }
in /tmp/broccoli-63474gJVA9pwuPPt9/out-070-funnel_funnel_ember_best_language_addon
at broccoli-persistent-filter:Babel > [Babel: ember-best-language] (Babel: ember-best-language)

  • name: Error
  • nodeAnnotation: Babel: ember-best-language
  • nodeName: broccoli-persistent-filter:Babel > [Babel: ember-best-language]
  • originalErrorMessage: /home/mabab/my-project/ttt/ember-best-language/services/best-language.ts: TypeScript 'declare' fields must first be transformed by @babel/plugin-transform-typescript.
    If you have already enabled that plugin (or '@babel/preset-typescript'), make sure that it runs before any plugin related to additional class features:
  • @babel/plugin-proposal-class-properties
  • @babel/plugin-proposal-private-methods
  • @babel/plugin-proposal-decorators
    2 | import Service from '@ember/service';
    3 |

4 | declare class FastBoot {
| ^
5 | isFastBoot: boolean;
6 | request: Request;
7 | }

  • stack: SyntaxError: /home/mabab/my-project/ttt/ember-best-language/services/best-language.ts: TypeScript 'declare' fields must first be transformed by @babel/plugin-transform-typescript.
    If you have already enabled that plugin (or '@babel/preset-typescript'), make sure that it runs before any plugin related to additional class features:
  • @babel/plugin-proposal-class-properties
  • @babel/plugin-proposal-private-methods
  • @babel/plugin-proposal-decorators
    2 | import Service from '@ember/service';
    3 |

4 | declare class FastBoot {
| ^
5 | isFastBoot: boolean;
6 | request: Request;
7 | }
at File.buildCodeFrameError (/home/mabab/my-project/ttt/node_modules/@babel/core/lib/transformation/file/file.js:249:12)
at NodePath.buildCodeFrameError (/home/mabab/my-project/ttt/node_modules/@babel/traverse/lib/path/index.js:143:21)
at assertFieldTransformed (/home/mabab/my-project/ttt/node_modules/@babel/helper-create-class-features-plugin/lib/typescript.js:10:16)
at PluginPass.Class (/home/mabab/my-project/ttt/node_modules/@babel/helper-create-class-features-plugin/lib/index.js:93:79)
at newFn (/home/mabab/my-project/ttt/node_modules/@babel/traverse/lib/visitors.js:177:21)
at NodePath._call (/home/mabab/my-project/ttt/node_modules/@babel/traverse/lib/path/context.js:53:20)
at NodePath.call (/home/mabab/my-project/ttt/node_modules/@babel/traverse/lib/path/context.js:40:17)
at NodePath.visit (/home/mabab/my-project/ttt/node_modules/@babel/traverse/lib/path/context.js:100:31)
at TraversalContext.visitQueue (/home/mabab/my-project/ttt/node_modules/@babel/traverse/lib/context.js:103:16)
at TraversalContext.visitMultiple (/home/mabab/my-project/ttt/node_modules/@babel/traverse/lib/context.js:72:17)

=================================================================================

Loosen required versions dependencies

Is there any opposition to un-'pinning' the versions of both ember-cli-babel and ember-cli-typescript? I think adding a ^ is fairly safe given the stability and ubiquity of these two addons. It prevents people from having to do their own resolutions configuration to stay on the latest versions of these dependencies.

Stopped working in IE 11

Version 0.1.0 worked fined. With recent version 1.0.3 I get the following:

TypeError: Invalid attempt to spread non-iterable instance

This stops the application from running completely. This only happens in IE11, it works fine in Chrome.

Code causing this is in beforeModel of application route:

config.i18n.allowedLocales = ['de', 'en', 'fr'];
const bestLanguage = get(this, 'bestLanguage').bestLanguageOrFirst(config.i18n.allowedLocales);
const languageToSet = get(bestLanguage, 'baseLanguage');

ember 3 compatibility

Hi, I just upgrade my app from 2.18.4 to 3.0.4 and are getting error on this addon;

From the console;

console-consumer.js:11 window Error: Assertion Failed: You attempted to access `fastboot.isFastBoot` (on `<Ember.Service:ember28>`), but `fastboot` is a computed property.:Assertion Failed: You attempted to access `fastboot.isFastBoot` (on `<Ember.Service:ember28>`), but `fastboot` is a computed property.

Due to certain internal implementation details of Ember, the `fastboot` property previously contained a private "descriptor" object, therefore `fastboot.isFastBoot` would have been `undefined`.

This implementation detail has now changed and the "descriptor" object is no longer present at this location. Soon, accessing `fastboot` on this object will return the computed property's current value (see RFC #281 for more details).

If you are seeing this error, you are likely using an addon that relies on this now-defunct private implementation detail. If you can, identify the addon from the stack trace below and report this bug to the addon authors. If you feel stuck, the Ember Community Slack (https://ember-community-slackin.herokuapp.com/) may be able to offer some help.

If you are an addon author and need help transitioning your code, please get in touch in the #dev-ember channel in the Ember Community Slack.

Due to certain internal implementation details of Ember, the `fastboot` property previously contained a private "descriptor" object, therefore `fastboot.isFastBoot` would have been `undefined`.

This implementation detail has now changed and the "descriptor" object is no longer present at this location. Soon, accessing `fastboot` on this object will return the computed property's current value (see RFC #281 for more details).

If you are seeing this error, you are likely using an addon that relies on this now-defunct private implementation detail. If you can, identify the addon from the stack trace below and report this bug to the addon authors. If you feel stuck, the Ember Community Slack (https://ember-community-slackin.herokuapp.com/) may be able to offer some help.

If you are an addon author and need help transitioning your code, please get in touch in the #dev-ember channel in the Ember Community Slack.
    at new EmberError (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:28721:25)
    at Object.assert (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:28964:15)
    at Object.get (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:38404:42)
    at BestLanguage.bestLanguage (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:101861:60)
    at BestLanguage.bestLanguageOrFirst (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:101868:33)
    at Class.<anonymous> (https://localhost:4201/assets/plhw-client-468fbff1eedeb089d410147a0a1aceb0.js:23166:39)
    at ComputedPropertyPrototype.get (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:40588:28)
    at get (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:39959:27)
    at Class.get (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:52872:34)
    at Object.initialize (https://localhost:4201/assets/plhw-client-468fbff1eedeb089d410147a0a1aceb0.js:7028:74)
    at https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:27719:21
    at Vertices.each (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:26386:17)
    at Vertices.walk (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:26315:18)
    at DAG.each (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:26260:28)
    at DAG.topsort (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:26266:18)
    at Class._runInitializer (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:27733:13)
    at Class.runInstanceInitializers (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:27716:12)
    at Class._bootSync (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:26563:24)
    at Class.didBecomeReady (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:27318:20)
    at invoke (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:24589:24)
    at Queue.flush (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:24509:25)
    at DeferredActionQueues.flush (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:24662:31)
    at Backburner.end (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:24794:42)
    at Backburner._run (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:25144:26)
    at Backburner._join (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:25120:29)
    at Backburner.join (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:24861:25)
    at Function.run.join (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:41564:30)
    at HTMLDocument.<anonymous> (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:41639:23)
    at mightThrow (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:8469:29)
    at process (https://localhost:4201/assets/vendor-68effba927d4ce1bd6edb6a52068e536.js:8537:12)

Not sure what I can do about this. We are not using fast boot.

Blank page with using ember-best-language on Ember 3.12.0

After updating the application from Ember 3.10.1 to 3.12.0, it began to give a blank page. After a long search, I found the reason. With ember-best-language, the application gives a blank page while there are no errors in the console.
The extension in the Ember Inspector browser also stops detecting the application.

node: 10.16.3
npm: 6.11.2
ember-cli: 3.12.0
ember-cli-fastboot: 2.1.3
ember-best-language: 1.0.6 or 1.0.7

Thanks!

any reason for being that specific about the node version?

If I understand correctly, the version 1.0.0 of this add on requires node version 10.14.1

"node": "10.14.1",

When using a different version, this leads to the following error when trying to install/upgrade the addon:

error [email protected]: The engine "node" is incompatible with this module. Expected version "10.14.1". Got "11.3.0"

Previous versions of this add-on were more relaxed regarding this requirement. Is there a reason for the change?

If there isn't any specific reason, using what ember itself is using, probably might make sense to improve compatibility:
"engines": { "node": "6.* || 8.* || >= 10.*" },

IE 11 Error

An error in IE happens...

return Array.from(new Set(languages))

new Set() works, but new Set(mySet) is not supported by IE11.

const set = new Set();
languages.forEach(lang => set.add(lang));
languages = Array.from(languages);

but if the uniqueness is the purpose this might be nicer

languages.filter((val, i, arr) => arr.indexOf(val) === i).

https://caniuse.com/#feat=mdn-javascript_builtins_set_set_iterable_allowed

Could not find module `@ember-decorators/object` imported from `ember-best-language/services/best-language`

Thank your for the fast fix regarding the node version. After installing I am seeing the following error in the browser at runtime:

Uncaught Error: Could not find module `@ember-decorators/object` imported from `ember-best-language/services/best-language`
    at missingModule (loader.js:247)
    at findModule (loader.js:258)
    at Module.findDeps (loader.js:168)
    at findModule (loader.js:262)
    at Module.findDeps (loader.js:168)
    at findModule (loader.js:262)
    at requireModule (loader.js:24)
    at Class._extractDefaultExport (index.js:422)
    at Class.resolveOther (index.js:101)
    at Class.resolve (index.js:163)

I am unfamiliar with ember-decorators, but probably you need to move it to the dependencies section of package.json. I had a quick look at some other add ons that are using ember-decorators and this seems to be the case. For instance:
https://github.com/miguelcobain/ember-yeti-table/blob/master/package.json

ember-cli-typescript version outdated

WARNING: The 'ember-best-language' addon is using an older version of ember-cli-typescript that uses stage 1 decorators. Please update to ember-cli-typescript@v2 to remove your dependency on legacy decorators.

Requires fastboot?

ember.debug.js:26509 Error: Assertion Failed: Attempting to inject an unknown injection: 'service:fastboot'
    at new EmberError (ember.debug.js:13653)
    at assert (ember.debug.js:13895)
    at Registry.validateInjections (ember.debug.js:10792)
    at FactoryManager.create (ember.debug.js:10399)
    at Proxy.create (ember.debug.js:10168)
    at instantiateFactory (ember.debug.js:10256)
    at lookup (ember.debug.js:10213)
    at Container.lookup (ember.debug.js:10075)
    at Class.lookup (ember.debug.js:37804)
    at Class.injectedPropertyGet (ember.debug.js:29596)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.