1

i am working to create a multilang Application, and i want to read the "array" from file, i don t know from where to start.

My english.lang looks like:

LOGIN_TITLE = Test login
LOGIN_CREATE_ACC_LABEL = Create an account
LOGIN_REMEMBER_ME_CHECKBOX = Remember Me
LOGIN_AUTO_LOGIN = Auto Login
LOGIN_STATUS_WELCOME = Welcome to Client
LOGIN_STATUS_WRONG = Invalid username or password
LOGIN_SING_IN_BUTTON = Sing In

And i am looking for smth like:

string = getLang("LOGIN_TITLE");

I have an ideea about how to do, but is not optimal, i am thinking to do smth like: read from file line by line up to line who contains "LOGIN_TITLE" and then replace "LOGIN_TITLE = " with blank, and the remains should be "Test login", the string i needed

I think it will have low performance if my lang file is too big.

Do you have any ideea about how to do that?

3 Answers3

1

I would use a dictionary

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.txt";
        static void Main(string[] args)
        {
            Header header = new Header(FILENAME);
        }
    }
    public class Header
    {
        Dictionary<string, string> dict = new Dictionary<string, string>();

        public Header(string filename)
        {
            StreamReader reader = new StreamReader(filename);

            string inputLine = "";

            while ((inputLine = reader.ReadLine()) != null)
            {
                inputLine = inputLine.Trim();

                string[] inputArray = inputLine.Split(new char[] { '=' }).ToArray();
                dict.Add(inputArray[0].Trim(), inputArray[1].Trim());

            }

        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
0

the best way is using resource file in multi languages application;

look this site

and chage the away. working with text file will be get your time what is important

Shahrooz Ansari
  • 2,637
  • 1
  • 13
  • 21
0

I think you better NOT do it yourslef.

Localization is most common problem in end-user application development and were solved long ago your question came up. Please, see another answer on stackoverflow: How to use localization in C#

Fredrik Mörk briefly explains how to create/use resource localization files in your code and gives simple example.

Community
  • 1
  • 1
eocron
  • 6,885
  • 1
  • 21
  • 50