question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Partial Loader failing when preferred language .json files are missing.

See original GitHub issue

When I set the $translateProvider to use the partialLoader it crashes and burns when it can’t find a file that matches the preferredLanguage rather than gracefully handling that scenario and using the fallbackLanguage.

e.g.

$translateProvider.useLoader("$translatePartialLoader", {
    urlTemplate: "{part}.{lang}.json"
});

$translatePartialLoaderProvider.addPart("app/i18n/shared");

$translateProvider.preferredLanguage("en-AU");
$translateProvider.fallbackLanguage("en");
/app
    /i18n
        /shared.en.json
        ... missing shared.en-AU.json

I noticed that there may be a way to pass in a loadFailHandler for the $translatePartialLoader but can’t figure out how to supply that handler and more importantly if that’s the recommended way I should be handling this situation.

http://plnkr.co/edit/BlWaG27IbTPioKImLZiM?p=preview

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
benpriebecommented, Mar 6, 2014

For anyone who comes across this post, this is how I created an error handler that will resolve an empty translation table when the loader can’t find a server side file. This will allow your fallback file translation tables to be used.

angular.module("appModule")
  .factory("i18nPartialLoaderErrorHandler", ["$q", function ($q) {
    return function (partName, languageKey) {
      return $q.when({});
    };
}]);

$translateProvider.useLoader("$translatePartialLoader", {
  urlTemplate: "{part}/{lang}",
  loadFailureHandler: "i18nPartialLoaderErrorHandler"
});

If you use this approach with physical files however the browser will report a 404 in the error console/network tab which isn’t ideal.

The solution I ended up going with was to use a server side endpoint that dynamically returned the contents of the file (if it existed) otherwise just returned an empty translation table “{}”.

e.g.

Instead of something like this:

http://app.website.com/i18n/string.en-AU.js

I used something like this:

http://app.website.com/i18n/strings/en-AU

And had the server resolve that address to a physical file if it existed and returned the contents as “application/json”. If the file didn’t exist, I just returned “{}”.

Hope this makes sense.

0reactions
DWandcommented, Apr 12, 2015

@knalli, I’d say this one can be closed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

angular-translate didn't attempt to use the fallback language if ...
When using angular-translate to localize my Angular app, the fallback language is not attempted if the preferred language (via static file loader) failed...
Read more >
Configure schema inference and evolution in Auto Loader
The column is missing from the schema. Type mismatches. Case mismatches. The rescued data column contains a JSON containing the rescued columns ...
Read more >
Python Tutorial: json.dump(s) & json.load(s) - 2021
dumps()" returns a string as indicated by the "s" at the end of "dumps". This process is called encoding. Let's write it to...
Read more >
Apache Tomcat 9 (9.0.70) - Changelog
In a normal Tomcat configuration, this will be the system class loader. ... Use client's preferred language for the Server Status page of...
Read more >
Content Types - ESBuild
This loader is enabled by default for .js , .cjs , and .mjs files. The .cjs extension is used by node for CommonJS...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found