Core: JSON files are not loading:getting 404 error with webpack

Created on 17 Nov 2016  路  6Comments  路  Source: ngx-translate/core

I'm submitting a ... (check one with "x")

[ ] bug report => check the FAQ and search github for a similar issue or PR before submitting
[ ] support request => check the FAQ and search github for a similar issue before submitting
[ ] feature request

Current behavior

I'm using webpack with ng2-translate and JSON files (ex-en.json, fr.json) are not picking up.

Most helpful comment

@yogesh51 If you follow the guide of ng2-translate correctly, you will know that you need to put this (the lines in bold below) in app.module.ts to point to the public folder that stores the JSON files.

@NgModule({
imports: [
BrowserModule,
AuthModule,
HomeModule,
CoreModule.forRoot(),
AppRoutingModule,
TranslateModule.forRoot({
provide: TranslateLoader,
useFactory: (http: Http) => new TranslateStaticLoader(http, 'dist/assets/i18n', '.json'),
deps: [Http]
})

],
declarations: [AppComponent, HeaderComponent, FooterComponent, AsideComponent],
bootstrap: [AppComponent]
})

All 6 comments

You should install Copy Webpack Plugin in this link Copy Webpack Plugin. It's used to copy files from a source to another. I tried it and now angular can find the JSON files.

The important lines to notice are these:
new CopyWebpackPlugin([ { from: 'src/assets/i18n', to: 'assets/i18n' } ])

replace the from with the folder that stores your JSON files

@ht89 How do I use that plugin? I mean where do I mention about that plugin in webpack.config.js?

More info could be found in the examples section of the plugin.

Also you can take a look at this starter that uses this plugin with angular 2: https://github.com/preboot/angular2-webpack

@SamVerschueren @ocombe I tried using copy-webpack plugin and tried to configure in webpack.dev.js in this way
`var聽webpackMerge = require('webpack-merge');
var聽ExtractTextPlugin = require('extract-text-webpack-plugin');
var聽commonConfig = require('./webpack.common.js');
var聽helpers = require('./helpers');
var聽CopyWebpackPlugin聽=聽require('copy-webpack-plugin');

module.exports聽= webpackMerge(commonConfig,聽{
聽聽聽聽devtool:聽'cheap-module-eval-source-map',

聽聽聽聽output: {
聽聽聽聽聽聽聽聽path: helpers.root('webapp'),
聽聽聽聽聽聽聽聽publicPath:聽'http://localhost:8080/',
聽聽聽聽聽聽聽// publicPath : '/',
聽聽聽聽聽聽聽聽filename:聽'[name].js',
聽聽聽聽聽聽聽聽chunkFilename:聽'[id].chunk.js'
聽聽聽聽},

聽plugins: [
聽聽聽聽聽聽聽聽new聽ExtractTextPlugin('[name].css'),

聽聽聽聽聽聽聽聽new聽CopyWebpackPlugin([聽{聽from:聽'app/i18n',聽to:聽'dist/assets/i18n'聽}聽])
聽聽聽聽],

聽聽聽聽devServer: {
聽聽聽聽聽聽聽聽contentBase: helpers.root(__dirname,聽'webapp'),
聽聽聽聽聽聽聽聽historyApiFallback:聽true,
聽聽聽聽聽聽聽聽stats:聽'minimal',
聽聽聽聽聽聽聽聽noInfo:聽false,
聽聽聽聽聽聽聽聽port:聽8080,
聽聽聽聽聽聽聽聽inline:聽true,
聽聽聽聽聽聽聽聽open:聽true
聽聽聽聽}
});`

But still getting 404 error. It is not at all recognizing the files. @ocombe Could you please suggest how did u make a reference in webpack.config.js to en & fr json files? How it gonna read those? Please guide me.

@yogesh51 If you follow the guide of ng2-translate correctly, you will know that you need to put this (the lines in bold below) in app.module.ts to point to the public folder that stores the JSON files.

@NgModule({
imports: [
BrowserModule,
AuthModule,
HomeModule,
CoreModule.forRoot(),
AppRoutingModule,
TranslateModule.forRoot({
provide: TranslateLoader,
useFactory: (http: Http) => new TranslateStaticLoader(http, 'dist/assets/i18n', '.json'),
deps: [Http]
})

],
declarations: [AppComponent, HeaderComponent, FooterComponent, AsideComponent],
bootstrap: [AppComponent]
})

Was this page helpful?
0 / 5 - 0 ratings

Related issues

IterationCorp picture IterationCorp  路  3Comments

guysan picture guysan  路  4Comments

crebuh picture crebuh  路  3Comments

rbaumi picture rbaumi  路  4Comments

pndewit picture pndewit  路  3Comments