0

I have a UWP app that has a set of text files, one for every language that we support. I want to read the contents of one text file based on the language that the system is set to. For example lets say the text file is called MyTextFile.txt and the 2 languages I support are English and French, I would have the following 2 files in my package:

fr-fr\MyTextFile.txt

en-us\MyTextFile.txt

If the system is set to French, I want to load and read the French text file and display it's contents. My current solution is only working on my dev machine, when I deploy my app to a different machine it only works if the OS is English. If I set the OS to French and install my app, it does not load the text file. This is what I am doing:

1) I have my text files store under [appname]\Files[language]\MyTextFile.txt 2) I have a props file that loads the files, it contains this:

<PropertyGroup>
    <FileDir>$(SolutionDir)App\[appname]\Files\</EulaDir>
  </PropertyGroup>
  <ItemGroup>    
    <Content Include="$(FileDir)\en-us\MyTextFile.txt">
      <Link>en-us\MyTextFile.txt</Link>
    </Content>
    <Content Include="$(FileDir)\fr-fr\MyTextFile.txt">
      <Link>fr-fr\MyTextFile.txt</Link>
    </Content>
  </ItemGroup>
</Project>

3) My code (which works in English) loads the file like this:

var rc = ResourceManager.Current.MainResourceMap.GetSubtree("Files").GetValue("MyTextFile.txt", ResourceContext.GetForCurrentView());

var lang = rc.Qualifiers.FirstOrDefault(q => q.QualifierName == "Language").QualifierValue;

// Get the file
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///" + lang + "/" + "MyTextFile.txt"));

// Read the contents of the file
UI.Text = await FileIO.ReadTextAsync(file, UnicodeEncoding.Utf8);

Perhaps there is a better way for loading this text file in a different language? It has to be a text file because it contains some legal text and can't be a string in a resw file

thanks

George

Kristian Vukusic
  • 3,284
  • 6
  • 30
  • 46

2 Answers2

0

You don't need to specify the language manually, just read the file as normal.

// Get the file
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///MyTextFile.txt"));

// Read the contents of the file
UI.Text = await FileIO.ReadTextAsync(file, UnicodeEncoding.Utf8);
tao
  • 760
  • 1
  • 7
  • 16
0

Perhaps there is a better way for loading this text file in a different language?

It is suggested to use application resources to separate localizable content from app code. Resources enable apps to be localized into multiple languages, customized for various device DPIs and form factors, and tailored for specific types of users.

For more information about localization of your app, you can refer to Globalization and localization, and here is an official Application resources and localization sample, in this sample you can see that it is encouraged to separate your resources, such as strings and files, from your app code, so you can maintain and alter your resources independently.

Grace Feng
  • 16,564
  • 2
  • 22
  • 45
  • In my case I needed a separate file, which was to be localized separately by our legal team. They aren't being localized like other strings. The solution above worked great – user6115111 Sep 06 '16 at 16:13