npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

http-partial-loader

v2.0.3

Published

Library QuickStart package.json from the documentation, supplemented with testing support

Downloads

5

Readme

julienbourgain/http-partial-loader Build Status npm version

A loader for ngx-translate that loads translation using http.

Get the complete changelog here: https://github.com/julienbourgain/http-partial-loader/releases

Installation

We assume that you already installed ngx-translate.

Now you need to install the npm module for TranslateHttpPartialLoader:

npm install julienbourgain/http-partial-loader --save

Usage

1. Setup the TranslateModule to use the TranslateHttpPartialLoader:

The TranslateHttpPartialLoader uses Http to load translations, which means that you have to import the HttpModule from @angular/http before the TranslateModule:

import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {HttpModule, Http} from '@angular/http';
import {TranslateModule, TranslateLoader} from '@ngx-translate/core';
import {TranslateHttpPartialLoader} from 'julienbourgain/http-partial-loader';
import {AppComponent} from "./app";

// AoT requires an exported function for factories
export function HttpLoaderFactory(http: Http) {
    return new TranslateHttpPartialLoader(http);
}

@NgModule({
    imports: [
        BrowserModule,
        HttpModule,
        LanguageModule.forRoot({prefix: './i18n/', suffix: '.json'})
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

The TranslateHttpPartialLoader also has two optional parameters:

  • prefix: string = "/assets/i18n/"
  • suffix: string = ".json"

By using those default parameters, it will load your translations files for the lang "en" from: /assets/i18n/en.json.

You can change those in the HttpLoaderFactory method that we just defined. For example if you want to load the "en" translations from /public/lang-files/en-lang.json you would use:

export function HttpLoaderFactory(http: Http) {
    return new TranslateHttpPartialLoader(http, "/public/lang-files/", "-lang.json");
}

For now this loader only support the json format.

Because ngx-translate doesn't target partial translations, we need to inform the partialLoader witch parts of the current language translation load. When you split translation in multiple translation files, one ngx page, need 0, 1 or many translation parts.

So to do this with @angular/router for example, you need to add a resolver to your routes

@Injectable()
export class TranslatePartialResolver implements Resolve<any> {
  constructor(private translateConfig: TranslateConfig) {}

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) 
    switch ((<Type<any>>route.component).name) {
      case AppComponent.name:
        return this.translateConfig.addPartials(['global', 'toastr']);
      case LoginComponent.name:
        return this.translateConfig.addPartials(['login']);
    }
  }
}

And you just add the resolver in your routes like :

{
  path     : 'app',
  component: AppComponent,
  resolve: {
    translatePartialLoader: TranslatePartialResolver
  }
}

Currently you cannot remove partials to load when you change the language, because it's difficult to know if a translation is in use or not. Also, translation parts adding needs to reload the language. And we cache already loaded parts.

For this reasons, translateService.reloadLang('en') doesn't works with this loader. But I invite you to use julienbourgain/partial-translate-ott to have a more friendly API :)