Ionic Translate


#1

Hi, I have a doubt in Angular nx-translate service. I’m doing localisation with en as the language by default. At later point, If user wants to change the language, get the particular language resource from a remote server and make it local and pointing to that file, my language should be loaded in the rest of my application. How to achieve this. Can you help me out.


#2

What did you try and what isn’t working yet?


#3

I tried to create a dynamic file in the documents directory and for the TranslateLoader I give the path from the cordova plugin. But im getting Cordova not found error as Factory method called before cordova platforms ready method called.


#4

Cordova plugin?

Check out this starter project that has full translation support: https://github.com/ionic-team/ionic-starter-super/blob/6fb926d044c76a13dd7553f925f3df7ee70fa55b/src/app/app.component.ts#L77


#5

Even in the template you shared, All my language resource should be available in assets/i18n/ folder. But my requirement is I don’t have the resource folder with me . So programatically I’m going to create this resource files and then I have to point that file in the TranslateModule.forRoot() method. Here UseFactory key was there. I want to change this factory to call dynamcially after some time like once the file is created.

My another doubt, is how to give the factory at some later point instead of defining in the app.module.ts. Is it possible if so Please guide me in it.


#6

Read the docs of the module used to load the translations:
https://github.com/ngx-translate/http-loader


#7

Can you please help me how to use the useStaticFilesLoader method for the translator in ionic2 typescript to provide the file location where all the language resource file available?


#8

I have used TranslateService.setTranslation method to feed the new language resource and used that language. But still I dont have any idea that whats the difference between feeding json object in the above method and using factory method dependent with http to feed the language resource json file location.


#9

for useStaticFilesLoader, in app.module.ts:

  import {TranslateModule, TranslateLoader} from '@ngx-translate/core';
  import {TranslateHttpLoader} from '@ngx-translate/http-loader';

 ...
 
 export function exportTranslateStaticLoader(http: Http) {
    return new TranslateHttpLoader(http, './assets/i18n/', '.json');
 }
 
 @NgModule({
   ...
   imports: [
      ...
      TranslateModule.forRoot({
            loader: {
                provide: TranslateLoader,
                useFactory: exportTranslateStaticLoader,
                deps: [Http]
            }
        }
    )

#10

In this case, If all my language resources are not available with the app and it will be available only with web service call, then will this solution works?


#11

Look like your question is too complicated for me sorry can’t help.


#12

Maybe a bug, look like some people are asking the same questions today…