2009-08-20 28 views
9

Tôi muốn tạo một tệp tài nguyên cho tiếng Anh Singapore (en-sg) có tên là "shopping.en-sg.resx" trong thư mục App_GlobalResources.Tạo văn hóa tùy chỉnh trong ASP.NET

Tôi gặp lỗi trong khi biên dịch.

Lỗi 1 Không gian tên 'Resources' đã có chứa một định nghĩa cho 'mua sắm' c: \ WINDOWS \ Microsoft.NET \ Framework \ v2.0.50727 \ Temporary ASP.NET Files \ web \ 2cd6afe9 \ 737b0a13 \ App_GlobalResources.vomuzavz.1.cs 26

Sau khi tìm kiếm trên Google, tôi phát hiện ra rằng "en-sg" không phải là văn hóa mặc định và tôi phải tạo văn hóa tùy chỉnh cho nó. Tôi không biết các bước chi tiết này.

Tôi nên làm gì để tạo văn hóa và xóa lỗi biên dịch?

tôi theo ví dụ trong MSDN, tạo ra một tập tin gọi là "shopping.x-en-US-sample.resx" và đặt đoạn mã sau vào chức năng BasePage của (protected override trống InitializeCulture()):

CultureAndRegionInfoBuilder cib = null; 

cib = new CultureAndRegionInfoBuilder(
    "x-en-US-sample", CultureAndRegionModifiers.None); 

CultureInfo ci = new CultureInfo("en-US"); 
cib.LoadDataFromCultureInfo(ci); 
RegionInfo ri = new RegionInfo("US"); 
cib.LoadDataFromRegionInfo(ri); 

cib.Register(); 

ci = new CultureInfo("x-en-US-sample"); 

Tuy nhiên, lỗi biên dịch vẫn tồn tại.

CẬP NHẬT:

Bạn có thể dễ dàng tạo lại vấn đề bằng cách tạo ra một trang web trống rỗng và hai tập tin "shopping.en-sg.resx" và "shopping.resx" trong thư mục app_globalresources.

Trả lời

15

Bạn có thể tạo ra một nền văn hóa mới dựa trên một nền văn hóa hiện có:

string culture = "en-sg"; 
string name = "Singaporean English"; 

CultureInfo cultureInfo = new CultureInfo("en-GB"); 
RegionInfo regionInfo = new RegionInfo(cultureInfo.Name); 

CultureAndRegionInfoBuilder cultureAndRegionInfoBuilder = new CultureAndRegionInfoBuilder(culture, CultureAndRegionModifiers.None); 

cultureAndRegionInfoBuilder.LoadDataFromCultureInfo(cultureInfo); 
cultureAndRegionInfoBuilder.LoadDataFromRegionInfo(regionInfo); 

// Custom Changes 
cultureAndRegionInfoBuilder.CultureEnglishName = name; 
cultureAndRegionInfoBuilder.CultureNativeName = name; 

cultureAndRegionInfoBuilder.Register(); 

Added: Chỉ cần kiểm tra các tài liệu tham khảo: tôi có:

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Globalization; 
using System.IO; 
using System.Reflection; 
using System.Runtime.CompilerServices; 

Đã thêm (Cập nhật trên cơ sở ý kiến):

Về thông báo lỗi:

Các lỗi mà bạn thấy là kết quả của một số cuộc xung đột tài nguyên đặt tên. Kiểm tra các tên tài nguyên, chúng được biên dịch thành các dll để bạn cần phải kiểm tra các tên không gian tên không xung đột. Bạn có thể kiểm tra điều này bằng cách sử dụng công cụ phản xạ: http://www.red-gate.com/products/reflector/

+0

Tôi nên đặt mã này ở đâu để tránh lỗi? – Billy

+0

Mã này chỉ cần chạy một lần trên máy mà bạn cần thêm văn hóa. Có lẽ bạn đang nhận được lỗi bởi vì nó đã tồn tại? –

+0

Đặt mã trong global.asax? Nếu tôi thay đổi "en-sg" thành "en-us", thì tốt thôi. Tôi chỉ muốn một đoạn mã mà tôi có thể đặt nó vào một nơi nào đó và sau đó tôi có thể sử dụng shopping.en-sg.resx – Billy

1
+0

Tôi đã thử và không hoạt động. Thêm thông tin. được thêm vào. – Billy

+1

Cố định liên kết: http://msdn.microsoft.com/en-us/library/ms172469(v=vs.100).aspx – Sprintstar

+0

Cảm ơn @Sprintstar: Tôi cũng đã cập nhật liên kết trong bài đăng của mình. –

3

Dưới đây là các bước và mã được yêu cầu để tạo văn hóa en-sg.

  1. Tạo ứng dụng bảng điều khiển.
  2. Thêm tham chiếu đến sysglobl (C: \ Windows \ Microsoft.NET \ Framework \ v2.0.50727 \ sysglobl.dll)
  3. Thêm mã bên dưới vào.
  4. Chạy nó trên (các) máy chủ web và (các) máy dev với tư cách Quản trị viên.

Nó sẽ tạo ra một nền văn hóa dựa trên những gì tôi thấy là kết quả gần nhất của nó (en-au). Sau đó tôi đã ghi đè tên vv để làm cho nó độc đáo.

Bạn chỉ phải chạy ứng dụng này một lần. Nó loại bỏ bất kỳ những cái hiện có trước khi tạo này chỉ trong trường hợp bạn muốn thực hiện bất kỳ sửa đổi sau khi chạy nó.

public static void Main() 
    { 
     CultureAndRegionInfoBuilder cib = null; 

     try 
     { 
      Console.Clear(); 
      Console.WriteLine("Unregister the \"en-SG\" " + "custom culture if it already exists..."); 
      CultureAndRegionInfoBuilder.Unregister("en-SG"); 
      Console.WriteLine("The custom culture was unregistered successfully."); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine("Error while unregistering..."); 
      Console.WriteLine(e); 
     } 

     try 
     { 
      cib = new CultureAndRegionInfoBuilder("en-SG", CultureAndRegionModifiers.None); 

      // Populate the new CultureAndRegionInfoBuilder object with culture information. 
      CultureInfo ci = new CultureInfo("en-AU"); 
      cib.LoadDataFromCultureInfo(ci); 

      // Populate the new CultureAndRegionInfoBuilder object with region information. 
      RegionInfo ri = new RegionInfo("SG"); 
      cib.LoadDataFromRegionInfo(ri); 

      cib.CultureEnglishName = "English (Singapore)"; 
      cib.CultureNativeName = "English (Singapore)"; 
      cib.IsMetric = true; 

      // Display some of the properties of the CultureAndRegionInfoBuilder object. 
      Console.WriteLine("CultureName:. . . . . . . . . . {0}", cib.CultureName); 
      Console.WriteLine("CultureEnglishName: . . . . . . {0}", cib.CultureEnglishName); 
      Console.WriteLine("CultureNativeName:. . . . . . . {0}", cib.CultureNativeName); 
      Console.WriteLine("GeoId:. . . . . . . . . . . . . {0}", cib.GeoId); 
      Console.WriteLine("IsMetric: . . . . . . . . . . . {0}", cib.IsMetric); 
      Console.WriteLine("ISOCurrencySymbol:. . . . . . . {0}", cib.ISOCurrencySymbol); 
      Console.WriteLine("RegionEnglishName:. . . . . . . {0}", cib.RegionEnglishName); 
      Console.WriteLine("RegionName: . . . . . . . . . . {0}", cib.RegionName); 
      Console.WriteLine("RegionNativeName: . . . . . . . {0}", cib.RegionNativeName); 
      Console.WriteLine("ThreeLetterISOLanguageName: . . {0}", cib.ThreeLetterISOLanguageName); 
      Console.WriteLine("ThreeLetterISORegionName: . . . {0}", cib.ThreeLetterISORegionName); 
      Console.WriteLine("ThreeLetterWindowsLanguageName: {0}", cib.ThreeLetterWindowsLanguageName); 
      Console.WriteLine("ThreeLetterWindowsRegionName: . {0}", cib.ThreeLetterWindowsRegionName); 
      Console.WriteLine("TwoLetterISOLanguageName: . . . {0}", cib.TwoLetterISOLanguageName); 
      Console.WriteLine("TwoLetterISORegionName: . . . . {0}", cib.TwoLetterISORegionName); 
      Console.WriteLine(); 

      // Register the custom culture. 
      Console.WriteLine("Register the custom culture..."); 
      cib.Register(); 

      // Display some of the properties of the custom culture. 
      ci = new CultureInfo("en-SG"); 

      Console.WriteLine("Name: . . . . . . . . . . . . . {0}", ci.Name); 
      Console.WriteLine("EnglishName:. . . . . . . . . . {0}", ci.EnglishName); 
      Console.WriteLine("NativeName: . . . . . . . . . . {0}", ci.NativeName); 
      Console.WriteLine("TwoLetterISOLanguageName: . . . {0}", ci.TwoLetterISOLanguageName); 
      Console.WriteLine("ThreeLetterISOLanguageName: . . {0}", ci.ThreeLetterISOLanguageName); 
      Console.WriteLine("ThreeLetterWindowsLanguageName: {0}", ci.ThreeLetterWindowsLanguageName); 

     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e); 
     } 
     Console.ReadKey(); 
    } 
Các vấn đề liên quan